更改链接按钮颜色

时间:2012-08-13 22:01:57

标签: 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>

1 个答案:

答案 0 :(得分:1)

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

代码前端

<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