Search code examples
jqueryhidden-field

HiddenField value doesn't update in row updating


Following is my page coding

<div class="alert alert-danger alert-error" style="display:none" id="alertErrorMessage">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>Error! </strong><% =HiddenField1.Value %>
    </div>

    <asp:HiddenField ID="HiddenField1" runat="server" Value="init HF value"/>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="gridViewAll" runat="server" CssClass="table table-striped table-hover table-bordered"
                            AutoGenerateColumns="False" DataKeyNames="ImageId"
                            OnRowEditing="gridViewStage_RowEditing"
                            OnRowCancelingEdit="gridViewStage_RowCancelingEdit"
                            OnRowUpdating="gridViewStage_RowUpdating">
.....
 </ContentTemplate>
                </asp:UpdatePanel>

Then I update hidden field value on below coding and shows the alert. But still it shows 'init HF value' in alert box. But it should show 'New HF Value' instead of that. What may be the cause behind this?

protected void gridViewStage_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
StringBuilder sb = new StringBuilder();
            sb.Append(@"<script>");
            sb.Append("$('#HiddenField1').val('New HF Value');");
            sb.Append("$('#alertErrorMessage').css('display', 'block');");
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ErrorMessageAlert", sb.ToString(), false);
}

Solution

  • I solved it by adding my alert box in to an UpdatePanel

    <asp:UpdatePanel ID="UpdatePanel7" runat="server">
            <ContentTemplate>
                <div class="alert alert-danger alert-error" style="display: none" id="alertErrorMessage">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                    <strong>Error! </strong><% =hiddenFieldErrorMessage.Value %>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>