![](/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.