How do I Dynamically Create Controls, and attach e

Results 1 to 2 of 2

Thread: How do I Dynamically Create Controls, and attach e

  1. #1
    Join Date
    Dec 1969

    Default How do I Dynamically Create Controls, and attach e

    First let me tell you what I wish to do:<BR>1. Create textboxes/Checkboxes/dropdownlistboxes and labels dynamically on a web page, using a stored procedure as the source of information (where the fields being pulled from the database are random, based on a given unique id).<BR>2. I would like to be able to read the information that the user puts into these dynamic fields (I assume this may be best using an array, so that I can simply loop through the fields systematically ... perhaps using GetRows()?)<BR>3. I wish to attach Functions/Subs to some of these dynamic objects, such that when changed or selected I can validate the information (looking only for specific values).<BR><BR>I have tried many methods to create dynamic controls on an aspx page. I have successfully created them, but not in a way that was persistent (re-appeared after postback), and have not been able to associate Event Handlers to them.<BR>In reading many articles regarding the object model of the .net aspx pages, it has appeared to me that perhaps the best place to create these controls is in the Page_Init Sub ... but even this has created problems for me, as I am unsure when or where I can create my record set information, and it appears that for peristence through postbacks, the objects muct be declared under the #Region area, before the Page_Init sub.<BR>However, I have read that .NET also backward initializes new objects, such that when you create them in the code (such as in the Page_Load Sub), the system creates them from scratch, running through all of the initialization methods until it catches up with the where you should be. This does not happen, and I have not seen any other reference to this.<BR><BR>While the following code does render the desired information onto the page, it does not persist through postbacks (meaning I have to re-create these every round trip ... which is un-desired), nor have I been able to attach event handles to any of these.<BR>And, what I would ultimately like to do is loop through this code for every new "registrant" the user wishes to add. So, they would fill out the information in these fields, and if desired, they would click on a button that would run through this code twice ... once to display the information they just filled out, and a second time to have a new set of blank fields displayed to enter a new record.<BR>Here is a code clip from my code in hopes that it may shed some light on the subject:<BR><BR> Private Sub CreateUserFields()<BR> Dim cmdUserFields As New SqlCommand("conf_GetConferenceUserFields", cnFields)<BR> cmdUserFields.CommandType = CommandType.StoredProcedure<BR> cmdUserFields.Parameters.Add(New SqlParameter("@nConferenceID", SqlDbType.Int))<BR> cmdUserFields.Parameters("@nConferenceID").Value = nConfID<BR><BR> Dim daGetPrograms As New SqlDataAdapter(cmdUserFields)<BR> Dim dsPrograms As New DataSet<BR> daGetPrograms.Fill(dsPrograms, "Programs")<BR><BR> Dim dtProgramDataTable As DataTable<BR> dtProgramDataTable = dsPrograms.Tables(0)<BR><BR> &#039; SELECT A_Field_Tbl.Field AS Field, <BR> &#039; A_Field_Tbl.FieldDataType AS FieldDataType, <BR> &#039; A_Field_Tbl.FieldDisplayType AS FieldDisplayType, <BR> &#039; A_Field_Tbl.FieldSize AS FieldSize, <BR> &#039; A_Field_Tbl.FieldMaxSize AS FieldMaxSize, <BR> &#039; A_Field_Tbl.FieldSelect AS FieldSelect, <BR> &#039; Conf_ConferenceFields_Tbl.Active AS Active, <BR> &#039; Conf_ConferenceFields_Tbl.Hidden AS Hidden, <BR> &#039; Conf_ConferenceFields_Tbl.Required AS Required, <BR> &#039; Conf_ConferenceFields_Tbl.DisplayName AS FieldName, <BR> &#039; Conf_ConferenceFields_Tbl.FieldInstructions AS Instruct <BR> &#039; FROM A_Field_Tbl INNER JOIN Conf_ConferenceFields_Tbl ON <BR> &#039; A_Field_Tbl.FieldID = Conf_ConferenceFields_Tbl.FieldID <BR> &#039; WHERE (Conf_ConferenceFields_Tbl.Active = 1) AND <BR> &#039; (Conf_ConferenceFields_Tbl.ConfID =@nConferenceID)<BR><BR> Dim pnlPanel As New Panel<BR> Dim theRow As DataRow<BR> Dim nCounter As Int32<BR> For Each theRow In dtProgramDataTable.Rows<BR> Dim tblTable As New Table<BR> Dim trRow As New TableRow<BR> Dim tcCell As New TableCell<BR> Dim tcCell2 As New TableCell<BR> Dim lblLabel As New Label<BR> Dim txtBox As New TextBox<BR> Dim ddlList As New DropDownList<BR> Dim cbCheckBox As New CheckBox<BR> Dim cblCheckList As New CheckBoxList<BR><BR> pnlPanel.ID = "Registrant" & Session("Registrant")<BR> pnlPanel.BorderStyle = BorderStyle.Inset<BR> tblTable.Width = New Unit("100%")<BR> tcCell.Width = New Unit("25%")<BR> tcCell.CssClass = ("cssLeftColumn")<BR> tcCell2.Width = New Unit("75%")<BR><BR> lblLabel.ID = "lbl" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> If theRow("Required") = 1 Then<BR> lblLabel.Text = Trim(theRow("FieldName")) & ":*&#060;font class=&#039;cssRequired&#039;&#062;*&#060;/font&#062;*"<BR> Else<BR> lblLabel.Text = Trim(theRow("FieldName")) & ":**"<BR> End If<BR> tcCell.Controls.Add(lblLabel)<BR><BR> Select Case theRow("FieldDisplayType")<BR> Case 0 &#039; Label<BR> Case 1 &#039; Regular text box<BR> txtBox.ID = "" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> txtBox.TextMode = TextBoxMode.SingleLine<BR> txtBox.CssClass = "cssFieldTextBox"<BR> txtBox.Style.Add("width", (5 * theRow("FieldSize")))<BR> tcCell2.CssClass = ("cssRightColumnTextBox")<BR> tcCell2.Controls.Add(txtBox)<BR> Case 2 &#039; Check Box<BR> cbCheckBox.ID = "" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> cbCheckBox.CssClass = "cssFieldTextBox"<BR> tcCell2.CssClass = ("cssRightColumnCheckBox")<BR> tcCell2.Controls.Add(cbCheckBox)<BR> Case 3 &#039; Drop Down List no parameter passing<BR> ddlList.ID = "" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> GetDropDownListItems(ddlList, Trim(theRow("FieldSelect")))<BR> ddlList.CssClass = "cssRightColumnDropDownList"<BR> tcCell2.Controls.Add(ddlList)<BR> Case 31 &#039; Drop Down List passing conferenceid and date<BR> ddlList.ID = "" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> GetRegistrationTypes(ddlList, Trim(theRow("FieldSelect")))<BR> ddlList.CssClass = "cssRightColumnDropDownList"<BR> tcCell2.Controls.Add(ddlList)<BR> Case 32 &#039; Drop Down List passing conferenceid<BR> ddlList.ID = "" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> GetGroupInformation(ddlList, Trim(theRow("FieldSelect")))<BR> ddlList.CssClass = "cssRightColumnDropDownList"<BR> tcCell2.Controls.Add(ddlList)<BR> End Select<BR><BR> &#039; If Trim(theRow("Instruct")).Length &#062; 0 Then<BR> &#039; Dim lblInstruct As New Label<BR> &#039; lblInstruct.ID = "lblInstruct" & Trim(theRow("Field")) & Session("Registrants") & ""<BR> &#039; lblInstruct.Text = "**" & Trim(theRow("Instruct"))<BR> &#039; lblInstruct.CssClass = "cssFieldInstruction"<BR> &#039; tcCell2.Controls.Add(lblInstruct)<BR> &#039; End If<BR><BR> If theRow("Required") Then<BR> Dim fvValidateField As New RequiredFieldValidator<BR> fvValidateField.ID = "Validator" & theRow("Field") & Session("Registrants") & ""<BR> fvValidateField.ControlToValidate = "" & theRow("Field") & Session("Registrants") & ""<BR> fvValidateField.Display = ValidatorDisplay.Dynamic<BR> fvValidateField.CssClass = "cssRequired"<BR> fvValidateField.Text = "Field is Required, please fill in this field"<BR> tcCell2.Controls.Add(fvValidateField)<BR> End If<BR><BR> trRow.Cells.Add(tcCell)<BR> trRow.Cells.Add(tcCell2)<BR> tblTable.Rows.Add(trRow)<BR> pnlPanel.Controls.Add(tblTable)<BR> nCounter += 1<BR> Next<BR> If TravelInfo = 1 Then<BR> CreateTravelFields(pnlPanel)<BR> End If<BR> If NonSummitFieldInfo = 1 Then<BR> CreateNonSummitFields(pnlPanel)<BR> End If<BR> pnlPersonalInformation.Controls.Add(pnlPanel)<BR> End Sub<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: How do I Dynamically Create Controls, and atta

    1. ASP.NET forum<BR>2. look up &#039;placeholders&#039; in the docs.<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts