c# - 更改链接按钮颜色

标签 c# asp.net linkbutton calendar-control

我有一个日历控件,其中填充了不同的事件。当一个事件只持续一天时,我希望它将链接按钮颜色更改为红色。

 public partial class RequestCalander : System.Web.UI.Page
{
    private DataSet GetData()
    {
        ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;

        var sql = "select (substring(status, 1,1)) AS stat1, lastname, firstname, lstdate, lenddate, requestid from [table] E inner join [tableEvent] T on E.EMPID = T.emppid where E.depdivid = '" + @UsrDepartment + "'";

        using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
        {
            conn.Open();

            using (iDB2Command cmd = new iDB2Command(sql, conn))
            {
                cmd.DeriveParameters();

                using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    return ds;
                }
            }
        }
    }

    private String GetConnectionString()
    {
        ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;

        return cssc["connStringEvent"].ToString();
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        e.Cell.Width = 150;
        e.Cell.Height = 100;
        DataSet ds = GetData();

        string link = "<a class='turnred' href='viewRequestForm.aspx?bwrequestid=";

        string s = e.Day.Date.ToShortDateString();

        e.Cell.Text = e.Day.Date.Day.ToString() + "<BR>";

        LiteralControl l = new LiteralControl();
        l.Text = e.Day.Date.Day.ToString() + "<BR>";
        e.Cell.Controls.Add(l);

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            string scheduledDate = Convert.ToDateTime(row["lstdate"]).ToShortDateString();
            string endDate = Convert.ToDateTime(row["lenddate"]).ToShortDateString();

            e.Cell.Width = 120;
            e.Cell.Height = 100;

            Int32 start = 0;
            Int32 end = 0;

            start = string.CompareOrdinal(scheduledDate, s);
            end = string.CompareOrdinal(endDate, s);

            if ((start > 0) || (end < 0) || (e.Day.IsWeekend == true))
            {
                return;
            }

            HyperLink hl = new HyperLink();
            hl.Text = "yes";

            if (scheduledDate.CompareTo(endDate) == 0){
                    hl.ForeColor = System.Drawing.Color.Red;

                e.Cell.Controls.Add(hl);
               }
                if ((start <= 0) & (end >= 0) & (!e.Day.IsWeekend))
                {
                    HyperLink lb = new HyperLink();
                    lb.Text = link + (Int64)row["bwrequestid"] + "' >" + row["lastname"] + "</a>" as String + "(" + row["stat1"] + ")" as String + "<br />";
                    lb.ForeColor = System.Drawing.Color.Red;
                    e.Cell.Controls.Add(lb);
                    lb.NavigateUrl = "~/Form.aspx?requestid={0}";
    }
}

aspx代码

    <%@ Page Title="Leave Request" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Calander.aspx.cs" Inherits="RequestCalander" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%@ Register TagPrefix="ec" Namespace="ControlSample" Assembly="EventCalendar" %>


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <link type="text/css" rel="Stylesheet" href="../../Styles/Calendar.css" /> 
    <style type="text/css">
        .style1{width: 782px;}
        .style2{width: 883px;}

    .changecolor { color: Red; }

    </style>
    </asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
                <table class="style1">
                        <tr>
                             <td class="style2">
                                 <table class="style4">
                                     <tr>
                                         <td>
                                           <asp:Button ID="backButton" runat="server" Text="Back"  Width="100" />
                                           <asp:Button ID="requestButton" runat="server" OnClick="goToPageRequest" Text="Make Request"  Width="100" />
                                          </td>
                                     </tr>
                                </table>
                            </td>
                        </tr>
                       <tr>
                           <td class="style2">
                            <asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" Font-Size="Large" Width="901px" style="font-family: 'Times New Roman', Times, serif" >
                                <TitleStyle Font-Bold="true" Font-Size="X-large" BorderStyle="Solid" />
                                <DayHeaderStyle CssClass="caldays" Height="30px" BorderStyle="Solid" />
                                <DayStyle CssClass="calcurrentmonth" Font-Size="X-small" Height="100px" />
                                <TodayDayStyle CssClass="calcurrentday" Width="120px" Height="100px"/>
                                <WeekendDayStyle CssClass="calweekend" Width="120px" Height="100px"/>
                                <OtherMonthDayStyle CssClass="calothermonth" Width="120px" Height="100px"/>
                            </asp:Calendar>
                           </td>
                        </tr>
                </table>
</asp:Content>

CSS

/************************************************************************
* 
* Calendar specific formatting 
*
************************************************************************/

/* Surrounds the calendar */
.eventmonth
{
    padding-left: 1px;
    padding-right: 1px;
    padding-top: 1px;
    text-align: center;
}

.changecolor
{ 
    color:red;
}


/* used as the cssclass of the actual calendar */
.eventmonthtable
{
    margin-right: 0px;
    margin-left: 8px;
    position: relative;
    margin-bottom: 9px;
    border: 1px solid #666666;
    border-collapse:collapse;
    top: 1px;
    left: -228px;
    height: 38px;
    width: 722px;
}


.dayNumber
{
    color :Background;
    float: right;
    border-bottom: 1px solid #666666;
    border-left: 1px solid #666666;
    clear: none;
    padding: 2px;
}

.linkbutton
{
    color:red;
}
.calcurrentmonth
{

}
.calothermonth
{
    background-color: #6B9EB9;

}
.calcurrentday
{
    background-color: #B0C9FF;

}
.calweekend
{
    background-color: #6B9EB9;
}

.calcurrentmonth , .calcurrentmonth , .calothermonth , .calcurrentday , .calweekend
{

    text-align: left;
    border: 2px solid #000000;
    vertical-align: top;
    /* needed for positioning the dayNumber part */
    position:relative;
    border-collapse:separate;
    border-spacing: 2px;

}

.nextlink
{
    position:absolute;
    right:0;
    padding-right:15px;    
}

链接包含在日历控件中。

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>Attendance</title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>

                </h1>
                <asp:Table ID="Table1" runat="server">
                   <asp:TableRow>
                      <asp:TableCell>
                            <h2>ATTENDANCE&nbsp;
                            <img src="/images/LogoDrop.jpg" alt="" height="20" width="15" />
                            &nbsp;
                            EXCEPTION LEAVE TIME</h2>
                      </asp:TableCell>
                  </asp:TableRow>
                </asp:Table>
            </div>
            <div class="loginDisplay">
                <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                    <AnonymousTemplate>
                        [ <a href="/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="/Login.aspx"/> ]
                    </LoggedInTemplate>
                </asp:LoginView>
            </div>
            <div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" 
                    EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" StaticDisplayLevels="1">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="PayPeriodProcessing" Selected="True"  ToolTip="Exception Leave Entry; Approval; Payroll " />
                        <asp:MenuItem NavigateUrl="~/Request/Time.aspx" Text="Leave Request" Selected="True"  ToolTip="Leave Request " />
                        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
                     </Items>
                </asp:Menu>
            </div>
        </div>
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
        <asp:Label ID="lblCopyright" runat="server"> </asp:Label>
    </div>
    </form>
</body>
</html>

最佳答案

您可能需要进一步描述您的代码,但这是我的初步想法。

代码前端

<style type="text/css">
    .disabledbtn { color: Red; }
</style>

<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" Font-Size="Large" Width="901px" style="font-family: 'Times New Roman', Times, serif" >
    <TitleStyle Font-Bold="true" Font-Size="X-large" BorderStyle="Solid" />
    <DayHeaderStyle CssClass="caldays" Height="30px" BorderStyle="Solid" />
    <DayStyle CssClass="calcurrentmonth" Font-Size="X-small" Height="100px" />
    <TodayDayStyle CssClass="calcurrentday" Width="120px" Height="100px"/>
    <WeekendDayStyle CssClass="calweekend" Width="120px" Height="100px"/>
    <OtherMonthDayStyle CssClass="calothermonth" Width="120px" Height="100px"/>
</asp:Calendar>

代码隐藏

问题是您创建了两个链接。一个没有任何链接文本,设置为红色,另一个有名称和状态。

您永远不会看到红色文本,因为它没有可显示的文本。我稍微简化了您的代码,现在可以在我这边运行了。

这是我创建的用于测试的假 DataSet,但您的应该取代它。

private DataSet GetData()
{
    // create a test data set
    DataTable dt = new DataTable();
    dt.Columns.Add("stat1");
    dt.Columns.Add("lastname");
    dt.Columns.Add("firstname");
    dt.Columns.Add("lstdate");
    dt.Columns.Add("lenddate");
    dt.Columns.Add("bwrequestid");

    dt.Rows.Add("P", "Doe", "John", "08/16/2012", "08/16/2012", 1);
    dt.Rows.Add("P", "Doe", "Jane", "08/14/2012", "08/17/2012", 2);
    dt.Rows.Add("C", "Black", "Jack", "08/12/2012", "08/12/2012", 3);
    dt.Rows.Add("C", "Morgan", "Dexter", "08/01/2012", "08/07/2012", 4);
    dt.Rows.Add("S", "Swearengen", "Al", "08/15/2012", "08/20/2012", 5);
    dt.Rows.Add("S", "Kirk", "James T", "08/04/2012", "08/04/2012", 6);
    dt.Rows.Add("P", "Bundy", "Al", "08/07/2012", "08/07/2012", 7);

    DataSet ds = new DataSet();
    ds.Tables.Add(dt);

    return ds;
}

我相信这应该可以正确呈现您的日历。有一件事我不确定,你希望周末约会总是是红色的吗?

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    e.Cell.Width = 150;
    e.Cell.Height = 100;
    DataSet ds = GetData();

    foreach (DataRow row in ds.Tables[0].Rows)
    {
        DateTime scheduledDate = Convert.ToDateTime(row["lstdate"]);
        DateTime endDate = Convert.ToDateTime(row["lenddate"]);

        // make sure it meets your criteria
        if ((e.Day.Date >= scheduledDate && e.Day.Date <= endDate))
        {
            // create the hyperlink
            HyperLink hl = new HyperLink();
            hl.Text = "<br />" + row["lastname"].ToString() + "(" + row["stat1"].ToString() + ")";
            hl.NavigateUrl = "~/Form.aspx?requestid=" + row["bwrequestid"].ToString();

            // if the start and end dates are the same day, make it red
            if (scheduledDate.CompareTo(endDate) == 0)
                hl.CssClass = "changecolor";

            // add control to cell
            e.Cell.Controls.Add(hl);
        }
    }
}

更新这是带有新代码的输出。

enter image description here

关于c# - 更改链接按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942978/

相关文章:

c# - 我需要 C# 中的 Shannon–Fano 算法

asp.net - ServiceStack 和 SignalR 一起在同一个项目中

asp.net - jQuery 在 LinkBut​​ton 上调用 __doPostBack

ASP.NET GridView : Confirm button click postback with jQueryUI dialog

c# - 如何使用 WebElement selenium C# 按名称获取表中列的索引

c# - 如何更改嵌套 json 结构中的键值对 C#

c# - TempData 只能访问一次?

javascript - 如何在 Javascript 中向用户显示是/否消息框?

javascript - 如何从用户控件访问隐藏字段

c# - 链接按钮 href url