当“回发”时,HiddenField Value 属性返回为空。使用 AJAX 更新面板

发布于 2024-12-27 19:49:17 字数 3547 浏览 0 评论 0原文

我已经处理这个问题几个小时了,但找不到解决方案。 现在我在一个 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 技术交流群。

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

发布评论

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

评论(2

静赏你的温柔 2025-01-03 19:49:17

我不确定是什么原因造成的,您可能需要尝试更简单的方案来排除故障。

如果您的方法适用于 Label 控件,您是否可以始终使用不可见控件而不是 HiddenField

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

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;"/>
岁月无声 2025-01-03 19:49:17

有点猜测。

但从代码的外观来看,当您在 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 your HiddenField, so when the UpdatePanel content loads, it still sees the HiddenField as empty.

Wrapping the HiddenField in the same UpdatePanel might work. Or you could try wrapping it in it's own UpdatePanel then call UpdatePanel.Update() within your button event, making sure the UpdateMode is set to 'Conditional' on the panel.

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