Disabled Panel control loses values

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>
 
J

Jim Corey

Robert,

Are you posting back as a result of checking Check1?

If so you might not have the correct value for Check1
during Page_Load -- you would want to enable/disable controls in the
Check1 event that triggers the post back.

Jim
 
M

Martin Dechev

Hi, Robert Phillips,

The cause for the behavior is the browser not posting the values in the
<input type=text disabled> controls. You can try enabling these on the
client side in the onsubmit event or, alternatively, preserve the values on
the server-side (in the Session or in the ViewState maybe) before you set
Enable = false.

Hope this helps
Martin
 
G

Guest

I do somthing like that with tables when I hide and show rows. An important thing to remember is that at the end of the Asp.NET rainbow you still come down to HTML markup. If the browser doesent paint it, it doesnt exist anymore to the client or the postback event UNLESS you stuff those values in some kind of server or local storage like cookies, or sessions, or state server. What I do, if your not on a cluster, is when an event is fired that removes a parent control from the presentation, stuff the values into session lik

public void HidePanel(

Session["saveChildControl1"] = objChildControl1.Text; // assuming it's a textbo
Session["saveChildControl2"] = objChildControl2.Text
Session["hasSaveControls"] = "true"
objPanel.Visible = false



then to reload i

public void ShowPanel(

objPanel.Visible = true;
// remember to check if it was hidden as you will bomb on referencing non existant stuf
if (Session["hasSavedControls"] != null

objChildControl1.Text = Session["saveChildControl1"].Text.ToString()
objChildControl2.Text = Session["saveChildControl2"].Text.ToString()

}
 
R

Robert Phillips

i know what you're saying, but if you look at the html generated in my
sample, all of the controls are being generated, but it's intersting that
the div (for the panel) is set to disabled, but the contained TextBox
controls are not (the TextBox control outside the panel does have its html
disabled property set).

the disabled TextBox that isn't in the panel maintains its value fine,
whether or not it is in the posted data.

so, its like asp.net isn't painting it correctly...thus it see it as enabled
during the postback, but no value is avialable in the posted data.

Harold said:
I do somthing like that with tables when I hide and show rows. An
important thing to remember is that at the end of the Asp.NET rainbow you
still come down to HTML markup. If the browser doesent paint it, it doesnt
exist anymore to the client or the postback event UNLESS you stuff those
values in some kind of server or local storage like cookies, or sessions, or
state server. What I do, if your not on a cluster, is when an event is fired
that removes a parent control from the presentation, stuff the values into
session like
public void HidePanel()
{
Session["saveChildControl1"] = objChildControl1.Text; // assuming it's a textbox
Session["saveChildControl2"] = objChildControl2.Text;
Session["hasSaveControls"] = "true";
objPanel.Visible = false;

}

then to reload it

public void ShowPanel()
{
objPanel.Visible = true;
// remember to check if it was hidden as you will bomb on referencing non existant stuff
if (Session["hasSavedControls"] != null)
{
objChildControl1.Text = Session["saveChildControl1"].Text.ToString();
objChildControl2.Text = Session["saveChildControl2"].Text.ToString();
}
}
 
M

Martin Dechev

Hi, Harold,

The problem is with Enabled = {true | false} for the container control.

When you set Visible = {true | false} the child controls keep their values,
so there is no need for this overhead.

Greetings
Martin
Harold said:
I do somthing like that with tables when I hide and show rows. An
important thing to remember is that at the end of the Asp.NET rainbow you
still come down to HTML markup. If the browser doesent paint it, it doesnt
exist anymore to the client or the postback event UNLESS you stuff those
values in some kind of server or local storage like cookies, or sessions, or
state server. What I do, if your not on a cluster, is when an event is fired
that removes a parent control from the presentation, stuff the values into
session like
public void HidePanel()
{
Session["saveChildControl1"] = objChildControl1.Text; // assuming it's a textbox
Session["saveChildControl2"] = objChildControl2.Text;
Session["hasSaveControls"] = "true";
objPanel.Visible = false;

}

then to reload it

public void ShowPanel()
{
objPanel.Visible = true;
// remember to check if it was hidden as you will bomb on referencing non existant stuff
if (Session["hasSavedControls"] != null)
{
objChildControl1.Text = Session["saveChildControl1"].Text.ToString();
objChildControl2.Text = Session["saveChildControl2"].Text.ToString();
}
}
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top