Making FindControl find dynamic controls

Discussion in 'ASP .Net Web Controls' started by Michael, Jul 24, 2004.

  1. Michael

    Michael Guest

    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>
    :
    :
     
    Michael, Jul 24, 2004
    #1
    1. Advertising

  2. "Michael" <> wrote in message
    news:...
    > 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.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 25, 2004
    #2
    1. Advertising

  3. Michael

    brisers 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.
     
    brisers, Jul 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward
    Replies:
    4
    Views:
    3,138
    S. Justin Gengo
    Jun 14, 2004
  2. Michael
    Replies:
    2
    Views:
    7,987
    =?Utf-8?B?YnJpc2Vycw==?=
    Jul 28, 2004
  3. Ronald
    Replies:
    2
    Views:
    853
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    473
    Eidolon
    May 4, 2004
  5. Sundararajan

    Custom Controls- Controls Collection-FindControl method

    Sundararajan, Mar 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    195
    Sundararajan
    Mar 10, 2005
Loading...

Share This Page