How to call PostBack from Javascript

Source: Internet
Author: User

Introduction

PostBack
Is a concept introduced in ASP. NET and is a very handy method. PostBack
Is built into the ASP. NET and most of the Web controls support it
Without writing any code.

Calling PostBack event from Javascript

There
May be some scenario where you may be want to explicitly PostBack to
Server using some clientside Javascript. It is pretty simple to do this.

ASP. NET already creates a client side JavaScript method as shown below to support postbacks for the Web controls:


function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}

So, all you have to do is, just call this method with appropriate arguments. You may call this as shown below:


<script language='Javascript'>
__doPostBack('__Page', 'MyCustomArgument');
</script>

However,
It is not recommended to use this method name directly in the client
Side. The best approach is, generate this piece of code from the code
Behind file using ASP. NET. This way, you are safe even if Microsoft
Later change the name of the method '_ dopostback' to something else in
A future release.

In your code behind file, declare a protected variable as shown below:



Protected PostBackStr As String

Now, in the page load event, write the following code:



PostBackStr = Page.ClientScript.GetPostBackEventReference(Me, "MyCustomArgument")

The
Method getpostbackeventreference () will generate the same piece
Client Side code that you need to use to call the PostBack method.
Instead of harcoding the method name _ dopostback, we are asking
ASP. NET to tell us what is the method name.

Now Insert the following code in your ASPX page:


<script language='Javascript'>
<%= PostBackStr %>
</script>

At runtime, it will be evaluated:


<script language='Javascript'>
__doPostBack('__Page', 'MyCustomArgument');
</script>

Remember
To insert the above script into some JavaScript method/event where you
Want to call the PostBack, instead of simply inserting into the page
Shown above.

How to identify and handle the PostBack in code behind?

You
Found how to call the PostBack from Javascript. Now you need a way
Identify your PostBack in the code behind file. The second argument
Dopostback method becomes helpful here.

Go to the code behind file and write the following code in the page load event:


If Page.IsPostBack Then
Dim eventArg As String = Request("__EVENTARGUMENT")
If eventArg = "MyCustomArgument" Then
Response.Write("You got it !")
End If
End If

Did
You notice how we identify if the page is loaded as part of our
PostBack? We used the second argument in the _ dopostback method
Pass a value and used that in pageload to identify if it is called as
Result of our PostBack.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.