i am trying to use __doPostBack
function so i can force my page to do post pack on the page load but i am having some difficulties understanding.
when i was looking at the examples online. On button click, i want to do the post back but not sure how to complete the code in the code behind.
Here is what i have so far:
<script type="text/javascript">
function DoPostBack() {
__doPostBack('btnNew', 'MyArgument');
}
</script>
Here is my button
<asp:Button ID="btnNew" runat="server" CausesValidation="False" CommandName="New" OnClick="DoPostBack()" Text="Create" />
I don't seem to understand to use "MyArgument" in the code behind. What do i need to do in the code behind so it does post back on the page load? thanks in advance for the assistance.
Understanding the JavaScript __doPostBack Function. This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.
The __EVENTARGUMENT is any relevant event arguments regarding the control performing the postback. For most controls, there are no specialized event arguments, and since event arguments are different for every control, null is passed to represent a default argument should be created during the event sequence.
A postback is the exchange of information between servers to report a user's action on a website, network, or app.
Scenario 1
If you want to use your JavaScript function to trigger the postback, you need to replace the OnClick
with OnClientClick
. Modify the button definition like this (I'm already assuming that it's nested inside an UpdatePanel
):
<asp:Button ID="btnNew"
runat="server"
CausesValidation="False"
CommandName="New"
OnClientClick="DoPostBack();"
Text="Create" />
In the code behind, in the Page_Load
method, read the Request["__EVENTTARGET"]
and the Request["__EVENTARGUMENT"]
:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request["__EVENTTARGET"] == "btnNew" &&
Request["__EVENTARGUMENT"] == "MyArgument")
{
//do something
}
}
}
Scenario 2
If you don't necessarily want to use JavaScript, modify the button's definition like this:
<asp:Button ID="btnNew"
runat="server"
CausesValidation="False"
CommandName="New"
OnClick="DoPostback"
CommandArgument="MyArgument"
Text="Create" />
Then, add the following method in the code behind:
protected void DoPostback(object sender, EventArgs e)
{
var target = ((Button)(sender)).ClientID; //"btnNew"
var argument = ((Button)(sender)).CommandArgument; //"MyArgument"
if (target == "btnNew" &&
argument == "MyArgument")
{
//do something
}
}
The values that are passed to the function __doPostBack
as arguments will be sent to the server as request parameters named __EVENTTARGET
and __EVENTARGUMENT
. ASP.NET uses these values to determine what control has fired an event and what arguments should be passed as EventArgs
object. But you can access them directly using object HttpRequest
:
string eventTarget = this.Request.Params.Get("__EVENTTARGET");
string eventArgument = this.Request.Params.Get("__EVENTARGUMENT");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With