__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.

__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

 

 

 

 

 

 

 

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(241)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-07-09 C#中的异常处理
2015-07-09 How to: Create a Windows Communication Foundation Client
2015-07-09 Sum of Digits / Digital Root
2015-07-09 Multiples of 3 and 5
2015-07-09 Moduli number system
2015-07-09 Case swapping
2014-07-09 zedgraph多个graphpane的处理
点击右上角即可分享
微信分享提示