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

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

Post by Aruna Bajp » Wed, 28 Jul 2004 01:07:59


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.yqcomputer.com/ ***
Don't just participate in USENET...get rewarded for it!
 
 
 

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

Post by Neo » Thu, 29 Jul 2004 06:23:40

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. http://www.yqcomputer.com/
DataForm.NET - The most powerful data entry web server control for ASP.NET


"Aruna Bajpayee" < XXXX@XXXXX.COM >
??????: XXXX@XXXXX.COM ...