ProtectedSub GridView1_RowCreated()Sub GridView1_RowCreated(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated If (e.Row.RowType = DataControlRowType.DataRow) Then Dim bt1 As Button =CType(e.Row.FindControl("cart"), Button) bt1.CommandArgument = e.Row.RowIndex.ToString Dim bt2 As Button =CType(e.Row.FindControl("favorite"), Button) bt2.CommandArgument = e.Row.RowIndex.ToString EndIf End Sub
接着写Gridview_rowcommand事件:
If e.CommandName ="cart"Then Dim index AsInteger= Convert.ToInt32(e.CommandArgument.ToString().Trim()) MsgBox(index) //取得当前的行号 Dim label1 As Label =CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label) Dim id AsInteger=CType(label1.Text.ToString, Integer) Dim label2 As Label =CType(Me.GridView1.Rows(index).FindControl("lblproname"), Label) Dim produtname AsString= label2.Text.ToString Dim price AsDecimal=CDec(Me.GridView1.Rows(index).Cells(5).Text.ToString) //以上各行为根据行号取得各栏们的值 Else Dim index AsInteger= Convert.ToInt32(e.CommandArgument.ToString().Trim()) Dim label1 As Label =CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label) Dim id AsInteger=CType(label1.Text.ToString, Integer) //同上 EndIf
在点击自定义的Button时引发的Gridview_rowcommand事件 首先需要做一下Gridview_rowcreated事件: Protected Sub GridView1_RowCreated()Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.Gri