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 に答える