Making FindControl find dynamic controls

M

Michael

Need some help trying to read values from web controls - specifically
*finding* the controls (like a drop down list) - that are added
dynamically added within an asp:panel control.

The page contains multiple panels within the form. The panels appear
at different times - for our argument, let's say panel 1 appears
first, users clicks a submit button and then
panel 1 disappears and panel 2 appears, etc.

When clicks on the submit button the first time (effectively switching
from panel 1 to panel 2), I can read the list box in panel 1 with
FindControl - all is right with the world.

I can create controls and add them to the asp:Table control
(ID="tabContainsDynamicControls") - again we're cool. Controls are
unique, tested with 1 and many rows. When adding controls, I verified
the parent of the control added to a table cell are indeed mathces the
name of the target cell, verified the table cell parent if the target
table row, etc. all the way to the tabContainsDynamicControls table
explicitly definied in the ASPX.
I did verify the tabContainsDynamicControls has the runat server
attribute and the tabContainsDynamicControls is defined in the
aspx.cs.

But when the user clicks submit the 2nd time and I hit the PageLoad or
OnInit or the event handler for the submit button, I cannot get
FindControl to find the dynamically added controls.
I can access any control explicitly mentioned in the ASPX.

WHAT in the world is going on??


IN THE ASPX:

<%@ Page language="c#" Codebehind="my.aspx.cs" AutoEventWireup="false"
Inherits="whatever" AspCompat="true" %>
:
<HTML>
:
<body class="bodymain">
<form runat="server" id="frmMain">
<div id="divMainBody" style="DISPLAY:block">
:
<table cellSpacing="0" cellPadding="0" width="100%"
bgColor="#ffffff" border="0">
:
<tr>
:
<td vAlign="top" align="left" width="100%">
<table id="tblMain" width="100%">
<tr id="trHeader">
:
</tr><!-- trHeader -->
<tr id="trStatusMessage">
:
</tr><!-- trStatusMessage -->
<tr id="trForm">
<td>
:
<asp:panel ID="Panel1" Runat="server" Visible="True">
:
<asp:ListBox ID="lbThisWorks" Runat="server" Rows="10"
Width="400" SelectionMode="Multiple"></asp:ListBox>
</asp:panel>
<asp:panel ID="Panel2" Runat="server" Visible="True">
:
<p/>
<asp:Table ID="tabContainsDynamicControls" Runat="server"
CssClass="myTableCss" CellPadding="2">
<asp:TableRow ID="trHeader" Runat="server"
CssClass="myTableHeaderCss">
microsoft.public.dotnet.framework.aspnet.* (8 groups)
<asp:TableCell Runat="server">
column contains simple text
</asp:TableCell>
<asp:TableCell Runat="server">
column contains drop down lists
</asp:TableCell>
<asp:TableCell Runat="server">
column contains different web controls
</asp:TableCell>
<asp:TableCell Runat="server">
column contains check boxes
</asp:TableCell>
<-- dynamic rows added here, see code
</asp:TableRow>
</asp:Table>
</asp:panel><!-- Panel2 -->

IN THE ASPX.CS:
:
protected System.Web.UI.WebControls.Table tabFieldsSelect;
:

<following code is in a routine called from PageLoad()>
:
System.Web.UI.WebControls.TableRow tr = new
System.Web.UI.WebControls.TableRow();
tr.ID = UniqueTableName();
tabContainsDynamicControls.Controls.Add(tr);
:
TableCell tc;

//cell 1
tc = new TableCell();
tc.ID = UniqueCell1Name(..);
tc.Text = text;
tr.Controls.Add(tc);

//cell 2
tc = new TableCell();
tc.ID = UniqueCell2Name(..);
DropDownList ddl = this.Control2DDL(..);
tc.Controls.Add(ddl);
tr.Controls.Add(tc);

//cell 3
WebControl webControl;
tc = new TableCell();
tc.ID = UniqueCell3Name(..);
webControl = this.Cell3Control(..);
tc.Controls.Add(webControl);
tr.Controls.Add(tc);

//cell 4
tc = new TableCell();
tc.ID = UniqueCell4Name(..);
CheckBox cb = new CheckBox();
cb.ID = UniqueControl5Name(..);
tc.Controls.Add(cb);
tr.Controls.Add(tc);

VIEW SOURCE WITH PANEL2 VISIBLE

:
<div id="pnlStep2">
:
<table id="tabContainsDynamicControls" class="myTableCss"
cellpadding="2" border="0">
<tr id="trHeader" class="GPUTableHeader">
<td>
column contains simple text
</td><td>
column contains drop down lists
</td><td>
column contains different web controls
</td><td>
column contains check boxes
</td>
</tr><tr id="tr393" class="TableRowClass">
<td id="tdName393">Joe Muggs</td><td id="tdDropDown393"><select
name="Select393" id="Select393">
<option value="1">Equal To</option>
<option value="2">Not Equal To</option>
<option value="3">Less Than</option>
<option value="4">Greater Than</option>
</select></td><td id="tdAnyControlType393"><select name="wcAny393"
id="wcAny393">
<option selected="selected" value="1">Dog</option>
<option value="2">Cat</option>
<option value="3">Mangoes</option>
<option value="4">Ants</option>
:
:
 
J

John Saunders

Michael said:
Need some help trying to read values from web controls - specifically
*finding* the controls (like a drop down list) - that are added
dynamically added within an asp:panel control.

The page contains multiple panels within the form. The panels appear
at different times - for our argument, let's say panel 1 appears
first, users clicks a submit button and then
panel 1 disappears and panel 2 appears, etc.

Instead of loading the controls dynamically, I suggest that you always load
all controls, and simply make all panels invisible except the one you want
to see.

This is important because all controls need to be added in the same order on
every request.
 
G

Guest

<following code is in a routine called from PageLoad()>

I suspect that it has something to do with creating the dynamic controls too late in the process. They need to be created in Page_Init or even better in the Init handler for the Web Control. This will hopefully ensure that the events and viewstate stuff is handled properly. Let me know if this helps.
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top