Parent-Child Gridview in ASP.net
I have two GridView in ASP.net 3.5 page. I have HyperLink field as one of the fields in the First GridView.
On click of this hyperlink I need to call display the 2nd grid by passing some values to开发者_如何转开发 a method showAllRecords(value from hyperlink)
How do I do this?
Thanks
You can try a TemplateField like this for GridView1 (primary GridView)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName" CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in GridView1's RowCommand, you can get the CommandArgument and setup the DataSource for GridView2 (child GridView).
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName = "cmdName")
{
var arg = e.CommandArgument;
// use arg to filter GridView2's DataSource
GridView2.DataSource = FilteredDataSource;
GridView2.DataBind();
// show GridView2 if it's hidden.
}
}
Maybe the following blog post could give you a hint;
http://www.tugberkugurlu.com/archive/parent-child-view-in-a-single-table-with-gridview-control-on-asp-net-web-forms
First you need to handle SelectedIndexChanged event on the first grid and then get the value from the hyperlink. Is the hyperlink a DataKey? If it is then you get it by GridOne.SelectedDataKey.Values["key"]
otherwise get the actual cell by valuefromGridOne = GridOne.SelectedRow.Cells[num].Text
where number is the cell number. Once you have it you can pass the value to your second grid by handling Selecting event of the objectDataSource (assuming you use it to bind data) and passing the value like this e.InputParameters["dataKey"] = valuefromGridOne;
精彩评论