博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to call Postback from Javascript
阅读量:4031 次
发布时间:2019-05-24

本文共 2632 字,大约阅读时间需要 8 分钟。

 

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 want to explicitly postback to the 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:
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 of 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:
At runtime, it will be evaluated as:
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 as 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 to identify your postback in the code behind file. The second argument the 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 to pass a value and used that in PageLoad to identify if it is called as a result of our PostBack.

转载地址:http://jcqbi.baihongyu.com/

你可能感兴趣的文章
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
查看>>
Arrays.asList()
查看>>
Big Endian 和 Little Endian
查看>>
java中重写方法应遵循规则
查看>>
Comparable的使用(用于Arrays.sort)
查看>>
Comparator(用于Arrays.sort)
查看>>
对自己的计划
查看>>
反应c语言程序结构特点的程序
查看>>
Android错误总结
查看>>
android margin
查看>>
Drawable setBounds()中的rect
查看>>
markdown编辑器
查看>>
过拟合原因及解决
查看>>
支持向量机(SVM)初探
查看>>
决策树与随机森林初探
查看>>
相似度与距离算法种类总结
查看>>
贝叶斯
查看>>
推荐系统总结
查看>>
relu与sigmod的比较
查看>>
交叉熵和均方差损失函数的比较(Cross-Entropy vs. Squared Error)
查看>>