【问题标题】:ASP.NET Gridview use 'onRowClick' to display record's Details (instead of Details/Select Button)ASP.NET Gridview 使用“onRowClick”来显示记录的详细信息(而不是详细信息/选择按钮)
【发布时间】:2015-08-24 14:46:14
【问题描述】:

好的,我有一个gridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     RowStyle-CssClass="td" HeaderStyle-CssClass="th"
    CellPadding="6" DataKeyNames="ID" ShowFooter="true">

     <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Date" HeaderText="Date" />
        ......
         <asp:TemplateField>
           <ItemTemplate>
             <asp:Button ID="lnkDetails" runat="server" Text="Details" OnClick="DetailsView" CommandName="DetailsCommand"></asp:Button>
           </ItemTemplate>
        </asp:TemplateField>
     </Columns>

如您所见,我已配置“lnkDetails”按钮,以便在单击时触发 DetailsView() 函数,该函数 然后调用“SqlCommand”并将数据绑定到“asp Repeater”,本质上显示所选记录的自定义“详细信息视图”。

 Protected Sub DetailsView(ByVal sender As Object, ByVal e As EventArgs)

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = ConnectionString() 'Thats a function
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SELECT FROM... WHERE ID=" & id & ""

        ....

        Repeater1.DataSource = cmd.ExecuteReader()
        Repeater1.DataBind()
 End Sub

相当直接,对吧?

现在我要做的(UI 方面)是允许用户通过选择记录的(整个)行而不是详细信息按钮来触发该单击事件。 然后我想隐藏它。

我很确定您可以从带有 jquery 的 .aspx 页面(onRowClick 转到并单击详细信息按钮)或从后面的代码中做到这一点,我只是还没有找到适合我的解决方案。 有什么想法吗?

【问题讨论】:

  • That answer 似乎是你要找的东西
  • 谢谢,实现了。我现在该如何调用我的 DetailsView(ByVal sender As Object, ByVal e As EventArgs) 函数或从 _SelectedIndexChanged 内部单击所述按钮?
  • 嗯,我的意思不是“逐字执行该链接上的任何内容”。您的 DetailsView 本身就是一个很好的事件处理程序(虽然不是最好的名称),只需使用它来订阅索引更改事件
  • @Satindersingh 这也是一个很酷的效果,如果您正在为您的详细信息查看数据进行“SQL 查询”,则认为它不可行

标签: jquery asp.net vb.net gridview webforms


【解决方案1】:

检查这个例子:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:ButtonField CommandName="click" Text="Click" Visible="False" />
                <asp:BoundField DataField="IDcontato" HeaderText="ID" />
                <asp:BoundField DataField="nome" HeaderText="Name" />
            </Columns>
            <SelectedRowStyle BackColor="#FFFF66" Font-Bold="True" />
        </asp:GridView>

创建按钮字段并将其可见性设置为 false。 在后面的代码上,你可以这样做:

  Protected Overrides Sub Render(writer As HtmlTextWriter)
        For Each row As GridViewRow In GridView1.Rows
            'You have register the events so it wont fire any event validation errors on rutime
            If row.RowType = DataControlRowType.DataRow Then
                Page.ClientScript.RegisterForEventValidation(row.UniqueID & "$ctl00")
            End If
        Next

        MyBase.Render(writer)
    End Sub

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        'Capture the event and do what you want
        Dim _commandName As String = e.CommandName

            Select (_commandName)
            'filter by command name, so you can have multiple events for each row
            Case ("click")
                'do something
                Dim _gridView As GridView = CType(sender, GridView)
                Dim _Index As Integer = e.CommandArgument.ToString()
                _gridView.SelectedIndex = _Index


        End Select

    End Sub

    Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState <> DataControlRowState.Selected Then
            'Set the apropriate css for the rows
            e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';")
            e.Row.Attributes.Add("onmouseout", "this.style.cursor='pointer';")
        End If
    End Sub


    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        'Capture the button event and set it for the entire row
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim _ClickButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
            e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(_ClickButton, "")

        End If

    End Sub

希望对你有帮助。

【讨论】:

    【解决方案2】:

    好吧,我设法解决了这个简单但繁琐的任务。 最后,我确实在 Andrei 提到的另一篇文章中重新使用了解决方案。这是我想出的:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()))
        End If
    End Sub
    
    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
        Try
            FillDetailsView(GridView1.SelectedIndex)
        Catch
            '...
        End Try
    End Sub
    
    
    Protected Sub FillDetailsView(RecordIndex)
    
        Dim id = GridView1.DataKeys(RecordIndex).Value
    
        'By passing the Row Index here and using the GridView's DataKey which is pointing to the record's ID field
        'I am ready to go to work
    
    End Sub
    

    Felipe your's 也是一个有趣的例子,虽然不太适用于我当前的代码(..和我的回发)

    感谢大家的指点!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多