当“回发”时,HiddenField Value 属性返回为空。使用 AJAX 更新面板
我已经处理这个问题几个小时了,但找不到解决方案。 现在我在一个 Web 应用程序中工作,我的第一个问题是我想动态创建一个 XML...然后我意识到在回发后,Xml 正在重置。然后我只是想,“好吧,让我们创建一个 HiddenField 并继续将节点作为字符串存储在 HiddenField 值属性中,所以最后我只需创建一个新的 XmlElement,使用 HiddenField.Value 的 InnerXml 创建一个文档片段并附加Fragmentto the XmlElement"...但是 HiddenField.Value 也会重置每次单击按钮...我刚刚用标签测试了此方法并且它有效...
基本上我有一个页面分为两部分AJAXControlToolKit TabContainer 控件。第一个表单用于用户主要数据,而第二个选项卡的表单旨在根据用户的需要多次填写表单,因为它用于存储家庭成员。所以过程是填充一个家庭成员数据,单击按钮并将其存储在 HiddenField.Value 中,填充第二个家庭成员数据,然后再次单击添加新的家庭成员并连接到 HiddenField.Value...但是我意识到,第一次单击“加载页面”方法后,HiddenField.Value 再次为空...
也许不是那么重要,但在 UpdatePanel 上只有按钮和列表框,用于显示用户所有家庭成员的一些数据已保存,因此唯一的对象单击刷新的是列表框。
正如我刚才所说,如果使用 HiddenField.Value 我使用 Label.Text,一切正常...
<%@ Page Language="c#" MasterPageFile="/Plantilla.master" AutoEventWireup="true" Inherits="alta_personal_interno" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<asp:Content runat="server" id="contentDefault" ContentPlaceHolderId="ContentPlaceHolderPagina">
<div align="left">
<table style="width: 100%; background-color: maroon">
<tbody>
<tr>
<td>
<span id="ctl00_ContentPlaceHolder1_lblTitulo" class="EtiquetaMedianoBlanco">
<asp:Label ID="lblTituloPExt" runat="server" Text="Alta de Personal Interno" />
</span>
</td>
</tr>
</tbody>
</table>
</div>
<cc1:TabContainer runat="server">
<cc1:TabPanel runat="server" HeaderText="Titular">
<ContentTemplate>
<--!Code with Form Elements-->
<asp:Button ID="btnAgregarNvo" runat="server" Text="Guardar" onclick="btnAgregarNvo_Click"/>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel runat="server" HeaderText="Familia">
<ContentTemplate>
<asp:HiddenField runat="server" id="hidFamiliares"></asp:HiddenField>
<!--Code with Form Elements-->
<asp:UpdatePanel runat="server" id="upFamiliares">
<ContentTemplate>
<asp:Button ID="btnAgregarFamiliar" runat="server" Text="Agregar" onclick="btnAgregarFamiliar_Click"/>
<asp:Button ID="btnQuitarFamiliar" runat="server" Text="Quitar" onclick="btnQuitarFamiliar_Click"/>
<br/>
<asp:ListBox runat="server" ID="lbFamiliares"/>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
//------------------------------CODE BEHIND------------------------------------
private XmlDocument objXML;
protected void Page_Load(object sender, EventArgs e){
objXML = new XmlDocument();}
protected void btnAgregarFamiliar_Click(object sender, EventArgs e){
XmlElement xmlFamiliar = objXML.CreateElement("familiar");
AddAttribute("nombre",txtNombreF.Text,xmlFamiliar);
AddAttribute("apaterno",txtApF.Text,xmlFamiliar);
hidFamiliares.Value+=xmlFamiliar.InnerXml;}
private void AddAttribute(string name, string val, XmlElement parent){
XmlAttribute at = objXML.CreateAttribute(name);
at.Value = val;
parent.SetAttributeNode(at);}
I have been working with this issue for hours and cant find a solution.
Now Im working in a web Application and my first problem was that I wanted to create an XML dynamically...Then I realized that after a postback, the Xml was resseting. Then I just though, "ok, lets create a HiddenField and keep storing nodes as string in the HiddenField value property, so at the end I just create a new XmlElement, create a document fragment with an InnerXml of the HiddenField.Value and attach the fragmentto the XmlElement"...But HiddenField.Value also RESETS every click on a button... I just tested this method with a label and IT WORKS...
Basically I have a page divided in two with the AJAXControlToolKit TabContainer Control. The first form is for the user main data while the second tab has a form that is intented to fill the form as many times as the user wants because its for storing family members. So the process is to fill a family member data, click the button and store it in the HiddenField.Value, fill the second family member data and click again to add a the new family member and concatenate to the HiddenField.Value...But I realized that after the first click on the Load Page method, the HiddenField.Value is again empty...
Maybe not that important but On the UpdatePanel there is just The button and a listbox to show some data of all the family members the user has saved, so the only object that refreshes on the click is the listbox.
As I just said, if instead using a HiddenField.Value I use a Label.Text, Everything Works...
<%@ Page Language="c#" MasterPageFile="/Plantilla.master" AutoEventWireup="true" Inherits="alta_personal_interno" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<asp:Content runat="server" id="contentDefault" ContentPlaceHolderId="ContentPlaceHolderPagina">
<div align="left">
<table style="width: 100%; background-color: maroon">
<tbody>
<tr>
<td>
<span id="ctl00_ContentPlaceHolder1_lblTitulo" class="EtiquetaMedianoBlanco">
<asp:Label ID="lblTituloPExt" runat="server" Text="Alta de Personal Interno" />
</span>
</td>
</tr>
</tbody>
</table>
</div>
<cc1:TabContainer runat="server">
<cc1:TabPanel runat="server" HeaderText="Titular">
<ContentTemplate>
<--!Code with Form Elements-->
<asp:Button ID="btnAgregarNvo" runat="server" Text="Guardar" onclick="btnAgregarNvo_Click"/>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel runat="server" HeaderText="Familia">
<ContentTemplate>
<asp:HiddenField runat="server" id="hidFamiliares"></asp:HiddenField>
<!--Code with Form Elements-->
<asp:UpdatePanel runat="server" id="upFamiliares">
<ContentTemplate>
<asp:Button ID="btnAgregarFamiliar" runat="server" Text="Agregar" onclick="btnAgregarFamiliar_Click"/>
<asp:Button ID="btnQuitarFamiliar" runat="server" Text="Quitar" onclick="btnQuitarFamiliar_Click"/>
<br/>
<asp:ListBox runat="server" ID="lbFamiliares"/>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
//------------------------------CODE BEHIND------------------------------------
private XmlDocument objXML;
protected void Page_Load(object sender, EventArgs e){
objXML = new XmlDocument();}
protected void btnAgregarFamiliar_Click(object sender, EventArgs e){
XmlElement xmlFamiliar = objXML.CreateElement("familiar");
AddAttribute("nombre",txtNombreF.Text,xmlFamiliar);
AddAttribute("apaterno",txtApF.Text,xmlFamiliar);
hidFamiliares.Value+=xmlFamiliar.InnerXml;}
private void AddAttribute(string name, string val, XmlElement parent){
XmlAttribute at = objXML.CreateAttribute(name);
at.Value = val;
parent.SetAttributeNode(at);}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是什么原因造成的,您可能需要尝试更简单的方案来排除故障。
如果您的方法适用于
Label
控件,您是否可以始终使用不可见控件而不是HiddenField
?I'm unsure what's causing it, you might have to try a simpler scenario to troubleshoot.
If your method works with a
Label
control, you could always use an invisible one instead of aHiddenField
?有点猜测。
但从代码的外观来看,当您在
UpdatePanel
中提交按钮事件时,它会重新加载面板,但不会重新加载HiddenField
的值,因此当UpdatePanel
内容加载时,它仍然将HiddenField
视为空。将
HiddenField
包装在同一个UpdatePanel
中可能会起作用。或者您可以尝试将其包装在它自己的UpdatePanel
中,然后在按钮事件中调用UpdatePanel.Update()
,确保UpdateMode
设置为面板上的'Conditional'
。Bit of a guess.
But by the looks of the code, when you submit a button event within the
UpdatePanel
, it reloads the panel, but it doesn't reload the value of yourHiddenField
, so when theUpdatePanel
content loads, it still sees theHiddenField
as empty.Wrapping the
HiddenField
in the sameUpdatePanel
might work. Or you could try wrapping it in it's ownUpdatePanel
then callUpdatePanel.Update()
within your button event, making sure theUpdateMode
is set to'Conditional'
on the panel.