[英]How to click all update buttons in a GridView programmatically using vb.net?
我在 ItemTemplate 中有一些 TextBox。 它允許用戶編輯多行。
'Button1' 可以更新一行。 我想使用 GridView 之外的按鈕在一頁中單擊所有內容。
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>' visible="false"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("something") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" CommandName="Update"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ApplicationServices%>'
UpdateCommand="...">
</asp:SqlDataSource>
我正在尋找這樣的東西:
For i = 0 To Me.GridView1.Rows.Count - 1
SqlDataSource1.Update(i) //pseudo
Next
我想在SqlDataSource1
中重用UpdateCommand
。
創建更新 function 或子程序,然后根據需要調用它。 像這樣的東西
Public Function ExecuteNonQuery(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Integer
Dim connection As SqlConnection = Nothing
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Integer = -1
Try
connection = New SqlConnection(ConnectionString)
command = New SqlCommand(cmd, connection)
command.CommandType = cmdType
Me.AssignParameters(command, parameters)
connection.Open()
transaction = connection.BeginTransaction()
command.Transaction = transaction
res = command.ExecuteNonQuery()
transaction.Commit()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Throw New SqlDbException(ex.Message, ex.InnerException)
Finally
If Not (connection Is Nothing) AndAlso (connection.State = ConnectionState.Open) Then connection.Close()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
那是一個可重用的 function 用於執行非查詢命令。
我只是知道該怎么做。 我回答我自己的問題。 有用。
.aspx
UpdateCommand="..." //SQL Update Command
<UpdateParameters>
//Some Parameters
</UpdateParameters>
.aspx.vb
For i = 0 To Me.GridView1.Rows.Count - 1
Me.GridView1.UpdateRow(i, True)
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.