单击整行的任何部分,触发网格视图的选定索引更改事件

G.S*_*gal 2 javascript asp.net gridview

我想在单击整行的任何部分时触发网格视图的选定索引更改事件

这就像我不想显示一个选择命令而不是用户可以单击行的任何部分意味着行的任何列和该行被选中

非常感谢

Tim*_*ter 5

你没有提供语言,所以我将在VB.NET中展示一个例子(易于转换为C#):

以下列方式处理GridView 的RowCreated事件:

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
            e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
            e.Row.ToolTip = "Click to select row"
            e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(DirectCast(sender,GridView), "Select$" & e.Row.RowIndex)
    End Select
End Sub
Run Code Online (Sandbox Code Playgroud)

重要的是:

e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(DirectCast(sender, GridView), "Select$" & e.Row.RowIndex)
Run Code Online (Sandbox Code Playgroud)

C#

e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, "Select$" + e.Row.RowIndex) 
Run Code Online (Sandbox Code Playgroud)