如何动态生成文本框并收集用户输入的数据?

时间:2022-01-07 13:19:57

I will appreciate answers in VB or C#

我将欣赏VB或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")

我使用此代码从数据库中的表生成标签和文本框(当用户点击按钮“加载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"

当我点击“插入”时,错误消息看起来像这样

如何动态生成文本框并收集用户输入的数据?

4 个解决方案

#1


2  

You need to retrieve the actual controls that are created in button1_click instead of their ids that you store in the session state.

您需要检索在button1_click中创建的实际控件,而不是它们存储在会话状态中的ID。

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:

但是,因为这些控件是在运行时动态创建的,所以您的代码将变得非常复杂,因为每次调用Page Init时都必须将所有控件添加到页面中。有关其他信息,请参阅以下文章:

ASP.NET Page Life Cycle Overview

ASP.NET页面生命周期概述

View State and Dynamically Added Controls

查看状态和动态添加的控件

Dynamic Web Controls, Postbacks, and View State

动态Web控件,回发和视图状态

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).

基于这引入的复杂性,我建议采用一种更简单的方法,在页面上已经有许多标签和文本框,但是CSS属性隐藏了(显示:无;可见性:隐藏)。

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

目录:ASP.NET 2.0的GridView示例

#2


2  

Please Check this article

请查看这篇文章

Creating Dynamic User Friendly Web-Based Data Collection Forms

创建动态用户友好的基于Web的数据收集表单

#3


0  

It is your ... = Session("FUt").Text and ... = Session("FUDt").Text.

这是你的... =会话(“FUt”)。文本和... =会话(“FUDt”)。文本。

These are strings, and the .Text is attempting to call a property that does not exist.

这些是字符串,而.Text正在尝试调用不存在的属性。

#4


0  

Where is Session defined? Have you checked access level of the Text property (it needs to be public otherwise the exception you are getting will be thrown)?

会话定义在哪里?您是否检查了Text属性的访问级别(它需要公开,否则将引发您获得的异常)?

#1


2  

You need to retrieve the actual controls that are created in button1_click instead of their ids that you store in the session state.

您需要检索在button1_click中创建的实际控件,而不是它们存储在会话状态中的ID。

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:

但是,因为这些控件是在运行时动态创建的,所以您的代码将变得非常复杂,因为每次调用Page Init时都必须将所有控件添加到页面中。有关其他信息,请参阅以下文章:

ASP.NET Page Life Cycle Overview

ASP.NET页面生命周期概述

View State and Dynamically Added Controls

查看状态和动态添加的控件

Dynamic Web Controls, Postbacks, and View State

动态Web控件,回发和视图状态

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).

基于这引入的复杂性,我建议采用一种更简单的方法,在页面上已经有许多标签和文本框,但是CSS属性隐藏了(显示:无;可见性:隐藏)。

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

目录:ASP.NET 2.0的GridView示例

#2


2  

Please Check this article

请查看这篇文章

Creating Dynamic User Friendly Web-Based Data Collection Forms

创建动态用户友好的基于Web的数据收集表单

#3


0  

It is your ... = Session("FUt").Text and ... = Session("FUDt").Text.

这是你的... =会话(“FUt”)。文本和... =会话(“FUDt”)。文本。

These are strings, and the .Text is attempting to call a property that does not exist.

这些是字符串,而.Text正在尝试调用不存在的属性。

#4


0  

Where is Session defined? Have you checked access level of the Text property (it needs to be public otherwise the exception you are getting will be thrown)?

会话定义在哪里?您是否检查了Text属性的访问级别(它需要公开,否则将引发您获得的异常)?