retrieve value of dynamic web control

Discussion in 'ASP .Net Building Controls' started by Aruna Bajpayee, Jul 26, 2004.

  1. 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("connectionstring"))
    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("connectionstring"))
    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 http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Aruna Bajpayee, Jul 26, 2004
    #1
    1. Advertising

  2. "Aruna Bajpayee" <> wrote in message
    news:...
    > 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("connectionstring"))
    > 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("connectionstring"))
    > 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


    Why not get the value right after you've added the TextBox or DropDownList?
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 26, 2004
    #2
    1. Advertising

  3. Thanks for the reply but how would I do that?
    User has to fill out the info before I can retrieve it.Am I going in the
    wrong direction?


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Aruna Bajpayee, Jul 26, 2004
    #3
  4. Aruna Bajpayee

    Austin Guest

    Austin, Jun 7, 2005
    #4
  5. Aruna Bajpayee

    Guest

    Austin wrote:
    > Did anyone get you a fix. We are having the same problem here
    >
    > From http://www.developmentnow.com/g/10_2004_7_0_0_77275/retrieve-value-of-dynamic-web-control.htm


    I'm not sure what the problem is. First of all, if you're adding the
    controls to a placeholder, why are you looping through all the controls
    on the form?

    For Each myControl as Control in placeholder.Controls
    Next

    Then again, you can also create an ArrayList and add the controls to
    the ArrayList as you create them.

    Private myList as New ArrayList

    If newvalue = "text" Then 'add a text box
    Dim tb As New TextBox()
    placeholder.Controls.Add(tb)
    myList.Add(tb)
    ElseIf newvalue = "dropdown" Then
    Dim dd As New dropdownlist()
    placeholder.controls.add(dd)
    myList.Add(dd)
    End If

    And then loop through the ArrayList.

    Or heck, just store the control names in a string array, and then loop
    through Page.Request.Form.Keys until you get a match, and look at
    Page.Request.Form at the same index for the value. If it's not there,
    then either you have an empty TextBox, or you did something weird with
    the DropDown.

    Lisa
     
    , Jun 7, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    600
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Larry Bud
    Replies:
    1
    Views:
    366
    bruce barker
    Jan 10, 2007
  3. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    503
    Eidolon
    May 4, 2004
  4. Nick

    Retrieve PostBack value of dynamic control

    Nick, May 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    172
  5. Replies:
    3
    Views:
    229
    Alessandro Zifiglio
    Aug 2, 2006
Loading...

Share This Page