隐藏字段在回发时失去其价值
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
调用 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.
您应该考虑使用 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
通常,当我遇到这样的问题时(回发后我丢失了数据),我最终发现我忘记将函数放在
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)
如果删除 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.
过去,我曾有过一些相当复杂的网页,大量使用了 Ajax。我喜欢 HiddenFields 因为其易用性和智能感知。在最近的一个案例中,我发现,像你一样,我的 HiddenField 在回发时失去了它的价值。所以我作弊了一点……
我使用 HiddenFields Init 事件和 Unload 事件来初始化并捕获其在 Session 中的值。更好的解决方案是修复其丢失状态的原因或切换到会话。我使用该值来操作许多不同区域中的 Multiview ActiveViewState 属性,并且不想重新连线或进行深入的调试跟踪。时间是一个问题。
因此,页面第一次加载时,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.
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
几周前我也遇到了同样的问题。我使用文本框和 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!