Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HiddenField Value property back to empty when "postback" using AJAX UpdatePanel

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);}
like image 725
Symphonicmind Avatar asked Feb 22 '23 19:02

Symphonicmind


1 Answers

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 a HiddenField?

<asp:Label runat="server" id="hidFamiliares" style="display:none;"/>
like image 71
Brissles Avatar answered Feb 24 '23 08:02

Brissles