zoukankan      html  css  js  c++  java
  • 客户端如何触发服务器端控件的事件

    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>");
       }
    }

  • 相关阅读:
    oracle 表误更新 (flashback )闪回操作
    经典sql语句大全
    如何让程序自动更新
    C#如何测试代码运行时间
    oracle复制表的sql语句
    NeatUpload的安装使用 文件上传。可传大文件。
    c#实现ftp上传代码
    C# ftp 上传、下载、删除
    oracle cursor 与refcursor及sys_refcursor的区别 -游标(cursor)应用-实例
    Oracle中Merge语句的使用
  • 原文地址:https://www.cnblogs.com/zhwl/p/1969585.html
Copyright © 2011-2022 走看看