Question

I read a couple hidden field related questions/answers here and there but none of them really solve my doubts. I don't think the problem is directly related to viewstate or not binding my data early enough since my other controls retain their values which I bind at the same time as i did with the hiddenfield values.

I am generating a table using asp repeater and binding the table values using server side tags like following: Note that I am binding <%# Eval("ProgramID") %> to hiddenfield values and to label text field.

<asp:Repeater ID="RpPrograms" runat="server" EnableViewState="True">
    <HeaderTemplate>
        <table id="tablePrograms" class="display">
            <thead>
                <tr>
                    <th>Checkbox</th>
                    <th>Program</th>
                    <th>Company Name</th>
                </tr>
            </thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <div id='<%# "ProgramCbxCol_" + Eval("ProgramID").ToString()%>'>
                    <asp:checkbox runat="server" ID="cbxProgram" />
                    <asp:HiddenField ID="hdnProgramID" runat="server" Value='<%# Eval("ProgramID")%>' />
                    <asp:HiddenField ID="hdnProgramTagName" runat="server" Value='<%# Eval("Program") + "(" + Eval("CompanyName") + ")"%>'/>
                </div>
            </td>
            <td><asp:label runat="server" ID="ProgramName" Text='<%# Eval("Program") + "(" + Eval("ProgramID").ToString() + ")"%>'></asp:label></td>
            <td><%#Eval("CompanyName")%></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

When the page first loaded, all my values including the hiddenfield values are properly binded. However, after the page postback (by clicking a save button I am saving the checked checkboxes by getting the value from the hiddenfields), all the hiddenfield values are gone. Nevertheless, other data that I binded using the server side tags is still available after postback. (Company Names, Programs are all available.)

I did a test by binding the ProgramID to the text field of an asp label. It turned out that, unlike the hiddenfield ProgramID, this ProgramID did persist after the postback. This proved that the problem is not related to binding data too late. Might not be related to viewstate as well, but i am not sure. I think is is something related to asp hiddenfield in particular.

Can someone solve this mystery? Why are hiddenfield values not persist after postback?

Was it helpful?

Solution

I tried your code but I can't seem to get the same behavior. For me the HiddenFields are persistent, just as you want. I think you need to include to supply some of your code behind code to be able to see if there is something strange going on there.

I'll Supply my test code so you can have a look:

My view:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Webtest.WebForm1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="RpPrograms" runat="server" EnableViewState="True">
        <HeaderTemplate>
            <table id="tablePrograms" class="display">
                <thead>
                    <tr>
                        <th>Checkbox</th>
                        <th>Program</th>
                        <th>Company Name</th>
                    </tr>
                </thead>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <div id='<%# "ProgramCbxCol_" + Eval("ProgramID").ToString()%>'>
                        <asp:checkbox runat="server" ID="cbxProgram" />
                        <asp:HiddenField ID="hdnProgramID" runat="server" Value='<%# Eval("ProgramID")%>' />
                        <asp:HiddenField ID="hdnProgramTagName" runat="server" Value='<%# Eval("Program") + "(" + Eval("CompanyName") + ")"%>'/>
                    </div>
                </td>
                <td><asp:label runat="server" ID="ProgramName" Text='<%# Eval("Program") + "(" + Eval("ProgramID").ToString() + ")"%>'></asp:label></td>
                <td><%#Eval("CompanyName")%></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <asp:Button runat="server" Text ="Save"/>
    </div>
    </form>
</body>
</html>

My codebehind:

using System;
using System.Collections.Generic;

namespace Webtest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private List<ProgramData> _programData = new List<ProgramData>
        {
            new ProgramData {ProgramID = 1, Program = "Program abc", CompanyName = "Company 3434"},
            new ProgramData {ProgramID = 2, Program = "Program def", CompanyName = "Company 3qa2434"},
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RpPrograms.DataSource = _programData;
                RpPrograms.DataBind();
            }
        }
    }

    public class ProgramData
    {
        public int ProgramID { get; set; }
        public string Program { get; set; }
        public string CompanyName { get; set; }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top