dynamic dropdownlist control

Asked By toma hawk
24-Aug-04 12:34 AM
Earn up to 0 extra points for answering this tough question.
Hi in my code for aspx i create a dropdown list dynamically and add it to a place holder control ,but upon postback i am unable to find the control or find the selected value in the dropbox so that i can create another dropbox with some other values.Here is the code how do i solve this issue ? i tried adding the control again after postback Please help --------------------------------------------------------- Dim dd1 As DropDownList = New DropDownList Dim dd2 As DropDownList = New DropDownList Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Me.IsPostBack = True Then ' PlaceHolder1.Controls.Add(dd1) ' Dim dd2 As DropDownList = CType(Me.Placeholder1.FindControl("dd1"), DropDownList) If dd1.SelectedIndex = 2 Then dd2.Items.Add("None") dd2.Items.Add("B1") dd2.Items.Add("B2") PlaceHolder1.Controls.Add(dd2) End If Else dd1.Items.Add("None") dd1.Items.Add("A1") dd1.Items.Add("A2") dd1.AutoPostBack = True dd1.EnableViewState = False PlaceHolder1.Controls.Add(dd1) End If End Sub -----------------------------------------------------------------------

  Dynamically created ASP NET controls

Asked By Peter Bromberg
24-Aug-04 08:25 AM
MUST be re-created on a postback. Repeat after me: "Dynamically created controls...."

  Recreating dynamically created controls

Asked By blake miller
21-Sep-04 04:42 AM
I have tried recreating the controls that I had dynamically created prior to postback, and I can access the controls, but their values are all null. I've seen this answer (that you have to "recreate" the controls after postback), but I'm doing this, and the controls still show as not having anything in them. What I have: Upon page load, I create several textbox objects and add them to a System.Web.UI.WebControls.Table object. Here's some sample code: Protected WithEvents dynInsertTbl As System.Web.UI.WebControls.Table Dim bCell As System.Web.UI.WebControls.TableCell Dim tmp As New System.Web.UI.WebControls.TextBox Dim boxRow As New System.Web.UI.WebControls.TableRow tmp.ID = colHeaders(i) tmp.Width = System.Web.UI.WebControls.Unit.Pixel(45) bCell = New System.Web.UI.WebControls.TableCell bCell.Controls.Add(tmp) boxRow.Cells.Add(bCell) dynInsertTbl.Rows.Add(boxRow) Upon post back, I call a function that does exactly the above, and then print out the textboxes: Response.Write(CType(dynInsertTbl.Rows(1).Cells(i).Controls(0), TextBox).Text().Trim() + "<br />") Note: there are several textboxes, hence why I use "cells(i)" above. When I access the textbox upon postback, it doesn't show what I typed into the textbox prior to postback? I don't really understand why recreating the textboxes allows you to access the dynamically generated textboxes. Doesn't recreating the textbox also set it's value to null?

  Exactly

Asked By Peter Bromberg
21-Sep-04 02:08 PM
you have to restore the values since after they are re-created, they are "Brand new'. You can do this by overriding or saving to ViewState, or by storing values in Session.
  ?
Asked By blake miller
21-Sep-04 05:19 PM
But that's the point I'm trying to make....I have no access to the values because they are gone. I'm saying, when I try to access the textboxes, they show a null value. How do I access the value that was typed into textboxes?
  OK let's back up for a second
Asked By Peter Bromberg
21-Sep-04 05:49 PM
Dynamically added controls have no object reference variable in the codebehind class. They appear only in the control collection of the containing control, i.e. the Page.Controls collection. When the page is posted back to the server as a result of user interaction a new instance of the codebehind class is instantiated, and all the variables of the class are set with values from ViewState. This means that the objects we are accessing from the codebehind class actually are new ones that got their predecessors' values via ViewState. So, the controls that were dynamically created are no longer there and consequently the values returned from these controls have no place to go. They are lost in the viewstate. In order to catch these values the dynamically generated controls needs to be re-generated at Page_Load. The important thing is to assign the same ID to each control as it previously had. The ViewState uses this ID property of the Control object to reinstate the values. Example: private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) this.NumberOfControls = 0; else // your method to add dynamic controls programmatically this.createControls(); } private void createControls() { int count = this.NumberOfControls; for(int i = 0; i < count; i++) { TextBox txb = new TextBox(); txb.ID = "ControlID_" + i.ToString(); //Add the Controls to the container of your choice Page.Controls.Add(txb); } } // example of dynamic addition of controls // note the use of the ViewState variable private void addSomeControl() { TextBox txb = new TextBox(); txb.ID = "ControlID_" + NumberOfControls.ToString(); Page.Controls.Add(txb); this.NumberOfControls++; } -- Hope this clarifies things for you.
  Theory
Asked By blake miller
21-Sep-04 08:18 PM
I understand the theory you're presenting, and I've tried a simpler experiment than I was attempting previously. I have a function called "InjectInsertTable()" that instantiates a textbox and adds it to a panel object. sub InjectInsertTable() Dim tmp As New System.Web.UI.WebControls.TextBox tmp.ID = "blake" tmp.Visible = True InsertRecordPnl.Controls.Add(tmp) InsertRecordPnl.Visible = True end sub "InsertRecordPnl" is a panel that I have created in the Visual Studio designer. I'm placing a textbox on the panel, dynamically, and the textbox shows when the page loads. Next, I enter something in the textbox, and click a button that posts the form back. Here is the code for the button handler: private sub AddRecordBtn_click(...) InjectInsertTable() ' <-- recreate the textbox Dim meal As TextBox = InsertRecordPnl.FindControl("blake") Response.Write("::" + meal.Text.Trim() + "::") end sub It prints nothing. I have created the textbox once with the InjectInsertTable function, and upon postback, I call the SAME function again, thereby recreating it. Then I print, and I still get nothing. I am sorry, I am a nube...: /
  Solution
Asked By blake miller
21-Sep-04 10:16 PM
Ok, I wasn't listening. I was trying to recreate the textboxes in my button event handler, which, apparently is not where the recreation should occur. I put my "recreation" code in the postback part of the page_load event, and I am indeed able to get the values that were in the textboxes from the button handler function. Ah, 10 hours of growing pains.... Thanks for the help and excellent explanation Pete.
Create New Account