我以編程方式創建了2個按鈕。現在我想知道在頁面加載時按下哪一個按鈕....按鈕將觸發頁面加載事件,然後是它自己的事件。有沒有辦法知道在PageLoad事件中是否按下了一個控件?
在它激發自己的事件之前,我想知道頁面加載事件是否按下了哪個按鈕。
有沒有辦法找出答案?
我的按鈕都是這樣
Button btnMoveThread = new Button();
btnMoveThread.Text = "1";
btnMoveThread.Command += ModeratorButtonPassTo_Click;
Button btnMoveThread = new Button();
btnMoveThread.Text = "2";
btnMoveThread.Command += ModeratorButtonPassTo_Click;
此功能不會工作:
public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)
{
Control control = null;
string ctrlname = page.Request.Params["__EVENTTARGET"];
if (ctrlname != null && ctrlname != String.Empty)
{
control = page.FindControl(ctrlname);
}
// if __EVENTTARGET is null, the control is a button type and we need to
// iterate over the form collection to find it
else
{
string ctrlStr = String.Empty;
Control c = null;
foreach (string ctl in page.Request.Form)
{
// handle ImageButton controls ...
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
ctrlStr = ctl.Substring(0, ctl.Length - 2);
c = page.FindControl(ctrlStr);
}
else
{
c = page.FindControl(ctl);
}
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton)
{
control = c;
break;
}
}
}
return control;
}
它不會在屏幕上打印任何東西。它是一個動態控制:例如,按鈕b =新的按鈕()...不是頁面上放置的按鈕..我將按鈕提供給頁面上的佔位符.. – Matrix001
@Matrix沒問題。事件目標仍將包含導致回帖的原因。這取決於你想要用它做什麼。 –
什麼都沒有打印在頁面上......自己測試 – Matrix001