[thelist] (asp.net) accessing controls created at runtime.

James Conley Conleyj at kubota-kma.com
Tue Feb 21 10:45:49 CST 2006


If you want to get events from dynamically created controls then you
need to dynamically create those controls on post back *before* the post
back data is "restored" into the controls.

So - any controls you create dynamically should be done in either
page_init or page_load. Probably page_init is safer.


james c


-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Tab Alleman
Sent: Tuesday, February 21, 2006 11:26 AM
To: Evolt Thelist (E-mail)
Subject: [thelist] (asp.net) accessing controls created at runtime.

I am creating controls at runtime, and they show up on the page, but
when the page is submitted, I can't find them.  What am I doing wrong?

To reduce the code to critical elements, I have a datacell on my .aspx
page like this:

<td class='TableData' id="tdAttorney" runat="server" nowrap></td>


Then in my vb.net code-behind, I populate the cell like this:

Dim  tdAttorney As System.Web.UI.HtmlControls.HtmlTableCell
tdAttorney = e.Item.FindControl("tdAttorney")

'add textboxes
Dim tbAttorneyFirstName As New System.Web.UI.HtmlControls.HtmlInputText
Dim tbAttorneyLastName As New System.Web.UI.HtmlControls.HtmlInputText
            With tbAttorneyFirstName
                .Value = e.Item.DataItem("AttorneyFirstName").ToString
                .Size = 20
                .ID = "tbAttorneyFirstName"
                .MaxLength = 50
            End With
            With tbAttorneyLastName
                .Value = e.Item.DataItem("AttorneyLastName").ToString
                .Size = 20
                .ID = "tbAttorneyLastName"
                .MaxLength = 50
            End With

            With tdAttorney
                .Controls.Add(tbAttorneyFirstName)
                .Controls.Add(tbAttorneyLastName)
            End With

-----------

Ok, all of this works...the textboxes show up on the page.  In the
submit_click handler, I've got this:

                tdAttorney = itemSI.FindControl("tdAttorney")

                tbAttorneyFirstName =
tdAttorney.FindControl("tbAttorneyFirstName")
                tbAttorneyLastName =
tdAttorney.FindControl("tbAttorneyLastName")


----when I stop the page in the debugger, it's finding tdAttorney (the
datacell), but it's saying it doesn't contain any controls.  The page
doesn't contain the controls, either.  

What am I missing?
-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and
archives of thelist go to: http://lists.evolt.org Workers of the Web,
evolt ! 



More information about the thelist mailing list