Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Making FindControl find dynamic controls

Reply
Thread Tools

Making FindControl find dynamic controls

 
 
Michael
Guest
Posts: n/a
 
      07-24-2004
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 aspanel 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>
:
<aspanel ID="Panel1" Runat="server" Visible="True">
:
<asp:ListBox ID="lbThisWorks" Runat="server" Rows="10"
Width="400" SelectionMode="Multiple"></asp:ListBox>
</aspanel>
<aspanel 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>
</aspanel><!-- 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>
:
:
 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      07-25-2004
"Michael" <> wrote in message
news: om...
> 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 aspanel 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.
--
John Saunders
johnwsaundersiii at hotmail


 
Reply With Quote
 
 
 
 
brisers
Guest
Posts: n/a
 
      07-28-2004
<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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Losing Hope! FindControl of dynamic gridview not working K B ASP .Net 2 09-17-2007 12:27 AM
Dynamic Controls created by Dynamic Controls Ronald ASP .Net 2 01-17-2006 12:44 AM
Custom Controls- Controls Collection-FindControl method Sundararajan ASP .Net Web Controls 2 03-10-2005 06:13 AM
Making FindControl find dynamic controls Michael ASP .Net 2 07-28-2004 10:13 AM
Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls Denny Smolinski via .NET 247 ASP .Net Web Controls 1 05-04-2004 09:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57