我在 Visual Basic 中有这段代码,当我测试它时它可以工作。所有行都有交替的颜色。
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#:
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);
}
}
这是我可以做的与 Visual Basic 中的“句柄”选项相同的方法吗?如果可能的话,请给我代码。