All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET中的doPostBack脚本函数实例_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/27 6:48:06 Browse times: 366 Comment times: 0

ASP.NET中的doPostBack脚本函数实例_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET中的doPostBack脚本函数实例_[Asp.Net教程]

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



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控件回送页面:



onclick="javascript:__doPostBack(’Button1’, ’Button Click’);" />

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 + "
");
Response.Write("Arguments: " + args + "
");
}
}

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

  阻止asp:Button提交页面:




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


设计家园 整理




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.