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

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

You're probably creating the controls *after* the click event handler.  Dynamically added controls aren't automatically re-created on postback, and they're not implicitly stored in Viewstate (or so I understand).

You'll need to add your controls a la Page.ViewState.Add("AttorneyFirstName", tbAttorneyFirstName) so you can pick them up later.  Of course you could just use the Request.Form collection which has every form element from the posted page, but Viewstate is the common Microsoft answer (though I avoid it when possible).



P.S.  If you want a more ASP.NET-specific list, consider http://groups.yahoo.com/group/AspNetAnyQuestionIsOK or some of the other groups at http://www.aspfriends.com .  Not like you can't post here, but you may get more specialized information there.

 From: "Tab Alleman" <talleman at Lumpsum.com>

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
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. * *

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