__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

最新文章

  1. PostgreSQL保存文件到数据库
  2. 【转】浅析Sql Server参数化查询
  3. [Z] 关于c++ typename的另一种用法
  4. 侧菜单栏的实现SlidingPaneLayout
  5. JSON转换类
  6. &lt;input&gt;标签中获得鼠标与否的样式变化——js实现
  7. 一个关于ExtJS4具体控件的详细教程
  8. 【CodeForces】【311C】Fetch the Treasures
  9. 第三部分 MediaPlayer的主要实现分析
  10. 文件和目录之umask函数
  11. linux中shell如何输出换行符
  12. Scala--样例类(case)详解
  13. AndroidTestCase测试用法
  14. git_自动同步_sync.sh
  15. 在嵌入式设备中使用 JavaScript 的前景
  16. python面向对象编程进阶
  17. 2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践
  18. Python基础学习---比较运算符
  19. Nginx的核心功能及应用实战
  20. Flutter之MaterialApp使用详解

热门文章

  1. python 字符串前面加r,u的含义
  2. html/css中map和area的应用
  3. Vue项目里添加特殊字体或 某些字体乱码的问题
  4. LaTeX的tasks宏包
  5. oracle存储过程临时表
  6. 从程序员小仙飞升上神,java技术开发要如何实现?
  7. centos 7 Network 脚本
  8. linux库(程序)与包名联系
  9. 时序数据库influxDB存储数据grafana展示数据
  10. 02bag模板