将href添加到Eval

时间:2014-01-08 12:18:33

标签: c# asp.net eval href

我使用转发器制作了日历。它将正确列出所有日期等。最终它将需要每天预订。我现在喜欢做的是使用aspx页面将每个日期编号作为链接。我链接到链接的部分是这个区域“<%#Eval(”DAY“)%>”。我希望它重定向到我的bookings.aspx页面。谢谢你的帮助。

C#:

public partial class Calendar : System.Web.UI.Page
{
    int month = 1;
    int year = 2014;
    protected void Page_Load(object sender, EventArgs e)
    {
        month = int.Parse(CurrentDate.Value);
    }
    protected override void OnPreRender(EventArgs e)
    {
        System.Globalization.DateTimeFormatInfo monthname = new System.Globalization.DateTimeFormatInfo();
        string strMonthName = monthname.GetAbbreviatedMonthName(month);
        MonthLabel.Text = strMonthName;
        YearLabel.Text = year.ToString();
        List<DayOb> thismonthsdays = new List<DayOb>();
        string datefirst = month.ToString() + "/1/" + year.ToString();
        DateTime fst = DateTime.Parse(datefirst);
        int dow = (int)fst.DayOfWeek;
        for (int i = 0; i < dow; i++)
        {
            if (i == 0)
            {
                thismonthsdays.Add(new DayOb { CSS = "day sunday", Day = "X" });
            }
            else
            {
                thismonthsdays.Add(new DayOb { CSS = "day", Day = "X" });
            }

        }
        int endofmonth = fst.AddMonths(1).AddDays(-1).Date.Day;
        for (int i = 1; i <= endofmonth; i++)
        {
            DayOfWeek thisdow = DateTime.Parse(month.ToString() + "/" + i.ToString() + "/" + year.ToString()).DayOfWeek;
            thismonthsdays.Add(new DayOb { CSS = "day " + thisdow.ToString().ToLower(), Day = i.ToString() });
        }
        cal.DataSource = thismonthsdays;
        cal.DataBind();
        base.OnPreRender(e);
    }
    class DayOb
    {
        public string CSS { get; set; }
        public string Day { get; set; }
    }

HTML:

<asp:Repeater ID="cal" runat="server">
    <HeaderTemplate>
        <div class='day sunday header'>

            Su
        </div>
        <div class='day header'>
            M
        </div>
        <div class='day header'>
            T
        </div>
        <div class='day header'>
            W
        </div>
        <div class='day header'>
            T
        </div>
        <div class='day header'>
            F
        </div>
        <div class='day header'>
            Sa
        </div>
    </HeaderTemplate>
    <ItemTemplate>
        <div class='<%# Eval("CSS") %>'>
            <%# Eval("DAY") %>

        </div>
    </ItemTemplate>
</asp:Repeater>

1 个答案:

答案 0 :(得分:2)

您可以使用HyperLink之类的

 <asp:HyperLink ID="lnkDetails" 
 NavigateUrl='<%# Eval("DAY", "~/bookings.aspx?ID={0}") %>'
 runat="server">
    <%# Eval("DAY") %>
 </asp:HyperLink>