簡體   English   中英

動態創建的按鈕,但為他們創建的事件未觸發

[英]Button created dynamically but the event created for them is not firing

我正在使用for循環動態創建按鈕,我想獲取我單擊的任何按鈕的文本並將其顯示在標簽中。 我嘗試了以下代碼,該代碼動態創建按鈕,並在單擊查找按鈕時獲得每月的幾周時間。 在單擊按鈕時,它不會給出任何響應,並且用戶也不會觸發創建的事件:

C#

protected void _btnFind_Click(object sender, EventArgs e)
{

        int month = Convert.ToInt32(_cmbMonths.SelectedValue);
        int year = Convert.ToInt32(_cmbYears.SelectedValue);
        var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;
        IEnumerable<int> daysInMonth = Enumerable.Range(1, cal.GetDaysInMonth(year, month));

        List<Tuple<int, DateTime, DateTime>> listOfWorkWeeks = daysInMonth
            .Select(day => new DateTime(year, month, day))
            .GroupBy(d => cal.GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
            .Select(g => Tuple.Create(g.Key, g.First(), g.Last(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)))
            .ToList();

        // Item1 = week in year, Item2 = first day, Item3 = last working day
        int weekNum = 1;
        foreach (var weekGroup in listOfWorkWeeks)
        {

            //lnk.Text = "Week " + weekNum;
            Button lnk = new Button();

            lnk.Text = "Week " + weekNum++ + " (" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) +
                " " + weekGroup.Item2.Day + " to " +
                System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " +
                weekGroup.Item3.Day + ")";
            lnk.Click += new EventHandler(lnk_Click);

            Panel1.Controls.Add(lnk);
            Panel1.Controls.Add(new LiteralControl("<br/><br/>"));

        }           
}

void lnk_Click(object sender, EventArgs e)
{
    Button but = sender as Button;
    Label1.Text = but.Text;
}

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Year&nbsp<asp:DropDownList ID="_cmbYears" runat="server"></asp:DropDownList>
        Month&nbsp<asp:DropDownList ID="_cmbMonths" runat="server">
            <asp:ListItem Value="1">January</asp:ListItem>
            <asp:ListItem Value="2">February</asp:ListItem>
            <asp:ListItem Value="3">March</asp:ListItem>
            <asp:ListItem Value="4">April</asp:ListItem>
            <asp:ListItem Value="5">May</asp:ListItem>
            <asp:ListItem Value="6">June</asp:ListItem>
            <asp:ListItem Value="7">July</asp:ListItem>
            <asp:ListItem Value="8">August</asp:ListItem>
            <asp:ListItem Value="9">September</asp:ListItem>
            <asp:ListItem Value="10">October</asp:ListItem>
            <asp:ListItem Value="11">Novermber</asp:ListItem>
            <asp:ListItem Value="12">December</asp:ListItem>
        </asp:DropDownList>
        &nbsp<asp:Button ID="_btnFind" runat="server" Text="Find" OnClick="_btnFind_Click"/>
    </div>
        <br />
        <asp:Panel ID="Panel1" runat="server">

        </asp:Panel>
        <br />
        <br />
        <b><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></b>
    </form>
</body>
</html>

您正在foreach中創建控件。 每個人都應該有一個唯一的ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM