<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName ="111" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" CommandName ="222" />
</ItemTemplate>
</asp:TemplateField>
Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
Dim index As Integer = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow).RowIndex
If e.CommandName = "111" Then
Response.Write(gv.Rows(index).Cells(1).Text)
End If
If e.CommandName = "222" Then
Response.Redirect("1.aspx")
End If
End Sub
方法二:
以下的Eval("ID")為GridView中的DataKeyNames
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName ="111" CommandArgument ='<%# Eval("ID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" CommandName ="222" CommandArgument ='<%# Eval("ID") %>'/>
</ItemTemplate>
</asp:TemplateField>
Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
If e.CommandName = "111" Then
Response.Write(e.CommandArgument)
Response.Write(gv.Rows(e.CommandArgument).Cells(1).Text)
End If
If e.CommandName = "222" Then
Response.Redirect("1.aspx")
End If
End Sub
沒有留言:
張貼留言