I have some TextBox in ItemTemplate. It allow users to edit multiple rows.
'Button1' can update one row. I want to click all of it in one page using a button outside 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>
I am looking for some thing like this:
For i = 0 To Me.GridView1.Rows.Count - 1
SqlDataSource1.Update(i) //pseudo
Next
I want to reuse UpdateCommand
in SqlDataSource1
.
Create an update function or subroutine then call it as you needed. Something like this
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
That is a reusable function for executing non-query command.
I just known how to do it. I answer my own question. It works.
.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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.