我正在使用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#语言,但无法使其正常工作。
C#
不知何故,“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);
}
}
答案 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));