Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control.remove method

Status
Not open for further replies.

oakgrove

Programmer
Sep 22, 2002
51
US
Hi,
I have an aspx form page the querys a datasource into a datareader. Each data item is then displayed in a textbox control each with a label control to identify it. I have each pair of lable/textbox controls in a planel control.
Often there will be missing data so I need to remove the label and the space on the displayed form when that is the case.

I thought I could do this by removing the panel and the lable and textbox contained in it when the data item that is bound to the textbox is empty. I'm doing something wrong.

This is the line the I am trying
<b><code>
Page.Controls.Remove(&quot;plnPhone&quot;)
</code></b>
'plnPhone' is the id of the panel control I am trying to remove.

The error message is
<color red>Value of type 'String' cannot be converted to 'System.Web.UI.Control&quot;
</color>
Any help would be much appreciated.
 
oak: In your code behind you can set the visible property to &quot;false&quot; for either the panel, or the label or textbox, should a Null be returned from the reader. One way to approach the problem. If the visible property is set to false it is not rendered, of course, on the client page.
 
Try
Code:
Page.Controls.Remove(Page.FindControl(&quot;plnPhone&quot;))
 
I need to have the space removed as well as the text in the textbox so just setting visible to false won't help
Here is a complete sample of what I CAN'T get to work. I obviously am doing something wrong with the controls.remove method
<code>
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm2.aspx.vb&quot; Inherits=&quot;listings1.WebForm2&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio .NET 7.1&quot;>
<meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic .NET 7.1&quot;>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; </HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:Label id=&quot;lblgo&quot; style=&quot;Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 72px&quot; runat=&quot;server&quot;>BeGone</asp:Label>
<asp:Label id=&quot;Label2&quot; style=&quot;Z-INDEX: 102; LEFT: 104px; POSITION: absolute; TOP: 112px&quot; runat=&quot;server&quot;>Stay</asp:Label>
</form>
</body>
</HTML>

</code>
The code behind page is
<code>
Public Class WebForm2
Inherits System.Web.UI.Page

#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents lblgo As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Page.Controls.Remove(Page.FindControl(&quot;lblgo&quot;))
End Sub

End Class
</code>

Thanks to any that have suggestions.
 
Looks like you 're trying to remove this control on page first run.
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
 if Page.IsPostBack then
  Page.Controls.Remove(Page.FindControl(&quot;lblgo&quot;))
 end if
End Sub
 
Well I tried putting the remove into a ispostback and it doesn't help ????? It's as if findcontrol(&quot;lblgo&quot;) doesn't find anything. If I put in page.controls.clear() this removes everything.

???? Still stumped.
 
Code:
HtmlForm form = (HtmlForm)Page.FindControl(&quot;lblgo&quot;);
form.Controls.Remove(Page.FindControl(&quot;lblgo&quot;));
See if this helps.
 
Thanks for your efforts. I wound up just putting the textboxes and labels in a panel and setting the panel.visible = false. This also gets rid of the white space.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top