简体   繁体   中英

ASP.Net get ScriptManager.RegisterClientScriptBlock return on UpdatePanel hiddenField

I am trying to obtain the return of a Javascript function that I call ont server side using ScriptManager.RegisterClientScriptBlock .

Currently, my javascript function save the value in a HiddenField that is contained in an UpdatePanel. When I call the method , I run the ScriptManager.RegisterClientScriptBlock and after that get the value of the HiddenField , but always returns me the value of the previous call. Here I show the code:

My User Control ASPX Side:

<script>

var num = 0;

function getReturn() {

    num = num + 1;
    var hr= document.getElementById('<%= hdf.ClientID %>');
    hRetorno.value = num;

}
</script>

...

<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:HiddenField ID="hdf" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

My User Control Server Side Code:

    public String GetReturn()
    {
        return MyControl.jsGetReturn(this.Page, this.hdf);
    }

    private static String jsGetReturn(Page page, HiddenField hid)
    {
        ScriptManager.RegisterStartupScript(page, page.GetType(), "key", "getReturn();", true);

        return hid.Value;

    }

Index Page ASPX Side:

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <div style="width:720px; height:480px;">
            <uc1:MyControl runat="server" id="MyControl1"/>
        </div>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>

                <asp:Button ID="btn" runat="server" OnClick="btn_Click" />
                <asp:TextBox ID="txt" runat="server" ></asp:TextBox>

            </ContentTemplate>
        </asp:UpdatePanel>

Index Page Server Side Code:

    protected void btn_Click(object sender, EventArgs e)
    {
        txt.Text = MyControl1.GetReturn();
    }

You can not do these things simultaneously.

  1. Set value of hidden field from server side code using javascript via RegisterClientScriptBlock or RegisterStartupScript .
  2. And retrieve that hidden filed value in server side code at the same time.

This is because, (changed) hidden field value is available to server-side code only when there is postback, when you set the value from server-side, there is no postback happening, that is why you are always getting previous value , ie that old value which is posted back to page.

EDIT

When you invoke RegisterClientScriptBlock or RegisterStartupScript , it won't make JS call instantly, rather it just append a javascript call before or after <form.. tag based on what you used and they are called on document load, so that means in jsGetReturn when you call RegisterStartupScript , it will set value of hidden field in document load, - hid.Value will not have updated value, as it is yet to be incremented via document load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM