我的页面上有64个按钮代表一个团队(这是一种NCAA括号类型的东西)。页面载入的按钮作为关键字存储在字典中。团队是一个存储团队信息的对象。在初始页面加载时,会创建事件处理程序,但当通过单击其中一个按钮提交后,页面将重新加载时,处理程序将消失。当我试图将它们添加到每个页面加载时,我在添加完后检查了处理程序,但它们仍然不在那里。关于在词典中的按钮对象的ASP.NET:EventHandler问题
这是通过字典访问原始对象的问题吗?
我使用的是这样的:
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);
}
}
为什么不将数据绑定到网格? – bleepzter 2012-02-24 21:40:56