這至少是你如何做到這一點的起點。我不確定所有的邏輯是100%,但它是開始...
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
DisableEvent(control);
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
UpdateViewstate(control);
}
}
private void DisableEvent(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
if (IsPostBack)
{
if (Session["update" + control.ID].ToString() != ViewState["update" + control.ID].ToString())
{
RemoveClickEvent((Button)control);
}
else
{
((Button)control).Click += new EventHandler(Button_Disable);
}
}
else
{
Session["update" + control.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}
DisableEvent(control);
}
}
private void UpdateViewstate(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
ViewState["update" + control.ID] = Session["update" + control.ID];
}
UpdateViewstate(control);
}
}
void RemoveClickEvent(Button b) {
System.Reflection.FieldInfo f1 = typeof(Button).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
object obj = f1.GetValue(b);
System.Reflection.PropertyInfo pi = typeof(Button).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
System.ComponentModel.EventHandlerList list = (System.ComponentModel.EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}
protected void Button_Disable(object sender, EventArgs e)
{
Button b = (Button)sender;
Session["update" + b.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
來源
2009-09-03 10:01:01
awe
我明白...但在這個頁面,我自己提供了一個刷新按鈕,用戶可以使用刷新內容...我想阻止按鈕點擊和其他事件再次被解僱。 – 2009-09-03 08:17:36
@The King--你自己的刷新按鈕與內置於瀏覽器中的刷新按鈕有什麼關係? – RichardOD 2009-09-03 08:51:02
我自己的刷新按鈕只是重新加載頁面,並且不會再次觸發最後一個控件事件。 – 2009-09-03 09:51:17