I am working with C# environment and I have the below code that I tested and it works under 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
So, I try to convert it to C# language and couldn’t get it to work.
-
There is no “handles” option in C#
-
Somehow, “
e.Row.Attributes("onclick")” works in VB, but not in 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); } }
Change it to this:
in C# arrays are called with brackets not parantheses. Also, we use plus signs instead of ampersands to concatenate strings.
You could also do this (to match the code directly above it):