有2个复选框列表,每个复选框里面有6个复选框(下面是我的数据库)
checkboxlist1:id,rowa
checkboxlist2:id,rowb
id : int
rowa : nvarchar(250)
rowb : nvarchar(250)
当我单击一个复选框时,该值将作为A1,A2保存到我的数据库....这是我的代码:
Dim str1 As [String] = ""
For a As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(a).Selected Then
If str1 = "" Then
str1 = CheckBoxList1.Items(a).Text
Else
str1 += "," + CheckBoxList1.Items(a).Text
str1 = ""
End If
End If
Next
For a1 As Integer = 0 To CheckBoxList1.Items.Count - 10
CheckBoxList1.Enabled = False
Next
Dim str2 As [String] = ""
For b As Integer = 0 To CheckBoxList2.Items.Count - 1
If CheckBoxList2.Items(b).Selected Then
If str2 = "" Then
str2 = CheckBoxList2.Items(b).Text
Else
str2 += "," + CheckBoxList2.Items(b).Text
End If
End If
Next
Dim connectionString As String = ConfigurationManager.ConnectionStrings( “的ConnectionString”)。的ConnectionString Dim insertSql As String =“INSERT INTO tbtest(rowa,rowb)VALUES(@ rowa,@ rowb)” 使用myConnection作为新的SqlConnection(connectionString) myConnection.Open() 将myCommand调暗为新的SqlCommand(insertSql,myConnection) myCommand.Parameters.AddWithValue(“@ rowa”,str1) myCommand.Parameters.AddWithValue(“@ rowb”,str2)
myCommand.ExecuteNonQuery() myConnection.Close() 结束使用
的Page_Load
Dim connectionString As String =
使用myConnection作为新的SqlConnection(connectionString)
Dim objCmd_team As SqlCommand = New SqlCommand(“SELECT [rowa],[rowb] FROM [tbtest]”,myConnection) myConnection.Open() Dim objReader As SqlDataReader = objCmd_team.ExecuteReader()While (objReader.Read()) Dim currentCheckBox As ListItem = checkboxlist1.Items.FindByText(objReader("rowa")) If currentCheckBox IsNot Nothing Then currentCheckBox.Selected = True End If Dim currentCheckBox1 As ListItem = checkboxlist2.Items.FindByText(objReader("rowb")) If currentCheckBox1 IsNot Nothing Then currentCheckBox1.Selected = True End If End While End Using