ASP.NET:字典中Button对象的EventHandler问题

本文关键字:对象 EventHandler 问题 Button NET 字典 ASP | 更新日期: 2023-09-27 18:25:39

我的页面上有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++;
                }
            }
        }
    }
}

LoadTeams:

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);
    }
}

ASP.NET:字典中Button对象的EventHandler问题

需要在oninit方法中的每个页面循环中添加处理程序。试试看会发生什么。如果这不起作用,请尝试发布一些更完整的页面代码。此外,如果这不起作用,你能指定按钮是如何创建的吗?它们只是出现在aspx中,还是动态创建的?如果它们是动态创建的,请确保在创建时为每个它们分配一个ID。

每次发出请求时都会重新创建ASP.NET中的页面,因此,每次新请求都会丢失所有动态添加的事件处理程序。因此,正如swannee已经说过的,每次请求页面时,都需要将它们添加到OnInit方法中(OnLoad、Page_Load也可以)。如果您有兴趣更好地了解ASP.NET页面的生命周期,请查看此页面。