The HiddenField Value property returns to empty when "postback" using the AJAX UpdatePanel

I have been working with this problem for hours and cannot find a solution. Now I am working in a web application, and my first problem was that I wanted to dynamically create XML ... Then I realized that after the postback, Xml was being rewritten. Then I just "okay, let's create a HiddenField and save the nodes as a string in the HiddenField property, so in the end I just create a new XmlElement, create a document fragment from the InnerXml of the HiddenField.Value file and attach the XmlElement fragment" ... But HiddenField.Value also RESETS pushes a button every time ... I just tested this method with a tag and IT WORKS ...

Basically, I have a page divided into two using the TabContainer AJAXControlToolKit control. The first form is for the user's main data, and the second tab has a form that is designed to fill out the form as many times as the user wants, because it is designed to store family members. So the process is to fill in the data on family members, click on the button and save it in HiddenField.Value, fill in the data of the second family member and click again to add a new family member and combine with HiddenField.Value ... But I I realized that after the first click on the "Load Page" method, HiddenField.Value is again empty ...

Perhaps this is not so important, but on UpdatePanel there is only a button and a list to show some data of all family members that the user has saved, so the only object that is updated when clicked is a list.

As I said, if instead of using HiddenField.Value I use 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);} 
+4
source share
2 answers

I'm not sure what caused this, you might have to try a simpler troubleshooting script.

If your method works with a Label control, can you always use the invisible instead of the HiddenField ?

 <asp:Label runat="server" id="hidFamiliares" style="display:none;"/> 
+7
source

A guess.

But in the look of the code, when you send the button event to the UpdatePanel , it reloads the panel, but does not reload the value of your HiddenField , so when loading the UpdatePanel content, it still sees the HiddenField as empty.

The HiddenField wrapper in the same UpdatePanel may work. Or you can try wrapping it in your own UpdatePanel and then calling UpdatePanel.Update() on your button event, making sure the UpdateMode panel is set to 'Conditional' .

+1
source

Source: https://habr.com/ru/post/1391688/


All Articles