簡體   English   中英

ASP.NET:字典中的Button對象上的EventHandler問題

[英]ASP.NET: EventHandler issue on Button object in Dictionary

我頁面上有64個代表團隊的按鈕(這是NCAA括號類型的內容)。 頁面加載時的按鈕作為鍵存儲在Dictionary中。 團隊是存儲有關團隊信息的對象。 在初始頁面加載時,將構建事件處理程序,但是當通過單擊按鈕之一提交后重新加載頁面時,處理程序消失。 當我嘗試讓它在每次頁面加載時添加它們時,我在添加處理程序后檢查了這些處理程序,現在它們不再存在。

通過詞典訪問原始對象是否有問題?

我正在這樣使用它:

foreach (KeyValuePair<Button, Team> tb in TeamButtons){
    tb.Key.Click += new EventHandler(Tournament_Click);
}

有任何想法嗎?

頁面加載:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Team> teamlist;
        for (int i = 1; i < 5; i++)
        {
            using (Db db = new Db(ServerConnection.DEV))
            {
                using (SqlDataReader dr = db.GetData("SELECT A.TeamId, A.Description 'TeamName', A.Seed, A.RegionId, B.Quadrant, B.Description 'RegionName' " +
                                                    "FROM Team A JOIN Region B on B.RegionId = A.RegionId WHERE B.Quadrant=" + i.ToString()))
                {
                    teamlist = new List<Team>();
                    while (dr.Read())
                    {
                        teamlist.Add(new Team(
                            dr["TeamId"].ToString(),
                            dr["TeamName"].ToString(),
                            Convert.ToInt16(dr["Seed"]),
                            dr["RegionId"].ToString(),
                            dr["RegionName"].ToString(),
                            Convert.ToInt16(dr["Quadrant"])
                            ));
                    }
                    switch (i)
                    {
                        case 1: LoadButtons(Quad1, teamlist); break;
                        case 2: LoadButtons(Quad2, teamlist); break;
                        case 3: LoadButtons(Quad3, teamlist); break;
                        case 4: LoadButtons(Quad4, teamlist); break;
                    }
                }
            }
        }

        FFButtons = new Dictionary<Button, Team>();
        FTButtons = new Dictionary<Button, Team>();
        Winner = new Team();

        FFButtons.Add(btnQ1FFL, new Team());
        FFButtons.Add(btnQ2FFL, new Team());
        FFButtons.Add(btnQ3FFR, new Team());
        FFButtons.Add(btnQ4FFR, new Team());
        FTButtons.Add(btnLFT, new Team());
        FTButtons.Add(btnRFT, new Team());

        Session["TeamButtons"] = TeamButtons;
        Session["FFButtons"] = FFButtons;
        Session["FTButtons"] = FTButtons;
        Session["Winner"] = Winner;

        ResetWinners(64);
    }
    else
    {
        TeamButtons = (Dictionary<Button, Team>)Session["TeamButtons"];
        FFButtons = (Dictionary<Button, Team>)Session["FFButtons"];
        FTButtons = (Dictionary<Button, Team>)Session["FTButtons"];
        Winner = (Team)Session["Winner"];
    }

    LoadTeams();

}

加載按鈕:

private void LoadButtons(System.Web.UI.HtmlControls.HtmlTable table, List<Team> teamlist)
{
    int i = 0;
    foreach (System.Web.UI.HtmlControls.HtmlTableRow c in table.Rows)
    {
        foreach (System.Web.UI.HtmlControls.HtmlTableCell d in c.Cells)
        {
            foreach (Control f in d.Controls)
            {
                if (f is Button)
                {
                    TeamButtons.Add((Button)f, teamlist[i]);
                    i++;
                }
            }
        }
    }
}

負載團隊:

private void LoadTeams()
{
    foreach (KeyValuePair<Button, Team> tb in TeamButtons)
    {
        tb.Key.Text = TeamText(tb.Value);
        switch (tb.Value.Quadrant)
        {
            case 1:
                tb.Key.Click += new EventHandler(Tournament1_Click);
                break;
            case 2:
                tb.Key.Click += new EventHandler(Tournament2_Click);
                break;
            case 3:
                tb.Key.Click += new EventHandler(Tournament3_Click);
                break;
            case 4:
                tb.Key.Click += new EventHandler(Tournament4_Click);
                break;
        }
    }
    foreach (KeyValuePair<Button, Team> tb in FFButtons)
    {
        tb.Key.Text = TeamText(tb.Value);
        if (tb.Value.Quadrant <= 2) tb.Key.Click += new EventHandler(TournamentFourL_Click);
        else tb.Key.Click += new EventHandler(TournamentFourR_Click);
    }
    foreach (KeyValuePair<Button, Team> tb in FTButtons)
    {
        tb.Key.Text = TeamText(tb.Value);
        tb.Key.Click += new EventHandler(TournamentTwo_Click);
    }
}

需要在oninit方法的每個頁面周期中添加處理程序。 試試看,看看會發生什么。 如果這樣不起作用,請嘗試發布一些更完整的頁面代碼。 另外,如果那行不通,您可以指定按鈕的創建方式嗎? 它們只是出現在aspx中還是動態創建的? 如果是動態創建的,請確保在創建時為它們分配一個ID。

每次發出請求時都會重新創建ASP.NET中的頁面,因此,每個新請求都會丟失所有動態添加的事件處理程序。 因此,正如swannee所說的那樣,每次請求頁面時,都需要在OnInit方法中添加它們(OnLoad,Page_Load也可以)。 如果您有興趣更好地了解ASP.NET頁面的生命周期,請查看此頁面

暫無
暫無

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

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