ASP.NET UpdatePanel 和 Javascript __dopostback
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6361680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET UpdatePanel and Javascript __dopostback
提问by r.piesnikowski
I'm calling a partial postback from javascript like so:
我正在从 javascript 调用部分回发,如下所示:
function GetPolicyClick()
{"__dopostback('UpdatePanel1', 'PostCall')";}
It does 1/2 of what I need it to. It doescall a partial postback, just for my UpdatePanel.
它完成了我需要的 1/2。它确实调用了部分回发,仅适用于我的 UpdatePanel。
Now the tricky part. I'm trying (somehow) to reference the second argument of __dopostback in my code behind. Thisdoes not work:
现在是棘手的部分。我正在尝试(以某种方式)在后面的代码中引用 __dopostback 的第二个参数。这不起作用:
Private Sub UpdatePanel1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdatePanel1.Load
Dim MyArg As String = Request("__EVENTARGUMENT")
End Sub
I just get an empty string.
我只是得到一个空字符串。
Of course, what I'm trying to do might be completely wrong (as with everything else I try to do with ASP). I'm suspecting that my codebehind is grabbing the event argument from the page instead of the panel, but I really don't know, Any ideas?
当然,我正在尝试做的事情可能是完全错误的(就像我尝试用 ASP 做的其他事情一样)。我怀疑我的代码隐藏是从页面而不是面板中获取事件参数,但我真的不知道,有什么想法吗?
回答by r.piesnikowski
If you want to put some value inside _EVENTARGUMENTyou should do this with javascript before sending form by _doPostBack('UpdatePanel1','')because __EVENTTARGET is hidden field and in your html document it looks like this:
如果您想在_ EVENTARGUMENT 中放入一些值,您应该在发送表单之前使用 javascript 执行此操作_doPostBack('UpdatePanel1','')因为 __EVENTTARGET 是隐藏字段,在您的 html 文档中它看起来像这样:
<input type="hidden" value="" id="__EVENTARGUMENT" name="__EVENTARGUMENT">
I recommend you to do something like this:
我建议你做这样的事情:
function setArgAndPostBack() {
var arg = document.getElementById('__EVENTARGUMENT');
var arg = document.getElementById("__EVENTARGUMENT");
arg.value = 'something you want to put to server';
__doPostBack('UpdatePanel1', '');
}
If you use jQuery it would be shorter:
如果你使用 jQuery,它会更短:
function setArgAndPostBack() {
$("#__EVENTARGUMENT").val('something you want to put to server');
__doPostBack('UpdatePanel1', '');
}
If it doesn't work I would like to suggest you to put one hidden field inside Update panel:
如果它不起作用,我建议您在更新面板中放置一个隐藏字段:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:HiddenField ID="hdnData" value="" runat="server" />
<!-- your content goes here -->
</ContentTemplate>
</asp:UpdatePanel>
And then do the same work like above:
然后像上面一样做同样的工作:
function setArgAndPostBack() {
//Here hidden field is filled with your data
$("#<%=hdnData.ClientID%>").val('something you want to put to server');
__doPostBack('UpdatePanel1', '');
}
In first scenario you are able to get __EVENTARGUMENT in server side:
在第一种情况下,您可以在服务器端获得 __EVENTARGUMENT:
String args = Request["__EVENTARGUMENT"];
If first scenario doesn't work you can use something like that:
如果第一种情况不起作用,您可以使用类似的方法:
String args = hdnData.Value;//This works even in Page_Load function.