我正在使用 C# 环境,我测试了以下代码,它在 Visual Basic .Net 下工作:
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
if e.Row.RowType = DataControlRowType.DataRow then
Dim RowNum as Integer
RowNum = e.Row.RowIndex
if RowNum mod 2 = 1 then
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'")
else
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'")
end if
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='DeepSkyBlue'")
e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
因此,我尝试将其转换为 C# 语言,但无法正常工作。
C# 中没有“句柄”选项
不知何故,“
e.Row.Attributes("onclick")
”在 VB 中有效,但在 C# 中无效private void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int RowNum = e.Row.RowIndex; if (RowNum % 2 == 1) { e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'"); } else { e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'"); } e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='DeepSkyBlue'"); e.Row.Attributes("onclick") = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" & e.Row.RowIndex); } }