How to retrieve values of dynamically added web controls on the form??

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default How to retrieve values of dynamically added web controls on the form??

    Hi,
    I have created a survey form in asp.net that has dynamically generated
    fields( based on the record that I get from the DB I adding a text box
    or a dropdown to the form). My problem is, how do I get the values of
    the text box or dropdown list to save in the DB after user has filled
    the survey?

    I have used the following code in my Page_Load sub:


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim conn As New
    SqlConnection(ConfigurationSettings.AppSettings("c onnectionstring"))
    Dim cmd As New SqlCommand()
    cmd.CommandText = "SELECT b.ftext as ftext, a.ftid as ftid FROM
    pagedetail a INNER JOIN fieldtext b ON a.fid = b.fid "
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text
    Dim dr As SqlDataReader
    conn.Open()
    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    Do While dr.Read()'for each record print the label and look in
    the field type table to see which field to add to the form.
    Dim Label As New Label()
    Dim labeltext As String
    labeltext = dr(0)
    Label.Text = labeltext
    ph.Controls.Add(Label)
    Dim conn1 As New
    SqlConnection(ConfigurationSettings.AppSettings("c onnectionstring"))
    Dim cmd1 As New SqlCommand()
    Dim newvalue As String
    cmd1.CommandText = "SELECT ftype from fieldtype where fid="
    & dr(1)
    cmd1.Connection = conn1
    cmd1.CommandType = CommandType.Text
    conn1.Open()
    newvalue = cmd1.ExecuteScalar
    If newvalue = "text" Then 'add a text box
    Dim tb As New TextBox()
    placeholder.Controls.Add(tb)
    ElseIf newvalue = "dropdown" Then
    Dim dd As New dropdownlist()
    placeholder.controls.add(dd)
    End If
    Loop

    *****this is where I am stuck************
    Dim el As Control
    For Each el In Controls
    Dim controltype As String
    controltype = el.ToString()
    If controltype = "System.Web.UI.HtmlControls.HtmlForm" Then
    I NEED THE VALUE OF THE TEXT BOX OR THE DROPDOWN LIST.
    End If



    Next
    End Sub

    *******************
    I would really appreciate if anyone can help me on this.....





    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Aruna Bajpayee Guest

  2. Similar Questions and Discussions

    1. Dynamically added controls are lost on a Postback
      Hi, i created a custom control overriden from HierarchicalDataBoundControl. In method PerformDataBinding I add some controls (Controls.Add(new...
    2. Retrieve Form Names and values
      Is there a way to loop through a list of fields that have been submitted to a page?? I know how to do it in asp, but im new to CF. Also do you...
    3. Trouble with dynamically added controls
      Hi everybody, I need to dynamically populate a webpage at runtime with controls. This is the code I wrote. Public Class WebForm1 ...
    4. onclick event for dynamically added controls?
      when you build the control you will need to set it's id to a unique value. then you will need to daisy chain the click event to a handler...
    5. Positioning Dynamically added controls
      Hi I am adding a textbox control at runtime that allows a user to enter a date. I also want to add a calendar icon beside the textbox so that the...
  3. #2

    Default Re: How to retrieve values of dynamically added web controls on the form??

    Hi,
    You can use findcontrol method to get the control and then retrieve its
    value.
    like: ph.findcontrol(controlid).
    So when you add the control to controls, give every control an ID.

    hope it helpful.


    --
    Juno
    MCSD.NET, MCDBA, MCSE
    ----------------------------------------------------------
    Support Team of EasyDotNet, INC. [url]http://www.EasyDotNet.com[/url]
    DataForm.NET - The most powerful data entry web server control for ASP.NET


    "Aruna Bajpayee" <aruna.bajpayee@delphi.com>
    ??????:uede5qycEHA.1764@TK2MSFTNGP10.phx.gbl...
    > Hi,
    > I have created a survey form in asp.net that has dynamically generated
    > fields( based on the record that I get from the DB I adding a text box
    > or a dropdown to the form). My problem is, how do I get the values of
    > the text box or dropdown list to save in the DB after user has filled
    > the survey?
    >
    > I have used the following code in my Page_Load sub:
    >
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Dim conn As New
    > SqlConnection(ConfigurationSettings.AppSettings("c onnectionstring"))
    > Dim cmd As New SqlCommand()
    > cmd.CommandText = "SELECT b.ftext as ftext, a.ftid as ftid FROM
    > pagedetail a INNER JOIN fieldtext b ON a.fid = b.fid "
    > cmd.Connection = conn
    > cmd.CommandType = CommandType.Text
    > Dim dr As SqlDataReader
    > conn.Open()
    > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    > Do While dr.Read()'for each record print the label and look in
    > the field type table to see which field to add to the form.
    > Dim Label As New Label()
    > Dim labeltext As String
    > labeltext = dr(0)
    > Label.Text = labeltext
    > ph.Controls.Add(Label)
    > Dim conn1 As New
    > SqlConnection(ConfigurationSettings.AppSettings("c onnectionstring"))
    > Dim cmd1 As New SqlCommand()
    > Dim newvalue As String
    > cmd1.CommandText = "SELECT ftype from fieldtype where fid="
    > & dr(1)
    > cmd1.Connection = conn1
    > cmd1.CommandType = CommandType.Text
    > conn1.Open()
    > newvalue = cmd1.ExecuteScalar
    > If newvalue = "text" Then 'add a text box
    > Dim tb As New TextBox()
    > placeholder.Controls.Add(tb)
    > ElseIf newvalue = "dropdown" Then
    > Dim dd As New dropdownlist()
    > placeholder.controls.add(dd)
    > End If
    > Loop
    >
    > *****this is where I am stuck************
    > Dim el As Control
    > For Each el In Controls
    > Dim controltype As String
    > controltype = el.ToString()
    > If controltype = "System.Web.UI.HtmlControls.HtmlForm" Then
    > I NEED THE VALUE OF THE TEXT BOX OR THE DROPDOWN LIST.
    > End If
    >
    >
    >
    > Next
    > End Sub
    >
    > *******************
    > I would really appreciate if anyone can help me on this.....
    >
    >
    >
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!


    Neo Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139