1

我正在使用 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. 不知何故,“ 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);
        }
    }
    
4

1 回答 1

2

将其更改为:

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

在 C# 中,数组是用括号而不是括号调用的。此外,我们使用加号而不是 & 号来连接字符串。

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

e.Row.Attributes.add("onclick", this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex));
于 2012-10-04T19:29:00.393 回答