ASP.NET:Dictionary中的Button对象上的EventHandler问题

时间:2012-02-24 21:33:26

标签: c# asp.net .net

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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

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