ASP.NET UpdatePanel 和 Javascript __dopostback [英] ASP.NET UpdatePanel and Javascript __dopostback
问题描述
我正在像这样从 javascript 调用部分回发:
I'm calling a partial postback from javascript like so:
function GetPolicyClick()
{"__dopostback('UpdatePanel1', 'PostCall')";}
它完成了我需要的 1/2.它确实调用了部分回发,仅用于我的 UpdatePanel.
It does 1/2 of what I need it to. It does call a partial postback, just for my UpdatePanel.
现在是棘手的部分.我正在尝试(以某种方式)在后面的代码中引用 __dopostback 的第二个参数.这不起作用:
Now the tricky part. I'm trying (somehow) to reference the second argument of __dopostback in my code behind. This does not work:
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.
当然,我尝试做的事情可能是完全错误的(就像我尝试用 ASP 做的其他事情一样).我怀疑我的代码隐藏是从页面而不是面板中获取事件参数,但我真的不知道,有什么想法吗?
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?
推荐答案
如果您想在 _EVENTARGUMENT 中放入一些值,您应该在 发送表单之前使用 javascript 执行此操作_doPostBack('UpdatePanel1','') 因为 __EVENTTARGET 是隐藏字段,在您的 html 文档中它看起来像这样:
If you want to put some value inside _EVENTARGUMENT you 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:
<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', '');
}
如果你使用 jQuery,它会更短:
If you use jQuery it would be shorter:
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', '');
}
在第一种情况下,您可以在服务器端获得 __EVENTARGUMENT:
In first scenario you are able to get __EVENTARGUMENT in server side:
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.
这篇关于ASP.NET UpdatePanel 和 Javascript __dopostback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!