![](/img/trans.png)
[英]How to display details about specific record on a same page in ASP.net MVC
[英]ASP.NET Gridview use 'onRowClick' to display record's Details (instead of Details/Select Button)
好了,我有一个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”,本质上显示了自定义的“ Details View”选择的记录。
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转到并单击详细信息按钮)或从后面的代码中执行此操作,我只是还没有找到适合我的解决方案。 有什么想法吗?
检查这个例子:
<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
希望对您有所帮助。
好吧,我设法解决了这个简单但棘手的任务。 最后,我确实在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您的示例同样是一个有趣的示例,尽管不适用于我当前的代码(以及我的回发)
感谢你们的指导!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.