panel's controls

  • Thread starter Alexandre Soares
  • Start date
A

Alexandre Soares

Hi,

If I make a control that derives from panel, how can I persist the state of
the controls that are inside the panel when it is not rendered (visible =
false)?

The reason for this is that I'm making a custom user control that derives
from the Panel class. In the render method, I render some stuff and then I
do base.render if the panel is to be visible. If it's to be hidden, I don't
render it, but when I render it again, the controls that I've put in my
control at design time are all reset.) I think I'm missing something in this
:)

Thank you
 
R

Raterus

Can you give me an example of what you have within your panel? I just tried a simple example of my own and it persisted attributes I set at design time after I toggled the panel visible/invisible.
--Michael

Here is what I did

Public Class Panel : Inherits System.web.ui.webcontrols.Panel
Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
MyBase.Render(Output)
End Sub
End Class

<%@ Register TagPrefix="tnc" Namespace="tnc" Assembly="tnc" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Intranet.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
</head>
<body>

<form id="Form1" method="post" runat="server">
<tnc:panel id="pnlTest" runat="server">
<asp:textbox id="txtTest" runat="server" text="Hello!"></asp:textbox>
</tnc:panel>
<asp:button id="btnText" runat="server" text="Toggle"></asp:button>
</form>

</body>
</html>

Private Sub btnText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnText.Click
If pnlTest.visible = False Then
pnlTest.Visible = True
Else
pnlTest.Visible = False
End If
End Sub
 
A

Alexandre Soares

I modified my example so that it fits what you did and you're right, the
info is persisted. However, in my original example, the button that
shows/hides the panel is rendered in the render method of the user control.
So in this render method, I always want to render the button, but not the
base panel. So if I do something like:

Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
button.Render(output)
if(panel must be visible) then
MyBase.Render(Output)
end if
End Sub

then the controls properties (like text) are not persisted. I don't know
why... :)


"Raterus" <[email protected]> a écrit dans le message de
Can you give me an example of what you have within your panel? I just tried
a simple example of my own and it persisted attributes I set at design time
after I toggled the panel visible/invisible.
--Michael

Here is what I did

Public Class Panel : Inherits System.web.ui.webcontrols.Panel
Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
MyBase.Render(Output)
End Sub
End Class

<%@ Register TagPrefix="tnc" Namespace="tnc" Assembly="tnc" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="Intranet.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
</head>
<body>

<form id="Form1" method="post" runat="server">
<tnc:panel id="pnlTest" runat="server">
<asp:textbox id="txtTest" runat="server" text="Hello!"></asp:textbox>
</tnc:panel>
<asp:button id="btnText" runat="server" text="Toggle"></asp:button>
</form>

</body>
</html>

Private Sub btnText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnText.Click
If pnlTest.visible = False Then
pnlTest.Visible = True
Else
pnlTest.Visible = False
End If
End Sub
 
A

Alexandre Soares

Actually my button is an anchor tag that fires a javascript postback (my
class implements IPostBackEventHandler to catch the postback event and
change the panel visibility state)

"Raterus" <[email protected]> a écrit dans le message de
Can you give me an example of what you have within your panel? I just tried
a simple example of my own and it persisted attributes I set at design time
after I toggled the panel visible/invisible.
--Michael

Here is what I did

Public Class Panel : Inherits System.web.ui.webcontrols.Panel
Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
MyBase.Render(Output)
End Sub
End Class

<%@ Register TagPrefix="tnc" Namespace="tnc" Assembly="tnc" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="Intranet.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
</head>
<body>

<form id="Form1" method="post" runat="server">
<tnc:panel id="pnlTest" runat="server">
<asp:textbox id="txtTest" runat="server" text="Hello!"></asp:textbox>
</tnc:panel>
<asp:button id="btnText" runat="server" text="Toggle"></asp:button>
</form>

</body>
</html>

Private Sub btnText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnText.Click
If pnlTest.visible = False Then
pnlTest.Visible = True
Else
pnlTest.Visible = False
End If
End Sub
 
R

Raterus

Do you realize that sub Render will never be called if the control.visible = false ?
So checking for visibility in Render is pointless, since it will always be true.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top