从Javascript触发回发 [英] Firing Postback from Javascript

查看:52
本文介绍了从Javascript触发回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道这个问题已经问了很多,并且我已经在网上找到了很多有关此的信息.不幸的是,似乎所有这些都不适合我.

Ok, I know this question has been asked a lot and I've found plenty of information on the Web about this. Unfortunately, none of it really seems to work for me.

基本上,我运行了一些JavaScript来响应用户事件.JavaScript获得了一些信息(一个int信息),然后我想将其返回给服务器.

Basically, I have some JavaScript running in response to a user event. The JavaScript obtains a bit of information (an int) and then I want to get that back to the server.

第一种方法是使用 Page.ClientScript.GetPostBackEventReference 来获得对 __ DoPostBack()的有效调用,如

The first approach was to use Page.ClientScript.GetPostBackEventReference to get a valid call to __DoPostBack() as described here.

我使用此方法从按钮触发回发.除了我收到错误消息外,这似乎是可行的:

I used this method to trigger a postback from a button. This seemed to be working except I get the error:

无效的回发或回调参数.使用in配置或页面中的<%@页面EnableEventValidation ="true"%>启用事件验证.为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它们的服务器控件.如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法来注册回发或回调数据以进行验证.

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

接下来,我尝试使用HiddenField的 ValueChanged 事件处理程序.我编写了处理程序,但是更改隐藏字段的值不会触发该事件.仍然需要以某种方式触发某种形式的回发.

Next, I tried to use a HiddenField's ValueChanged event handler. I wrote my handler, but changing the value of the hidden field does not trigger the event. Some form of postback still needs to be triggered somehow.

我真的不明白为什么我遇到这么多麻烦.是否有更直接的方式来做我需要的事情?

I don't really understand why I'm having so much trouble with this. Is there a more straight forward way of doing what I need?

推荐答案

哇,没有答案.谁会想到这太晦涩.

Wow, no answers. Who would've thought this is so obscure.

无论如何,这对我有用.我刚刚在服务器窗体处理程序中为 ValueChanged 事件添加了一个ASP.NET HiddenField.

Anyway, here's what worked for me. I just added an ASP.NET HiddenField to my form with a server-side handler for the ValueChanged event.

<asp:HiddenField ID="MyHidden" runat="server" OnValueChanged="MyHidden_ValueChanged" />

然后,为响应用户操作,我的JavaScript设置了此隐藏字段的值.

Then, in response to a user action, my JavaScript sets the value of this hidden field.

$('#<%= MyHidden.ClientID %>').val(myData);

最后,为了触发隐藏字段的 ValueChanged 事件,我使用以下代码启动回发. GetPostBackEventReference()返回一个JavaScript语句,它将替换服务器端标记.

And, finally, in order to trigger the hidden field's ValueChanged event, I initiate a postback with the following code. GetPostBackEventReference() returns a JavaScript statement, which will replace the server side markup.

<%= Page.ClientScript.GetPostBackEventReference(MyHidden, String.Empty) %>;

似乎工作正常.发生回发,并调用了我的 ValueChanged 处理程序.该处理程序确定我的隐藏字段的值并执行所需的操作.

And it seems to work just fine. A postback occurs, and my ValueChanged handler is called. That handler determines the value of my hidden field and performs the required actions.

这篇关于从Javascript触发回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆