HiddenField值不会在行更新中更新

时间:2014-11-16 03:37:41

标签: jquery hidden-field

以下是我的页面编码

<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>

然后我在下面的编码中更新隐藏的字段值并显示警报。 但它仍然在警报框中显示“初始HF值”。但它应该显示'新的HF值'而不是那个。 这背后可能是什么原因?

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

2 个答案:

答案 0 :(得分:0)

也许这篇相关文章可以帮助您或提供进一步的指导:

Asp .net hidden field can't set value with jquery 祝你好运!

答案 1 :(得分:0)

我通过将我的警告框添加到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>