我有一個固定的ImageButton和一些ImageButtons在Page_Load動態添加的表。在頁面「查看源」他們看起來(比ID之外)相同:動態添加imagebuttons不添加到page.Request.Form
<tr>
<td>
<input type="image" name="ctl00$cphMain$ibSel_020" id="cphMain_ibSel_020" src="/Images/Deselected.gif" />
</td>
<td>Fixed ImageButton
</td>
</tr>
<tr>
<td>
<input type="image" name="ctl00$cphMain$ibSel_001" id="cphMain_ibSel_001" src="/Images/Deselected.gif" />
</td>
<td>Dynamic ImageButton
</td>
</tr>
....plus ten more dynamically added ImageButtons ibSel_002 through ibSel_011.
在Page_Load事件我確定哪些控制通過page.Request.Form迭代導致回發。如果我點擊固定的ImageButton,如果在page.Request.Form中找到「ibSel_020」。如果我點擊任何動態ImageButton,則找不到ID。
我知道所有ImageButton都會發生回傳,因爲我更新(在Page_Load中)帶有ID的標籤(用於測試目的) - 對於所有動態ImageButton都是空白的,對於固定的是「ibSel_020」的ImageButton。
我該如何判斷哪個動態添加的ImageButton導致了回發?
下面是一些代碼來生成ImageButtons:
for(int Q = 0; Q < g_zTypes.GetLength(0); Q++){
TableRow tr = new TableRow();
TableCell tc = new TableCell(); //Add image button to row
ImageButton ib = new ImageButton();
ib.ID = "ibSel_" + Q.ToString().PadLeft(3, '0');
ib.ImageUrl = "/Images/Deselected.gif";
tc.Controls.Add(ib);
tr.Cells.Add(tc);
tc = new TableCell(); //Add extra cell to row
tc.Text = g_zTypes[Q, 1];
tr.Cells.Add(tc);
tblType.Rows.Add(tr);
}
希望它能幫助。
UPDATE:
爲了測試,我寫的代碼塊堆積在字符串中的所有元素page.Request.Form。在Page_Load的最後,我是Response.Writing zMagic。這裏是塊:
//=============MAGIC CODE=========================
Control cF;
string zF;
foreach(string zControl in Page.Request.Form){
//For ImageButtons crop off mouse coordinates property
if(zControl.EndsWith(".x") || zControl.EndsWith(".y")){
cF = Page.FindControl(zControl.Substring(0, zControl.Length - 2));
}
else{
cF = Page.FindControl(zControl);
}
zF = (cF == null) ? "NULL" : cF.ToString();
zMagic += "<br />Form[" + zControl + "]=[" + zF + "]<br />";
}
zMagic += "[END]";
//================================================
我發現,動態添加ImageButtons實際上目前無論在哪裏在Page_Load中,我堅持「魔典」的。發生了什麼事情是Page.FindControl()失敗了 - 但只有在動態ImageButton被重新創建的代碼之前有'Magic Code'的時候。
BOTTOM LINE:您不能Page.FindControl(zDynamicControl)直到它被重新創建。
你是如何生成和添加這些按鈕? – Andrei
將代碼添加到您創建按鈕的位置 – Plue
@Murali - 呃,呈現的HTML位於我的原始文章中。 –