Disabled Panel control loses viewstate

R

Robert Phillips

I have a Panel control containing a few TextBox controls. The Panel is
originally enabled, I enter data into the TextBox controls. When I submit,
the Panel is disabled during the PostBack and the TextBox controls render
greyed-out, and I can see the values in the TextBox controls....this is what
I expected.

I submit again, the Panel is enabled during the PostBack. All of the
TextBox controls within the Panel are now enabled, however, the values are
gone. This doesn't happen with a TextBox control outside of the Panel that
is also enabled/disabled.

Is this by design? I have a sample below I am testing with. I have a much
more complicated form I was going to do this with.

Also, this doesn't occur when the Visible property is used.


<%@ Page Language="C#" AutoEventWireup="True" %>


<script runat="server">

void Page_Load(Object sender, EventArgs e)
{
if (Check1.Checked)
{
Panel1.Enabled=false;
TestText3.Enabled=false;
}
else
{
Panel1.Enabled=true;
TestText3.Enabled=true;
}
}
</script>

<html>
<head>
</head>
<body>

<form runat="server" ID="Form1">

<asp:panel id="Panel1" runat="server" BackColor="gainsboro" Height="200px"
Width="300px">
Panel1: Here is some static content...
<asp:TextBox id="TestText" Width="100" Runat="server"></asp:TextBox>
<p/>
<asp:TextBox id="TestText2" Width="100" Runat="server"></asp:TextBox>
</asp:panel>

<p/><asp:TextBox Runat="server" ID="TestText3" Width="100"/>

<p/><asp:CheckBox id="Check1" Text="Disable Panel" runat="server" />
<p/><asp:Button Text="Refresh Panel" runat="server" ID="Button1" />

</form>

</body>
</html>
 
S

Scott Mitchell [MVP]

Robert, explicitly set the TextBox's in the Panel to disabled (i.e.,
Enabled = False). That is, change your server-side code to something like:
void Page_Load(Object sender, EventArgs e)
{
if (Check1.Checked)
{
Panel1.Enabled=false;
TestText.Enabled = false;
TestText2.Enabled = false;
TestText3.Enabled=false;
}
else
{
Panel1.Enabled=true;
TestText.Enabled = true;
TestText2.Enabled = true;
TestText3.Enabled=true;
}
}

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
R

Robert Phillips

yep, i figured that out later. unfortunately my real form has a lot
more stuff on it. i'm going the route of looping through the panel's
controls collection.

would you consider this an asp.net deficiency?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top