当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中的doPostBack脚本函数实例

ASP.NET
ASP.NET立即上手教程(13)
ASP.NET立即上手教程(14)
Repeater控件分页例子
从文本文件读取行信息
Asp.Net 2.0数据库基本操作方法学习
url传递中文的解决方案
如何实现无刷新的DropdownList联动效果
将非模态对话框显示为模态对话框
微软新版开发工具VS 2008 beta2功能定案
c#.net函数列表
.Net FW中无法正确显示中文问题
ASP.NET中的doPostBack脚本函数实例
教你在asp.net中动态变更CSS
一个功能齐全的DataGrid分页例子
在ASP.NET程序中创建唯一序号
asp.net 2.0中用GRIDVIEW插入新记录
ASP.Net中保护自定义的服务器控件
在ASP.NET中跨页面实现多选
转换DataSet到普通xml的新法
ASP.NET中用healthMonitor属性用法

ASP.NET中的doPostBack脚本函数实例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 52 ::
收藏到网摘: n/a

今天来说说当ASP.NET中的doPostBack脚本函数的应用,ASPX页面有包含asp:LinkButton或者带有AutoPostBack属性且其值为true的服务器控件时,ASP.NET会自动为页面生成下面的脚本:

以下为引用的内容:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> 
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /> 
function __doPostBack(eventTarget, eventArgument) { 
    if(!theForm.onsubmit || (theForm.onsubmit() != false)) { 
        theForm.__EVENTTARGET.value = eventTarget; 
        theForm.__EVENTARGUMENT.value = eventArgument; 
        theForm.submit(); 
       } 
}

__doPostBack带有两个参数:eventTarget和eventArgument。

eventTarget是引起回送的控件的ID,eventArgument是回调参数(与控件相关的附加数据)。这两个参数分别由隐藏的两个表单域__ EVENTTARGET和__ EVENTARGUMENT保存。

使用这两个隐藏的表单可以查找引起页面回送的控件ID和回送时的参数:

以下为引用的内容:
  protected void Page_Load(object sender, EventArgs e)
  {
  string target = Request.Params["__EVENTTARGET"];
  string args = Request.Params["__EVENTARGUMENT"];
  }

因为asp:Button和asp:ImageButton不是使用__doPostBack回送页面,所以使用这两个控件回送页面时,上面的代码是无效的。

使用HTML控件回送页面:

以下为引用的内容:
<form id="form1" runat="server"> 
<asp:LinkButton ID="LinkButton1" runat="server"></asp:LinkButton> 
<input type="button" value="Client Control" onclick="javascript:__doPostBack(’Button1’, ’Button Click’);" /> 
</form> 
protected void Page_Load(object sender, EventArgs e) 

    if(this.IsPostBack) 
    { 
        string target = Request.Params["__EVENTTARGET"]; 
        string args = Request.Params["__EVENTARGUMENT"]; 
        Response.Write("Button ID: " + target + "<br />"); 
        Response.Write("Arguments: " + args + "<br />"); 
    } 
}

加入的目的是为了让ASPX自动生成__doPostBack脚本。

阻止asp:Button提交页面:

以下为引用的内容:

<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" /> 
</form> 
protected void Page_Load(object sender, EventArgs e) 

    string scr = "return confirm(’Are you sure you want to submit this form?’);"; 
    this.Button1.Attributes.Add("onclick", scr); 
}