I will appreciate answers in VB or C#
we use a database to create Data Collection forms with dynamic items
I used this code to generate labels and textboxes from a table in database (when the user hits the button "load CRF")
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CRFgrid As New GridView
CRFgrid.DataSource = CRFds
CRFgrid.DataBind()
Dim ItemCount As Integer = CRFgrid.Rows.Count
Session("Itemcount") = CRFgrid.Rows.Count
For I = 0 To (ItemCount - 1)
Dim itemname As String = CRFgrid.Rows(0 + I).Cells(1).Text.ToString
Session("Item") = "Item" + (I + 1).ToString
Dim ItemNamelabel As New Label
Dim ItemNameBox As New TextBox
ItemNamelabel.ID = "L" + (I + 1).ToString
Session("FU" + I.ToString) = ItemNamelabel.ID.ToString
ItemNameBox.ID = "T" + (I + 1).ToString
ItemNamelabel.Text = "<br />" + (Session("Item").ToString) + " " + itemname + " " + "<br />"
Me.Form.Controls.Add(ItemNamelabel)
Me.Form.Controls.Add(ItemNameBox)
Next
End Sub
when the data collection form is loaded, the page looks like this
and the "view page source" shows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTI0NTA5MDI3NGRkUb8sl0uZpLbvUN/GSmHgjYxS9xqGR7rmcMBR3Ufhz4w=" />
</div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWCQLf7PXOCQKM54rGBgK7q7GGCALF9vKRBQLs7+btDALs7+LtDALs797tDALs79rtDALs79btDDHrb+Vhkcph8brtCJP9//5IH57FFoImey2PApARP+k1" />
<input type="submit" name="Button1" value="LoadCRF" id="Button1" style="width:85px;" />
<br />
<br />
<input type="submit" name="Button2" value="InsertData" id="Button2" style="width:85px;" />
<br />
<br />
MRN
<input name="MRNtxt" type="text" id="MRNtxt" />
<br />
<br />
<span id="L1"><br />Item1 Diagnosis <br /></span><input name="T1" type="text" id="T1" /><span id="L2"><br />Item2 Treatment Protocol <br /></span><input name="T2" type="text" id="T2" /><span id="L3"><br />Item3 Initial CSF <br /></span><input name="T3" type="text" id="T3" /><span id="L4"><br />Item4 Location <br /></span><input name="T4" type="text" id="T4" /><span id="L5"><br />Item5 Consultant <br /></span><input name="T5" type="text" id="T5" /></form>
once the user fills the form and click insert, i want to save entered text into a other tabel in the database using this code. it does not work. can u tell me where my mistake is?
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mydb As New OleDbConnection
mydb =
New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= |datadirectory|openclinica.mdb;Persist Security Info=True")
mydb.Open()
Dim Y As Integer = Session("Itemcount")
For M = 1 To Y
Session("FUt") = "L" + M.ToString
Session("FUDt") = "T" + M.ToString
Dim sqlstring = "INSERT INTO [Followup] ([MRN], [FU], [FUData]) VALUES (@MRNtxt, @" + Session("FUt") + ", @" + Session("FUDt") + ");"
Dim mydbcommand As New OleDbCommand(sqlstring, mydb)
mydbcommand.Parameters.Add("@MRNtxt", OleDbType.VarChar).Value = MRNtxt.Text
mydbcommand.Parameters.Add("@" + Session("FUt"), OleDbType.VarChar).Value = Session("FUt").Text
mydbcommand.Parameters.Add("@" + Session("FUDt"), OleDbType.VarChar).Value = Session("FUDt").Text
mydbcommand.ExecuteNonQuery()
Next
mydb.Close()
End Sub
the error message looks like this when i click "insert"
You need to retrieve the actual controls that are created in button1_click instead of their ids that you store in the session state.
However, because these controls are dynamically created at runtime, your code will become substantially more complex because you will have to add all of the controls to the page every time Page Init is called. See the following articles for additional information:
ASP.NET Page Life Cycle Overview
View State and Dynamically Added Controls
Dynamic Web Controls, Postbacks, and View State
Based on the complexity that this introduces, I would suggest a more straightforward approach where you have a number of labels and text boxes already on the page, but hidden by CSS attributes (display: none; visibility: hidden
).
An even better approach is to use a grid control, which is ideally suited to this kind of behavior. There are plenty of examples here:
Table of Contents: GridView Examples for ASP.NET 2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With