在gridview中使用带有文本框和下拉列表框控件的vb.net.crating动态行,但是当我单击添加新行按钮时,我无法创建,并且该行中的值也未保存在后端。在这里,我附上了我的代码供你参考。
designing code
-------------
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" Width ="100%">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Origin country">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat ="server">
<asp:ListItem ></asp:ListItem>
<asp:ListItem >India</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Origin City">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Destination Country">
<ItemTemplate>
<asp:DropDownList ID="ddl2" runat ="server" >
<asp:ListItem ></asp:ListItem>
<asp:ListItem >India</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Destination City">
<ItemTemplate>
<asp:TextBox ID="TextBox21" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<%--<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick = "ButtonAdd_click"/>--%>
<asp:LinkButton ID="lnk1" runat ="server" Text ="Add new Row" ></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
code
-----
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Private Sub SetInitialRow()
Dim dt As DataTable = New DataTable
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("RowNumber", GetType(System.String)))
dt.Columns.Add(New DataColumn("Column1", GetType(System.String)))
dt.Columns.Add(New DataColumn("Column2", GetType(System.String)))
dt.Columns.Add(New DataColumn("Column3", GetType(System.String)))
dt.Columns.Add(New DataColumn("column4", GetType(System.String)))
dr = dt.NewRow
dr("RowNumber") = 1
dr("Column1") = String.Empty
dr("Column2") = String.Empty
dr("Column3") = String.Empty
dr("column4") = String.Empty
dt.Rows.Add(dr)
'dr = dt.NewRow();
'Store the DataTable in ViewState
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End Sub
Protected Sub lnk1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim rowIndex As Integer = 0
Dim sc As StringCollection = New StringCollection
If (Not (ViewState("CurrentTable")) Is Nothing) Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
If (dtCurrentTable.Rows.Count > 0) Then
Dim i As Integer = 1
Do While (i <= dtCurrentTable.Rows.Count)
'extract the TextBox values
Dim box1 As DropDownList = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("ddl1"), DropDownList)
Dim box2 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
Dim box3 As DropDownList = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("ddl2"), DropDownList)
Dim box4 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("TextBox21"), TextBox)
'get the values from the TextBoxes
'then add it to the collections with a comma "," as the delimited values
'sc.Add((box1.Text + ("," + (box2.Text + ("," + box3.Text)))))
sc.Add((box1.Text + ("," + (box2.Text + ("," + box3.Text + ("," + box4.Text))))))
rowIndex = (rowIndex + 1)
i = (i + 1)
Loop
'Call the method for executing inserts
InsertRecords(sc)
End If
End If
End Sub
Private Sub InsertRecords(ByVal sc As StringCollection)
Dim str As String = "server=local host; database=traveldesk; username=root; password=test123;"
Dim conn As MySqlConnection = New MySqlConnection(Str)
Dim sb As StringBuilder = New StringBuilder(String.Empty)
Dim splitItems() As String = Nothing
For Each item As String In sc
Const sqlStatement As String = "INSERT INTO tbl_testing (Col1,Col2,Col3,Col4) VALUES"
If item.Contains(",") Then
splitItems = item.Split(",".ToCharArray)
sb.AppendFormat("{0}('{1}','{2}','{3}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2))
End If
Next
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand(sb.ToString, conn)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
'Display a popup which indicates that the record was successfully inserted
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert Error:"
msg = (msg + ex.Message)
Throw New Exception(msg)
Finally
conn.Close()
End Try
答案 0 :(得分:0)
我已经更改了插入记录子程序,如下所示....
问题出在你的stringbuilder.appendformat语句
中'Dim str As String = "server=local host; database=traveldesk; username=root; password=test123;"
Dim conn As System.Data.SqlClient.SqlConnection
Dim sb As StringBuilder = New StringBuilder(String.Empty)
Dim splitItems() As String = Nothing
Const sqlStatement As String = "INSERT INTO tbl_testing (Col1,Col2,Col3,Col4) VALUES"
conn = New System.Data.SqlClient.SqlConnection
conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\Workspace\App_Data\Database.mdf;Integrated Security=True;User Instance=True"
For Each item As String In sc
If item.Contains(",") Then
splitItems = item.Split(",".ToCharArray)
sb.AppendFormat("{0} ('{1}', '{2}', '{3}', '{4}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3))
End If
Next
Try
conn.Open()
Dim cmd As System.Data.SqlClient.SqlCommand
cmd = New System.Data.SqlClient.SqlCommand(sb.ToString, conn)
cmd.ExecuteNonQuery()
'Display a popup which indicates that the record was successfully inserted
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert Error:"
msg = (msg + ex.Message)
Throw New Exception(msg)
Finally
conn.Close()
End Try
我还修改了sql-connection和sql-command参数,使其与我创建的测试db一起使用,因此您必须替换旧的sql-connection和sql-command参数。
此致