__doPostBack function
__doPostBack function
Hi everyone.
Today I am going to talk about the __doPostBack function, because there is some confusion with using this function.
You can see this __doPostBack function in your ASP.NET generated HTML code.
The function takes the following two arguments:
eventTarget - This contains the ID of the control that caused the post back.
eventArgument - This contains any additional data associated with the control.
In any ASP.NET page the two hidden fields: __EVENTTARGET and __EVENTARGUMENT are automatically declared. When a page is posted back to the server ASP.NET inspects __EVENTTARGET and __EVENTARGUMENT
values and this way it can decide which of the controls caused the page
to be posted back and what is the event that has to be handled.
The value of the parameters eventTarget and eventArgument
are stored in the hidden fields. The two hidden variables can be
accessed from the code behind using the forms or params collection.
If we inspect the code of the <span class="Apple-style-span">__doPostBack</span>
function, we can see that it first sets the values of two hidden fields
with the two parameters passed to the function. After this, the page is
submitted back to the server. The ID of the control which causes the
postback is stored in the __EVENTTARGET hidden field, so you can find
the control which caused the postback.
<a id="LinkButton1" href="javascript:__doPostBack( 'LButton3','' )">LinkButton</a>
You can see the function call __doPostBack('LButton3','') in the href and the argument passed for eventTarget is "LButton3" which is the id of the link button control (EventSource)
Example
1.Add two hidden fields inside the form.
<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">
2.Add javascript under the Head tag.
直接点击button的话,不会触发__doPostBack方法。会自动触发button后台绑定的click事件
<script>
function __doPostBack( eventTarget, eventArgument )
{
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
3.Add two controls.
<a id="LButton3" href="javascript:__doPostBack('Button2','')">LinkButton</a>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
4.Add function in your cs page.
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Welcome to Student Academic Blog");
}
5.You also need some code in the code behind to capture the postback and fire the event.
In the PageLoad method add.
if (Request.Form["__EVENTTARGET"] == "Button2")
{
// Fire event
Button2_Click( this, new EventArgs( ) );
}
This will capture the posted variable __EVENTTARGET and cause it to fire the event "Button2_Click".
You can also pass an event argument along with the target in case you need to pass something to your code behind:
__doPostBack( "Button2', '<event argument here>' )
This would be captured in the code behind as Request.Form["__EVENTARGUEMENT"]
So this is how you can use __doPostBack
__doPostBack function的更多相关文章
- 如何解决ASP.NET网站'__doPostBack' is undefined的脚本错误
最近碰到一个很奇怪的问题! 新发布一个ASP.NET的网站,基于.net 4.0 framework,基本没有用到什么特别新的技术,但是由于给客户预览用的服务器比较老,操作系统用的是Windows 2 ...
- (转)深入理解 __doPostBack
在我的随笔<Page,你是怎样处理回发事件的?>中曾提出一个疑问,如何得到引起页面PostBack的控件?通过阅读Page类的源码,误打误撞,无意中看到了__EVENTTARGET和__E ...
- __doPostBack初识
周五在公司看到有看到Request.Params["__EVENTARGUMENT"]的代码不解,不解遂上网查找,发现一篇文章<Understanding the JavaS ...
- 深入理解asp.net中的 __doPostBack函数
前段时间做一个.net网站的时候,用到了模拟前端按钮刷新updatePanel进行局部刷新的时候,遇见了这个问题,当时没顾上记下来,查看网上资料,记下来留着以后查看. 很早以前,当我刚接触asp.NE ...
- [转]jQuery UI Dialog Modal Popup Yes No Confirm example in ASP.Net
本文转自:http://www.aspsnippets.com/Articles/jQuery-UI-Dialog-Modal-Popup-Yes-No-Confirm-example-in-ASPN ...
- response 后刷新页面,点击按钮后,禁用该按钮
一,正常的点击按钮后,将其灰显,全部执行完毕再正常显示. this.btnSave.Attributes.Add("onclick", "if (typeof(Page_ ...
- Asp.net中Postback及Callback
我们知道,在默认的情况下,当我们点击Asp.net Page中的一个服务器Button时(默认其实是Submit Form),会导致Page被Recreated,这个过程我们称之为Postback,它 ...
- 浅谈ASP.NET的Postback
说道ASP.NET的Postback,就得说Web Page的生命周期,但是Web Page的生命周期却不是三言两语就能够说得清楚的,所以在这里单纯站的编程的角度,撇开Web Page 的生命周期浅谈 ...
- easyui combogrid 多选加载,保存,显示代码
1.调用代码 UTIL.SetDict($("#txt_ExcludeIndustry_"), "SECTOR_TYPE", true, true, funct ...
随机推荐
- O007、KVM 存储虚拟化
参考https://www.cnblogs.com/CloudMan6/p/5273283.html KVM 的存储虚拟化是通过存储池(Storage Pool) 和 卷(Volume)来管理的. ...
- 分布式的几件小事(六)dubbo如何做服务治理、服务降级以及重试
1.服务治理 服务治理主要作用是改变运行时服务的行为和选址逻辑,达到限流,权重配置等目的. ①调用链路自动生成 一个大型的分布式系统,会由大量的服务组成,那么这些服务之间的依赖关系和调用链路会很复杂, ...
- 07 Python之字典和集合
1. 什么是字典 字典是用{}表示,以key:value的形式来保存数据的,其查找效率比较高 坑: 字典存储数据时是用哈希值来存储的,算法不能变(python的) 数据必须是可哈希的(不可变的),字典 ...
- PHP之常用操作
在最高权限下执行相关命令 1)查看PHP配置 php --ini Configuration File (php.ini) Path: /www/server/php//etc Loaded Conf ...
- 工作中apache 403的一个小问题
最近在虚拟机上安装hadoop, 需要设备本地的网络源,所以启用了apache. 由于需要,首先修改了家目录的位置 指向/opt/www 然后修改家目录的配置文件 修改完成之后重启服务,访问目录 ...
- EF报错:对一个或多个实体的验证失败(Entity Framework 强制转换失败数据异常处理方法)
1.使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信 ...
- 图书管理系统UML建模
图书管理系统UML建模 用例图 借阅者请求服务用例图 图书管理员处理借书还书用例图 系统管理员系统维护用例图 时序图 系统管理员添加书籍时序图 协作图 借阅者预留书籍协作图 状态图 书的状态图 活动图 ...
- Vivado添加sublime text编辑器
我们当用vivado会发现文本编辑器有点鸡肋,没有自动的缩进的功能,所以我想用sublime来进行文本的编辑,下面就是绑定的一些方法(但是呢其实吧,虽然可以绑定却不能实时的报错,,,我感觉我还是老老实 ...
- Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)
B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...
- 零拷贝的原理及Java实现
在谈论Kafka高性能时不得不提到零拷贝.Kafka通过采用零拷贝大大提供了应用性能,减少了内核和用户模式之间的上下文切换次数.那么什么是零拷贝,如何实现零拷贝呢? 什么是零拷贝 WIKI中对其有如下 ...