隐藏字段在回发时失去其价值

发布于 2024-09-04 14:49:43 字数 2244 浏览 2 评论 0原文

我有一个 ascx 页面,其中使用隐藏字段来存储下拉框的值,因为它是使用谷歌地址查找器生成的。我的问题是,当我尝试将值直接存储在隐藏字段中时:

hfDdlVerifyID.Value = ddlVerifySS.SelectedValue;

单击按钮时,该值会被存储,但在回发时会再次丢失。然而,如果我尝试使用 Scriptmanager 来执行此操作,则不会存储任何内容。

getBuild.AppendLine("$get('" + hfDdlVerifyID.ClientID + "').value = $get('" + ddlVerifySS.ClientID + ").value;");

ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "storeHidden", getBuild.ToString(), true);
//  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "storeHidden", getBuild.ToString(), true);
string test = hfDdlVerifyID.Value.ToString();

ascx 页面是:

<asp:UpdatePanel ID = ddlUpdate runat="server">
    <ContentTemplate>
         <asp:Panel ID="pVerify" runat="server">
             <br />
             <fieldset>
                 <legend>
                     <asp:Literal ID="lVerify" runat="server" />
                 </legend>
                 <asp:CheckBox
                     ID      ="cbVerify"
                     runat   ="server"
                     Text    ="Use the value from the following list, (Uncheck to accept address as it is)."
                     Checked ="true" />
                 <br />
                 <asp:DropDownList ID="ddlVerifySS" runat="server"
                     onselectedindexchanged="ddlVerifySS_SelectIndexChange" />
                 <asp:HiddenField id="hfDdlVerifyID"
                     runat   ="server" /> 
             </fieldset>
         </asp:Panel>  
     </ContentTemplate>
 </asp:UpdatePanel>

 <padrap:Button          ID          ="bVerify"
                         runat       ="server"
                         CssClass    ="btn"
                         OnClick     ="bVerify_Click"
                         Text        ="Verify Address" />

 <asp:Button             ID          ="btnSubSite" 
                         runat       ="server" 
                         text        ="Save"
                         CssCLass    ="btn" 
                         OnClick     ="save_btn_Click_subSite" 
                         onLoad="ddlVerify_Load" />

I have a ascx page where I am using a hidden field to store the value of a the drop down box as it is generated using a google address finder. My problem is that when I try to store the value directly in the hidden field:

hfDdlVerifyID.Value = ddlVerifySS.SelectedValue;

in the event of a button click, the value is stored but on postback is lost again. Whereas, if i try to use Scriptmanager to do it, nothing is stored.

getBuild.AppendLine("$get('" + hfDdlVerifyID.ClientID + "').value = $get('" + ddlVerifySS.ClientID + ").value;");

ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "storeHidden", getBuild.ToString(), true);
//  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "storeHidden", getBuild.ToString(), true);
string test = hfDdlVerifyID.Value.ToString();

The ascx page is :

<asp:UpdatePanel ID = ddlUpdate runat="server">
    <ContentTemplate>
         <asp:Panel ID="pVerify" runat="server">
             <br />
             <fieldset>
                 <legend>
                     <asp:Literal ID="lVerify" runat="server" />
                 </legend>
                 <asp:CheckBox
                     ID      ="cbVerify"
                     runat   ="server"
                     Text    ="Use the value from the following list, (Uncheck to accept address as it is)."
                     Checked ="true" />
                 <br />
                 <asp:DropDownList ID="ddlVerifySS" runat="server"
                     onselectedindexchanged="ddlVerifySS_SelectIndexChange" />
                 <asp:HiddenField id="hfDdlVerifyID"
                     runat   ="server" /> 
             </fieldset>
         </asp:Panel>  
     </ContentTemplate>
 </asp:UpdatePanel>

 <padrap:Button          ID          ="bVerify"
                         runat       ="server"
                         CssClass    ="btn"
                         OnClick     ="bVerify_Click"
                         Text        ="Verify Address" />

 <asp:Button             ID          ="btnSubSite" 
                         runat       ="server" 
                         text        ="Save"
                         CssCLass    ="btn" 
                         OnClick     ="save_btn_Click_subSite" 
                         onLoad="ddlVerify_Load" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

像你 2024-09-11 14:49:43

调用 ajax 的 asp 按钮应该位于 ajax 面板中。将按钮放在更新面板中,然后重试。希望你能体会到隐藏字段的价值。

The asp button which invokes the ajax call should be in ajax panel. Put the button in the update panel then try again. I hope you will get the value of hidden field.

七度光 2024-09-11 14:49:43

您应该考虑使用 Viewstate。
希望这有帮助

http://msdn.microsoft.com/en-us/库/bb386448.aspx#Y2000

You should consider using Viewstate.
Hope this helps

http://msdn.microsoft.com/en-us/library/bb386448.aspx#Y2000

忆离笙 2024-09-11 14:49:43

通常,当我遇到这样的问题时(回发后我丢失了数据),我最终发现我忘记将函数放在 if(!IsPostback) 内的字段上重置数据的位置

Usualy, when I have problems like this one(after post back I lost the data) I end finding out that I forgot to put the function where I reset the data on the fields inside a if(!IsPostback)

小情绪 2024-09-11 14:49:43

如果删除 UpdatePanel,这段代码还能工作吗?

此外,您为 getBuild 调用发布的代码在对项目 ddlVerifySS.ClientID 的 $get 调用中缺少结束单引号。这是在这里输入的拼写错误,还是您的代码中的问题?如果是后者,这可以解释为什么在使用脚本管理器时看不到复制的值。

Does your this code work if you remove the UpdatePanel?

Also, the code you posted for the getBuild call is missing a closing single quote in the call to $get for the item ddlVerifySS.ClientID. Is this a typo entering this here, or a problem in your code? If it is the latter, this would explain why you don't see the value copied when you use the script manager.

夜访吸血鬼 2024-09-11 14:49:43

过去,我曾有过一些相当复杂的网页,大量使用了 Ajax。我喜欢 HiddenFields 因为其易用性和智能感知。在最近的一个案例中,我发现,像你一样,我的 HiddenField 在回发时失去了它的价值。所以我作弊了一点……
我使用 HiddenFields Init 事件和 Unload 事件来初始化并捕获其在 Session 中的值。更好的解决方案是修复其丢失状态的原因或切换到会话。我使用该值来操作许多不同区域中的 Multiview ActiveViewState 属性,并且不想重新连线或进行深入的调试跟踪。时间是一个问题。

Protected Sub HiddenFieldActiveView_Init(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Init
  If Not Page.IsPostBack Then
    HiddenFieldActiveView.Value = "0"
  Else
    HiddenFieldActiveView.Value = CStr(Session("ActiveViewIndex"))
  End If
End Sub

Protected Sub HiddenFieldActiveView_Unload(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Unload
    Session.Add("ActiveViewIndex", HiddenFieldActiveView.Value)
End Sub

因此,页面第一次加载时,HiddenField 被初始化为 0,然后在后续初始化时,它会从 Session 加载该值。

这可能不是最漂亮或最聪明的解决方案。但这对我的情况有用。

〜伊恩

In the past I have had some pretty complex webpages that heavily utilized Ajax. I like HiddenFields because of the ease of use and intellisense. In one case recently I found that, like you my HiddenField was losing it value on postbacks. So I cheated a little…
I used the HiddenFields Init event and Unload event to initialize and capture its value in Session. A better solution would be to either fix why it was losing state or switch to Session. I was using the value to manipulate a Multiview ActiveViewState property in a bunch of different areas and did not want to have to rewire or do an in-depth debug trace. Time was an issue.

Protected Sub HiddenFieldActiveView_Init(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Init
  If Not Page.IsPostBack Then
    HiddenFieldActiveView.Value = "0"
  Else
    HiddenFieldActiveView.Value = CStr(Session("ActiveViewIndex"))
  End If
End Sub

Protected Sub HiddenFieldActiveView_Unload(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Unload
    Session.Add("ActiveViewIndex", HiddenFieldActiveView.Value)
End Sub

So the firsttime the page loads the HiddenField is initialized with the value of 0, then on subsequent initializations it loads the value from Session.

This may not be the prettiest or smartest solution. But it worked for my situation.

~Ian

清风不识月 2024-09-11 14:49:43

几周前我也遇到了同样的问题。我使用文本框和 CSS 解决了这个问题。尝试使用 asp:TextBox 和 CSS 属性 display: none;

这只是我的建议,它对我有帮助。

希望对您有帮助!

I had the same problem few weeks ago. I solved it using textbox and CSS. Try to use asp:TextBox and CSS property display: none;

This is only my suggestion and it helped me.

Hope it will help you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文