Hi everyone. 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:
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.
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 fields inside the form as given below
<input name="__EVENTTARGET" value="" type="hidden">
<input name="__EVENTARGUMENT" value="" type="hidden">
2) Add javascript under the Head tag as given below
<script><br /> function __doPostBack(eventTarget, eventArgument) {<br /> document.Form1.__EVENTTARGET.value = eventTarget;<br /> document.Form1.__EVENTARGUMENT.value = eventArgument;<br /> document.Form1.submit();<br /> }<br /> </script>
3) add two control given below
<a id="LButton3" href="javascript:__doPostBack('Button2','')">LinkButton</a>
<asp:button onclick="Button2_Click" id="Button2" runat="server" 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:
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:
javascript:__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
Enjoy it</event></asp:button>
No comments:
Post a Comment