ASP.NET UpdatePanel and Javascript __dopostback

2019-03-20 12:48发布

I'm calling a partial postback from javascript like so:

function GetPolicyClick()
   {"__dopostback('UpdatePanel1', 'PostCall')";}

It does 1/2 of what I need it to. It does call a partial postback, just for my UpdatePanel.

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.

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?

1条回答
祖国的老花朵
2楼-- · 2019-03-20 13:38

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', '');
}

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', '');
}

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.
查看更多
登录 后发表回答