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

Peter Brunone (EasyListBox.com) peter at easylistbox.com
Tue Feb 21 11:07:21 CST 2006


Not just define (Dim) it; *create* it there.  If you add it to the page structure, then it's not going to go away like a variable which only has local scope.

 From: "Tab Alleman" talleman at Lumpsum.com

So you're saying I should DIM the control in page_init, but where do I "FindControl()" it? Still in page_init, or in the submit_click event handler?

> -----Original Message-----
> From: thelist-bounces at lists.evolt.org On Behalf Of James Conley
> 
> 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 On Behalf Of Tab Alleman
> 
> 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:
> 
> 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?



More information about the thelist mailing list