我的頁面上有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