最佳解答者
如何用Button去抓取GridView中相同欄位總數 ASP.NET VB

問題
-
想請教各位豪傑
我想利用Button去抓取GridView中相同欄位總數,並且放到GridView外的Label52.Text
以下是程式碼-----------
Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
Dim I As IntegerFor I = 0 To Me.GridView14.Rows.Count - 1
Dim t As String = ""
If CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton1"), RadioButton).Checked Then
t = 1
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=red>NO</fong>"
End If
End IfIf CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton2"), RadioButton).Checked Then
t = 2
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=red>NO</fong>"
End If
End If
If CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton3"), RadioButton).Checked Then
t = 3
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=red>NO</fong>"
End If
End If
If CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton4"), RadioButton).Checked Then
t = 4
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=red>NO</fong>"
End If
End IfDim k As Integer
Dim summary As Integer = 0For k = 0 To CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "OK"
summary += Me.GridView14.Rows(k).TableSection.ToString.CountNext
Label52.Text = summary
Next
End Sub
- 已編輯 Shih_Fong 2013年2月1日 下午 03:06
2013年2月1日 下午 03:02
解答
-
請將程式改為如下
Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
Dim I As Integer
Dim summary As Integer = 0
For I = 0 To Me.GridView14.Rows.Count - 1Dim t As String = ""
If CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton1"), RadioButton).Checked Then
t = 1
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"
summary += CInt(1)
Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text += "<font color=red>NO</fong>"
End If
End IfIf CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton2"), RadioButton).Checked Then
t = 2
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"
summary += CInt(1)
Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text += "<font color=red>NO</fong>"
End If
End If
If CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton3"), RadioButton).Checked Then
t = 3
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"
summary += CInt(1)
Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text += "<font color=red>NO</fong>"End If
End IfIf CType(GridView14.Rows(I).Cells(2).FindControl("RadioButton4"), RadioButton).Checked Then
t = 4
If CType(GridView14.Rows(I).Cells(3).FindControl("Label1"), Label).Text.Trim = t Then
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text = "<font color=darkgreen>OK</fong>"
summary += CInt(1)
Else
CType(GridView14.Rows(I).Cells(4).FindControl("Label51"), Label).Text += "<font color=red>NO</fong>"
End If
End If
NextLabel52.Text = "共答對" & summary & "題/" & Me.GridView14.Rows.Count
End Sub- 已標示為解答 Shih_Fong 2013年2月2日 下午 02:43
2013年2月2日 下午 02:42