在点击自定义的Button时引发的Gridview_rowcommand事件
首先需要做一下Gridview_rowcreated事件:
Protected
Sub GridView1_RowCreated()
Sub GridView1_RowCreated(ByVal sender As Object, 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
End If
End Sub
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
End If
End Sub
接着写Gridview_rowcommand事件:
If
e.CommandName
=
"
cart
"
Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
MsgBox (index) // 取得当前的行号
Dim label1 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproid " ), Label)
Dim id As Integer = CType (label1.Text.ToString, Integer )
Dim label2 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproname " ), Label)
Dim produtname As String = label2.Text.ToString
Dim price As Decimal = CDec ( Me .GridView1.Rows(index).Cells( 5 ).Text.ToString)
// 以上各行为根据行号取得各栏们的值
Else
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
Dim label1 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproid " ), Label)
Dim id As Integer = CType (label1.Text.ToString, Integer )
// 同上
End If
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
MsgBox (index) // 取得当前的行号
Dim label1 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproid " ), Label)
Dim id As Integer = CType (label1.Text.ToString, Integer )
Dim label2 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproname " ), Label)
Dim produtname As String = label2.Text.ToString
Dim price As Decimal = CDec ( Me .GridView1.Rows(index).Cells( 5 ).Text.ToString)
// 以上各行为根据行号取得各栏们的值
Else
Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
Dim label1 As Label = CType ( Me .GridView1.Rows(index).FindControl( " lblproid " ), Label)
Dim id As Integer = CType (label1.Text.ToString, Integer )
// 同上
End If
就这个问题,研究了好久呢,终于出来了!