我有一個包含動態創建的命令按鈕的頁面。在ASP.NET中動態添加命令按鈕
我需要動態連接一個點擊事件,以便我可以抓取點擊按鈕的CommandArgument。
Button b = new Button();
b.ID = "btnTrigger2";
b.CssClass = "hiddenBtn";
b.CommandName = "lbl3";
b.Click += new EventHandler(btnTrigger_Click);
的問題是最後一行 - 我不能線了一個事件處理這樣 - 我需要使用標準的EventArgs而不是CommandEventArgs。
任何人有任何建議讓這個工作?還有的必須是一個辦法......
編輯
想我會張貼終於在工作情況下,其他人試圖做同樣的事情的代碼。
`string tabLoadedScript = string.Empty; string postBackScript = string.Empty;
string script = " <script language='javascript' type='text/javascript'>" + System.Environment.NewLine;
script += "function clientActiveTabChanged(sender, args) {" + System.Environment.NewLine;
int i = 0;
foreach(TabPanel tp in tc1.Tabs)
{
Button b = new Button();
b.ID = "btn" + tp.ClientID;
b.CssClass = "hiddenBtn";
b.CommandName = tp.ID;
b.Command += btnTrigger_Click;
this.form1.Controls.Add(b);
AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
trg.ControlID = "btn" + tp.ClientID;
tabLoadedScript += "var isTab" + tp.ClientID + "loaded=$get('" + tp.Controls[0].ClientID + "');" + System.Environment.NewLine;
postBackScript += "if(!isTab" + tp.ClientID + "loaded && sender.get_activeTabIndex() == " + i + ") {" + System.Environment.NewLine;
postBackScript += "__doPostBack('" + b.ClientID + "','');}" + System.Environment.NewLine;
i++;
}
script += tabLoadedScript;
script += postBackScript;
script += "}" + System.Environment.NewLine;
script += "</script>";
this.ClientScript.RegisterClientScriptBlock(this.GetType(),「cs」,script,false);
protected void btnTrigger_Click(object sender,CommandEventArgs e) System.Threading.Thread.Sleep(2500); Panel ctrl =(Panel)FindControlRecursive(this,「pnl」+ e.CommandName); ctrl.Visible = true; }
public static Control FindControlRecursive(Control Root, string Id)
{
if(Root.ID == Id)
return Root;
foreach(Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if(FoundCtl != null)
return FoundCtl;
}
return null;
}
`
謝謝你們的迴應! \ – Tim 2011-04-04 13:41:36