Asp.net 将动态文本框中的文本插入数据库
我使用下面的代码创建了一个文本框控件数组,其中控件的数量是根据从组合框中选择的值的基础生成的Asp.net 将动态文本框中的文本插入数据库,asp.net,Asp.net,我使用下面的代码创建了一个文本框控件数组,其中控件的数量是根据从组合框中选择的值的基础生成的 Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged Dim lstTextBox = New List(Of TextBox)()
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim lstTextBox = New List(Of TextBox)()
For i As Integer = 0 To DropDownList1.SelectedValue
for j as integer =0 to 3
Dim txtbx As New TextBox()
txtbx.ID = String.Format("txtbx{0}", j)
lstTextBox.Add(txtbx)
txtbx.Text = "sample text " & j
form1.Controls.Add(txtbx)
next
Next
Session("lstTextBox") = lstTextBox
End Sub
现在我需要通过单击一个按钮将值插入数据库,代码如下
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dim mysql as string
mysql="insert into mytable values("
Dim lstText = New List(Of TextBox)()
lstText = Session("lstTextBox")
For i As Integer = 0 To lstText.Count - 1
for j as integer =0 to 3
mysql= mysql & "'" & trim(lstText(j).Text) & "',"
next
'使用mysql字符串为每行执行insert
下一个
端接头
现在的问题是:只有文本框中的初始值被插入到数据库中,我在文本框中所做的更改不会被插入到数据库中。即,在会话中,文本框中的文本更改不会改变初始值
我需要一个解决这个问题的方法来继续我的工作。因此,请各位专家注意。。如有积极回应,将不胜感激 我不太清楚VB和我的Sql。但我可以给你一个方法,让你从会议中解脱出来 在DropDownList1\u SelectedIndexChanged中尝试这样做
if(Session("lstTextBox"))==null
Session("lstTextBox") = lstTextBox
else
Session.Abandon()
Session("lstTextBox") = lstTextBox
现在您可以检查会话是否正在更新。你也可以得到你想要的结果。请告诉我输出。会话是在生成控件时生成的,因此会话变量保存文本框的初始值。如果我在文本框中做了任何更改都不会影响会话变量,那么问题就出在会话变量的定义位置?。根据您的代码,会话在DropDownList1\u SelectedIndexChanged上定义。如果这是真的,那么你可以继续我的回答。初始文本框值将在放弃阶段清除。因此,文本框值将根据需要更新。