简体   繁体   English

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

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

I have 64 buttons on my page that represent a team (it's a sort of NCAA bracket type thing). 我页面上有64个代表团队的按钮(这是NCAA括号类型的内容)。 The buttons on page load are stored into a Dictionary as a key. 页面加载时的按钮作为键存储在Dictionary中。 Team is an object that stores information about the team. 团队是存储有关团队信息的对象。 On the initial page load, the event handlers are built, but when the page reloads after a submit by clicking one of the buttons, the handler disappears. 在初始页面加载时,将构建事件处理程序,但是当通过单击按钮之一提交后重新加载页面时,处理程序消失。 When I tried to have it add them on every page load, I checked the handlers after they are added, and they're still not there anymore. 当我尝试让它在每次页面加载时添加它们时,我在添加处理程序后检查了这些处理程序,现在它们不再存在。

Is this an issue accessing the original object via the Dictionary? 通过词典访问原始对象是否有问题?

I'm using it like this: 我正在这样使用它:

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

Any ideas? 有任何想法吗?

Page load: 页面加载:

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

}

Load Buttons: 加载按钮:

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

The handlers need to be added on every page cycle in the oninit method. 需要在oninit方法的每个页面周期中添加处理程序。 Try that and see what happens. 试试看,看看会发生什么。 If that doesn't work, try posting some more complete page code. 如果这样不起作用,请尝试发布一些更完整的页面代码。 Also, if that doesn't work, can you specify how the buttons are created? 另外,如果那行不通,您可以指定按钮的创建方式吗? Are they just present in the aspx or are they dynamically created? 它们只是出现在aspx中还是动态创建的? If they are dynamically created, make sure you assign an ID to each of them on creation. 如果是动态创建的,请确保在创建时为它们分配一个ID。

The pages in ASP.NET are recreated every time a request is made, therefore all the dynamically added event handlers are lost with each new request. 每次发出请求时都会重新创建ASP.NET中的页面,因此,每个新请求都会丢失所有动态添加的事件处理程序。 So as swannee already said, you need to add them in the OnInit method (OnLoad, Page_Load would work too) every time the page is requested. 因此,正如swannee所说的那样,每次请求页面时,都需要在OnInit方法中添加它们(OnLoad,Page_Load也可以)。 If you are interested in understanding the ASP.NET page life cycle better, check this page . 如果您有兴趣更好地了解ASP.NET页面的生命周期,请查看此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM