четверг, 28 января 2010 г.

__doPostBack function

hi every one Today i am going to talk about __doPostBack function.because there is some confusion in using _dopostback

You can see this __doPostBack function in your asp.net generated HTML code.
The function takes two arguments are:

1) eventTarget

2) eventArgument

1) EventTarget contains the ID of the control that causes the postback

eventArgument contains any additional data associated with the control.


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 postedback and what is the event that has to be handled.
In any asp.net page the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the 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.

My Anaylsis(Aamir Hasan)

If we inspect the code of the __doPostBack function we can see that it first sets the values of two hidden fields created by ASP .NET named __EVENTTARGET and__EVENTARGUMENT with the two parameters passed to the function. After this the page is submitted back to the server.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 postedback and what is the event that has to be handled.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 fucntion call "__doPostBack('LButton3','')" in href and the argument passed for eventTarget is "LButton3" which is the id of the linkbutton control (EventSource)

Example

i am going to talk aamirhasan.aspx Page.

in aamirhasan.aspx Page

1) Add two hidden filed inside the form as given below

<input type ="hidden" name ="__EVENTTARGET" value ="">
<input type ="hidden" name ="__EVENTARGUMENT" value ="">

2) Add javascript under the Head tag as given below

<script>
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>

3) add two control given below

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

So this is how you can use _doPostBack

Enjoy it

Note:ultimately a postback will only fire, if there is a control in the page with a property "autopostback=true".

Комментариев нет: