首页
技术库|站长工具|技术手册|字体库|知识点词汇表| 联系我们|
打开本页的html静态页面
 

主菜单

文章分类

.: .Net技术 .: .Net分析与设计 .: ASP.NET中客户端触发服务器端控件的事件(GetPostBackEventReference方法)

  • 全文内容
  • 发表评论
  • 文章点评
  • 文章附件
  • Email文章
  • 打印文章

ASP.NET中客户端触发服务器端控件的事件(GetPostBackEventReference方法)

点击次数:574 创建日期:11-7-2007 录入:cn-web.com 字体:[ ] 点评:


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

前台页面
有个服务器控件
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

一个客户端控件用来触发服务器端

<href="#" onclick="document.getElementById('Button1').click()">触发服务器端按钮事件</a>

 (2)
利用GetPostBackEventReference给客户端生成__doPostBack()
前台  

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

 


请文明参与讨论,禁止漫骂攻击。
评论总数:0 [ 查看全部 ] 网友评论
此文章还没有任何评论!
(+5分)
(+4分)
(+3分)
(+2分)
(+1分)
附件下载Default2.rar [0.9 KB 下载次数 97]
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号