Another version with SQLDataSource and dynamic generated textbox.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="dynamicTextBoxVB.aspx.vb" Inherits="dynamicTextBoxVB" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"
SelectCommand="SELECT [ID], [Name] FROM [NameForTextBox]" DataSourceMode="DataReader"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"
DeleteCommand="DELETE FROM [NameForTextBoxForInsert] WHERE [ID] = @ID" InsertCommand="INSERT INTO [NameForTextBoxForInsert] ([fromLabel], [fromTextBox]) VALUES (@fromLabel, @fromTextBox)"
SelectCommand="SELECT [ID], [fromLabel], [fromTextBox] FROM [NameForTextBoxForInsert]"
UpdateCommand="UPDATE [NameForTextBoxForInsert] SET [fromLabel] = @fromLabel, [fromTextBox] = @fromTextBox WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="fromLabel" Type="String" />
<asp:Parameter Name="fromTextBox" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="fromLabel" Type="String" />
<asp:Parameter Name="fromTextBox" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="fromLabel" HeaderText="fromLabel" SortExpression="fromLabel" />
<asp:BoundField DataField="fromTextBox" HeaderText="fromTextBox" SortExpression="fromTextBox" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert data from Textbox" />
</div>
</form>
</body>
</html> Imports System.Data.SqlClient
Imports System.Data
Imports System
Partial Class dynamicTextBoxVB
Inherits System.Web.UI.Page
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
'generate all textbox
myfunctionAddTextBox()
MyBase.OnInit(e)
End Sub
Protected Sub myfunctionAddTextBox()
Dim ctrlTable1 As New Table()
ctrlTable1.CellPadding = 0
ctrlTable1.CellSpacing = 1
ctrlTable1.Attributes.Add("Border", "1")
Dim reader As System.Data.SqlClient.SqlDataReader = DirectCast(SqlDataSource1.[Select](DataSourceSelectArguments.Empty), System.Data.SqlClient.SqlDataReader)
' start
While reader.Read()
Dim tr As New TableRow()
Dim tc1 As New TableCell()
Dim lbl1 As New Label()
lbl1.Attributes.Add("runat", "Server")
lbl1.EnableViewState = True
lbl1.Width = 20
lbl1.ID = "lbl" + reader(0).ToString()
lbl1.Visible = True
lbl1.Text = reader(1).ToString()
'0:ID; 1:Name;
tc1.Controls.Add(lbl1)
tr.Cells.Add(tc1)
Dim tc2 As New TableCell()
Dim tb2 As New TextBox()
tb2.Attributes.Add("runat", "Server")
' tb2.EnableViewState = true;
tb2.Width = 80
tb2.ID = "txt" + reader(0).ToString()
'tb2.ReadOnly = true;
tb2.Visible = True
tc2.Controls.Add(tb2)
tr.Cells.Add(tc2)
ctrlTable1.Rows.Add(tr)
End While
Me.Form.Controls.Add(ctrlTable1)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim reader As SqlDataReader = DirectCast(SqlDataSource1.[Select](DataSourceSelectArguments.Empty), SqlDataReader)
While reader.Read()
Dim strLabel As String = ""
Dim strTextBox As String = ""
Dim myLabel As New Label()
Dim myLabelId As String = "lbl" + reader(0).ToString()
Dim txtTextBox As New TextBox()
Dim myTxtId As String = "txt" + reader(0).ToString()
txtTextBox = DirectCast(Me.FindControl(myTxtId), TextBox)
If txtTextBox IsNot Nothing Then
strTextBox = txtTextBox.Text.ToString()
Else
strTextBox = ""
End If
myLabel = DirectCast(Me.FindControl(myLabelId), Label)
If myLabel IsNot Nothing Then
strLabel = myLabel.Text.ToString()
Else
strLabel = ""
End If
If strLabel <> "" Then
SqlDataSource2.InsertParameters("fromLabel").DefaultValue = strLabel
SqlDataSource2.InsertParameters("fromTextBox").DefaultValue = strTextBox
'SqlDataSource2.InsertParameters["createdBy"].DefaultValue = User.Identity.Name;
'SqlDataSource2.InsertParameters["createdDate"].DefaultValue = DateTime.Now.ToString();
'add data to table2
SqlDataSource2.Insert()
txtTextBox.Text = ""
End If
End While
' Close the reader
reader.Close()
End Sub
End Class
Tables:
CREATE
TABLE [dbo].[NameForTextBoxForInsert]([ID] [int]
IDENTITY(1,1) NOT NULL,[fromLabel] [nvarchar]
(50) NULL,[fromTextBox] [nvarchar]
(50) NULL)
CREATE
TABLE [dbo].[NameForTextBox]([ID] [int]
NULL,[Name] [nvarchar](50) NULL)