gridview中备用行的颜色

时间:2012-10-04 19:20:18

标签: c# asp.net migration

我正在使用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#语言,但无法使其正常工作。

  1. C#

  2. 中没有“句柄”选项
  3. 不知何故,“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);
        }
    }
    

1 个答案:

答案 0 :(得分:2)

将其更改为:

e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
C#数组中的

是用括号而不是parantheses调用的。另外,我们使用加号而不是&符号来连接字符串。

您也可以这样做(以匹配其正上方的代码):

e.Row.Attributes.add("onclick", this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex));