GetPostBackEventReference

1.__doPostBack("id","")方法
2.GetPostBackEventReference方法作用
3.客户端如何触发服务器端控件的事件
 
右边提供程序用此方法实现在客户端单击按钮后,禁用此按钮,直到程序运行完毕再开启按钮。(单击右边下载)
 
下面再举个小例子.

前台页面
有个服务器控件 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
一个客户端控件用来触发服务器端

<a href="#" onclick="document.getElementById('Button1').click()">触发服务器端按钮事件</a>
 (2)
利用GetPostBackEventReference给客户端生成__doPostBack()
前台 

<a href="#" onclick="<%=PostBack()%>">触发服务器端按钮事件</a>
 后台

protected string PostBack()
        {
            return this.Page.GetPostBackEventReference(this.Button1,"haha");
        }
通过__EVENTARGUMENT="haha"可以判断是不是点了那个链接的PostBack
把Button1的按钮事件这么写:


if(Request["__EVENTARGUMENT" ]=="haha")
            {
                Response.Write("这个是链接的PostBack");
            }
            else
            {
                Response.Write("这个不是链接的PostBack");
            }

 以下这个是微软MSDN上的例子:

public class MyControl : Control, IPostBackEventHandler
{
  
   // Create an integer property that is displayed when
   // the page that contains this control is requested
   // and save it to the control's ViewState property.
   public int Number
   {
     get
     {
       if ( ViewState["Number"] !=null )
          return (int) ViewState["Number"];
       return 50;
     }

     set
     {
       ViewState["Number"] = value;
     }       
   }

   // Implement the RaisePostBackEvent method from the
   // IPostBackEventHandler interface. If 'inc' is passed
   // to this method, it increases the Number property by one.
   // If 'dec' is passed to this method, it decreases the
   // Number property by one.
   public void RaisePostBackEvent(string eventArgument)
   {
     if ( eventArgument == "inc" )
     Number = Number + 1;

     if ( eventArgument == "dec" )
     Number = Number - 1;
   }

  
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
   protected override void Render(HtmlTextWriter writer)
   {
     // Converts the Number property to a string and
 // writes it to the containing page.
     writer.Write("The Number is " + Number.ToString() + " (" );

 // Uses the GetPostBackEventReference method to pass
 // 'inc' to the RaisePostBackEvent method when the link
 // this code creates is clicked.
     writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"inc") + "\">Increase Number</a>");

     writer.Write(" or ");

 // Uses the GetPostBackEventReference method to pass
 // 'dec' to the RaisePostBackEvent method when the link
 // this code creates is clicked.
     writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"dec") + "\">Decrease Number</a>");
   }
}

posted @ 2008-04-11 23:20  hemingchen  阅读(3531)  评论(1编辑  收藏  举报