简体   繁体   English

asp.net嵌套的gridview模板控件

[英]asp.net nested gridview template controls

I have a gridview inside a gridview's "ItemTemplate". 我在gridview的“ ItemTemplate”中有一个gridview。 The outside gridview, (gvPOs) , has a "select" option. 外部gridview(gvPOs)具有“选择”选项。 Since my sql data source is handled through code behind, not sqldatasource through design mode, there's no "enable select" , "enable delete", etc. So I added select under "edit columns" as a CommandField. 由于我的sql数据源是通过后面的代码处理的,而不是通过设计模式通过sqldatasource处理的,因此没有“启用选择”,“启用删除”等。因此,我在“编辑列”下添加了select作为CommandField。 Usually, when a gridview is by itself , and I make a formview using the table data to display that information, I can just fill in the where clause to display where ID is from gvPOs.SelectedValue control and it would show that specific line. 通常,当gridview本身是一个,而我使用表数据显示一个formview来显示该信息时,我只需填写where子句以显示ID来自gvPOs.SelectedValue控件的位置,它将显示该特定行。 But in this case, since there's a nested gridview, for some reason when I do that, "select" doesn't do anything. 但是在这种情况下,由于存在嵌套的gridview,出于某种原因,当我这样做时,“选择”不会执行任何操作。

Output http://i57.tinypic.com/14ue0kg.png 输出http://i57.tinypic.com/14ue0kg.png

I'm trying to get the select on the far right column of the outside gridview to open up my formview that corresponds to the PO_ID. 我正在尝试在外部gridview的最右列上进行选择,以打开与PO_ID相对应的formview。

The left side is just to show what else it's doing. 左侧只是为了显示它在做什么。 When the expand button is clicked on the left, the 2nd gridview pops up with the records that have that PO_ID. 单击左侧的扩展按钮时,第二个网格视图会弹出,其中包含具有该PO_ID的记录。 My overall goal is to be able to add, delete, and edit records from both tables. 我的总体目标是能够从两个表中添加,删除和编辑记录。

Code Behind: 背后的代码:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
   namespace PO_1_5_15
{
    public partial class _Webform : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvPOs.DataSource = GetData("select * from dbo.po_table");
                gvPOs.DataBind();

                FormView1.Visible = false;

            }

        }

        protected void gvTasks_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            gvPOs.DataBind();
        }



        private static DataTable GetData(string query)
        {
            string constr = ConfigurationManager.ConnectionStrings["Test_DatabaseConnectionString3"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = query;
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataSet ds = new DataSet())
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
            }
        }

        protected void Show_Hide_ChildGrid(object sender, EventArgs e)
{
    ImageButton imgShowHide = (sender as ImageButton);
    GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);

    if (imgShowHide.CommandArgument == "Show")
    {
        row.FindControl("pnlTasks").Visible = true;
        imgShowHide.CommandArgument = "Hide";
        imgShowHide.ImageUrl = "~/images/minus.png";
        string POId = gvPOs.DataKeys[row.RowIndex].Value.ToString();
        GridView gvTasks = row.FindControl("gvTasks") as GridView; gvTasks.ToolTip = POId;

        gvTasks.DataSource = GetData(string.Format("select * from task where po_Id='{0}'", POId));
        gvTasks.DataBind();
    }
    else
    {
        row.FindControl("pnlTasks").Visible = false;
        imgShowHide.CommandArgument = "Show";
        imgShowHide.ImageUrl = "~/images/plus.png";
    }
}



private void BindTasks(string POId, GridView gvTasks)
{
    gvTasks.ToolTip = POId;
    gvTasks.DataSource = GetData(string.Format("select * from task where po_Id='{0}'", POId));
    gvTasks.DataBind();
}
protected void OnChildGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gvTasks = (sender as GridView);
    gvTasks.PageIndex = e.NewPageIndex;
    BindTasks(gvTasks.ToolTip, gvTasks);
}

protected void Button1_Click(object sender, EventArgs e)
{
    FormView1.Visible = true;
 //   FormView2.Visible = false;
}

protected void Button2_Click(object sender, EventArgs e)
{
 //   FormView2.Visible = true;
    FormView1.Visible = false;
}

protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
    gvPOs.DataBind();
}



protected void gvTasks_SelectedIndexChanged(object sender, EventArgs e)
{
    gvPOs.DataBind();
}
    }
}

HTML Markup: HTML标记:

 <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Webform.aspx.cs" Inherits="PO_1_5_15._Webform" %>

<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .ChildGrid {
            margin-right: 2px;
        }
        .Grid {
            margin-right: 30px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add PO" />
            <br />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Add Task" />
            <br />
            <asp:GridView ID="gvPOs" runat="server" AutoGenerateColumns="False" CssClass="Grid"
    DataKeyNames="PO_ID" EnableModelValidation="True" >

    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="imgShow" runat="server" OnClick="Show_Hide_ChildGrid" ImageUrl="~/images/plus.png"
                    CommandArgument="Show" />
                <asp:Panel ID="pnlTasks" runat="server" Visible="false" Style="position: relative">
                    <asp:GridView ID="gvTasks" runat="server" AutoGenerateColumns="False" PageSize="5" 
                        AllowPaging="True" OnPageIndexChanging="OnChildGrid_PageIndexChanging" CssClass="ChildGrid" EnableModelValidation="True">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="PO_ID" HeaderText="PO #" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="Invoice_No" HeaderText="Invoice #" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="Invoice_Date" HeaderText="Invoice Date" >
                            <ItemStyle Width="150px" />
                            </asp:BoundField>
                            <asp:BoundField ItemStyle-Width="150px" DataField="Task_Note" HeaderText="Task Note" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="Username" HeaderText="Username" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="UserDate" HeaderText="Current Date/Time" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="_Status" HeaderText="Status" >
                             <ItemStyle Width="150px" />
                            </asp:BoundField>
                             <asp:BoundField ItemStyle-Width="150px" DataField="Cost" HeaderText="Cost" >
                              <ItemStyle Width="150px" />
                            </asp:BoundField>
                              <asp:BoundField ItemStyle-Width="150px" DataField="Services" HeaderText="Service" >
                            <ItemStyle Width="150px" />
                            </asp:BoundField>
                            <asp:CommandField ShowSelectButton="True" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
                <br />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="PO_ID" HeaderText="PO #" >
<ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
          <asp:BoundField ItemStyle-Width="150px" DataField="Invoice_No" HeaderText="Invoice #" >
<ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField ItemStyle-Width="150px" DataField="Username" HeaderText="Username" >
<ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField ItemStyle-Width="150px" DataField="UserDate" HeaderText="Current Date/Time" >
<ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField ItemStyle-Width="150px" DataField="PO_Note" HeaderText="PO Note" >
<ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:CommandField ShowSelectButton="True" />
    </Columns>
</asp:GridView>

            <br />
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="PO_AutoID" DataSourceID="SqlDataSource1" EnableModelValidation="True" OnPageIndexChanging="FormView1_PageIndexChanging">
                <EditItemTemplate>
                    PO_AutoID:
                    <asp:Label ID="PO_AutoIDLabel1" runat="server" Text='<%# Eval("PO_AutoID") %>' />
                    <br />
                    PO_ID:
                    <asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
                    <br />
                    PO_Title:
                    <asp:TextBox ID="PO_TitleTextBox" runat="server" Text='<%# Bind("PO_Title") %>' />
                    <br />
                    Date_Received:
                    <asp:TextBox ID="Date_ReceivedTextBox" runat="server" Text='<%# Bind("Date_Received") %>' />
                    <br />
                    Date_Completed:
                    <asp:TextBox ID="Date_CompletedTextBox" runat="server" Text='<%# Bind("Date_Completed") %>' />
                    <br />
                    Username:
                    <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
                    <br />
                    UserDate:
                    <asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Bind("UserDate") %>' />
                    <br />
                    PO_Note:
                    <asp:TextBox ID="PO_NoteTextBox" runat="server" Text='<%# Bind("PO_Note") %>' />
                    <br />
                    Department:
                    <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' />
                    <br />
                    Invoice_No:
                    <asp:TextBox ID="Invoice_NoTextBox" runat="server" Text='<%# Bind("Invoice_No") %>' />
                    <br />
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    PO_ID:
                    <asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
                    <br />
                    PO_Title:
                    <asp:TextBox ID="PO_TitleTextBox" runat="server" Text='<%# Bind("PO_Title") %>' />
                    <br />
                    Date_Received:
                    <asp:TextBox ID="Date_ReceivedTextBox" runat="server" Text='<%# Bind("Date_Received") %>' />
                    <br />
                    Date_Completed:
                    <asp:TextBox ID="Date_CompletedTextBox" runat="server" Text='<%# Bind("Date_Completed") %>' />
                    <br />
                    Username:
                    <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
                    <br />
                    UserDate:
                    <asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Bind("UserDate") %>' />
                    <br />
                    PO_Note:
                    <asp:TextBox ID="PO_NoteTextBox" runat="server" Text='<%# Bind("PO_Note") %>' />
                    <br />
                    Department:
                    <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' />
                    <br />
                    Invoice_No:
                    <asp:TextBox ID="Invoice_NoTextBox" runat="server" Text='<%# Bind("Invoice_No") %>' />
                    <br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    PO_AutoID:
                    <asp:Label ID="PO_AutoIDLabel" runat="server" Text='<%# Eval("PO_AutoID") %>' />
                    <br />
                    PO_ID:
                    <asp:Label ID="PO_IDLabel" runat="server" Text='<%# Bind("PO_ID") %>' />
                    <br />
                    PO_Title:
                    <asp:Label ID="PO_TitleLabel" runat="server" Text='<%# Bind("PO_Title") %>' />
                    <br />
                    Date_Received:
                    <asp:Label ID="Date_ReceivedLabel" runat="server" Text='<%# Bind("Date_Received") %>' />
                    <br />
                    Date_Completed:
                    <asp:Label ID="Date_CompletedLabel" runat="server" Text='<%# Bind("Date_Completed") %>' />
                    <br />
                    Username:
                    <asp:Label ID="UsernameLabel" runat="server" Text='<%# Bind("Username") %>' />
                    <br />
                    UserDate:
                    <asp:Label ID="UserDateLabel" runat="server" Text='<%# Bind("UserDate") %>' />
                    <br />
                    PO_Note:
                    <asp:Label ID="PO_NoteLabel" runat="server" Text='<%# Bind("PO_Note") %>' />
                    <br />
                    Department:
                    <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Bind("Department") %>' />
                    <br />
                    Invoice_No:
                    <asp:Label ID="Invoice_NoLabel" runat="server" Text='<%# Bind("Invoice_No") %>' />
                    <br />
                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                    &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                    &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
                </ItemTemplate>
            </asp:FormView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_DatabaseConnectionString3 %>" SelectCommand="SELECT * FROM [PO_Table] WHERE ([PO_ID] = @PO_ID)" DeleteCommand="DELETE FROM [PO_Table] WHERE [PO_AutoID] = @PO_AutoID" InsertCommand="INSERT INTO [PO_Table] ([PO_ID], [PO_Title], [Date_Received], [Date_Completed], [Username], [UserDate], [PO_Note], [Department], [Invoice_No]) VALUES (@PO_ID, @PO_Title, @Date_Received, @Date_Completed, @Username, @UserDate, @PO_Note, @Department, @Invoice_No)" UpdateCommand="UPDATE [PO_Table] SET [PO_ID] = @PO_ID, [PO_Title] = @PO_Title, [Date_Received] = @Date_Received, [Date_Completed] = @Date_Completed, [Username] = @Username, [UserDate] = @UserDate, [PO_Note] = @PO_Note, [Department] = @Department, [Invoice_No] = @Invoice_No WHERE [PO_AutoID] = @PO_AutoID">
                <DeleteParameters>
                    <asp:Parameter Name="PO_AutoID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="PO_ID" Type="Int32" />
                    <asp:Parameter Name="PO_Title" Type="String" />
                    <asp:Parameter Name="Date_Received" Type="DateTime" />
                    <asp:Parameter Name="Date_Completed" Type="DateTime" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="UserDate" Type="DateTime" />
                    <asp:Parameter Name="PO_Note" Type="String" />
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="Invoice_No" Type="String" />
                </InsertParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="gvPOs" Name="PO_ID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="PO_ID" Type="Int32" />
                    <asp:Parameter Name="PO_Title" Type="String" />
                    <asp:Parameter Name="Date_Received" Type="DateTime" />
                    <asp:Parameter Name="Date_Completed" Type="DateTime" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="UserDate" Type="DateTime" />
                    <asp:Parameter Name="PO_Note" Type="String" />
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="Invoice_No" Type="String" />
                    <asp:Parameter Name="PO_AutoID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <br />
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Test_DatabaseConnectionString3 %>" DeleteCommand="DELETE FROM [Task] WHERE [Task_AutoID] = @Task_AutoID" InsertCommand="INSERT INTO [Task] ([PO_AutoID], [PO_ID], [Task_Title], [Username], [UserDate], [Cost], [_Status], [Invoice_Date], [Paid_Date], [Task_Note], [Department], [Invoice_No], [Services]) VALUES (@PO_AutoID, @PO_ID, @Task_Title, @Username, @UserDate, @Cost, @column1, @Invoice_Date, @Paid_Date, @Task_Note, @Department, @Invoice_No, @Services)" SelectCommand="SELECT * FROM [Task]" UpdateCommand="UPDATE [Task] SET [PO_AutoID] = @PO_AutoID, [PO_ID] = @PO_ID, [Task_Title] = @Task_Title, [Username] = @Username, [UserDate] = @UserDate, [Cost] = @Cost, [_Status] = @column1, [Invoice_Date] = @Invoice_Date, [Paid_Date] = @Paid_Date, [Task_Note] = @Task_Note, [Department] = @Department, [Invoice_No] = @Invoice_No, [Services] = @Services WHERE [Task_AutoID] = @Task_AutoID">
                <DeleteParameters>
                    <asp:Parameter Name="Task_AutoID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="PO_AutoID" Type="Int32" />
                    <asp:Parameter Name="PO_ID" Type="Int32" />
                    <asp:Parameter Name="Task_Title" Type="String" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="UserDate" Type="DateTime" />
                    <asp:Parameter Name="Cost" Type="Decimal" />
                    <asp:Parameter Name="column1" Type="String" />
                    <asp:Parameter Name="Invoice_Date" Type="DateTime" />
                    <asp:Parameter Name="Paid_Date" Type="DateTime" />
                    <asp:Parameter Name="Task_Note" Type="String" />
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="Invoice_No" Type="String" />
                    <asp:Parameter Name="Services" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="PO_AutoID" Type="Int32" />
                    <asp:Parameter Name="PO_ID" Type="Int32" />
                    <asp:Parameter Name="Task_Title" Type="String" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="UserDate" Type="DateTime" />
                    <asp:Parameter Name="Cost" Type="Decimal" />
                    <asp:Parameter Name="column1" Type="String" />
                    <asp:Parameter Name="Invoice_Date" Type="DateTime" />
                    <asp:Parameter Name="Paid_Date" Type="DateTime" />
                    <asp:Parameter Name="Task_Note" Type="String" />
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="Invoice_No" Type="String" />
                    <asp:Parameter Name="Services" Type="String" />
                    <asp:Parameter Name="Task_AutoID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>

            <br />
            <br />

        </div>
    </form>
</body>
</html>

I am not able to understand your problem 100%. 我无法100%理解您的问题。 if you want to use nested GridView, bind child GridView in Row_DataBound event of parent GridView. 如果要使用嵌套的GridView,请在父GridView的Row_DataBound事件中绑定子GridView。 Make sure you bind parent gridview inside if(!Page.IsPostBack) condition so once you hit select button it won't bind parent GridView. 确保在if(!Page.IsPostBack)条件内绑定父Gridview,因此一旦按下选择按钮,它将不会绑定父GridView。

Sample code for ASPX ASPX的示例代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SO_GVSample.aspx.cs" Inherits="MVCWeb.SO_GVSample" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:GridView ID="gvParent" runat="server" OnRowCommand="gvParent_RowCommand" AutoGenerateColumns="false" >
                <Columns>
                    <asp:BoundField HeaderText="Parent ID" DataField="ID" />
                    <asp:BoundField HeaderText="Description" DataField="Description" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:Button ID="btnSelect" runat="server" CommandName="cmdSelect" Text="Select" CommandArgument='<%# Eval("ID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

        </div>

        <asp:Panel ID="pnlChild" runat="server">
            Child Record
            <hr />
            <asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="false" OnRowCommand="gvChild_RowCommand">
                <Columns>
                    <asp:BoundField HeaderText="Parent ID" DataField="ID" />
                    <asp:BoundField HeaderText="Description" DataField="Description" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:Button ID="btnSelect" runat="server" CommandName="cmdCSelect" Text="Select" CommandArgument='<%# Eval("ID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </asp:Panel>
        <asp:Panel ID="pnlForm" runat="server">
            Form View
            <hr />
            <asp:FormView ID="FormView1" runat="server">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td><b>Form ID:</b></td>
                            <td><%# Eval("ChildID") %></td>
                        </tr>
                        <tr>
                            <td><b>Child ID:</b></td>
                            <td><%# Eval("ID") %></td>
                        </tr>

                        <tr>
                            <td><b>Form Desc:</b></td>
                            <td><%# Eval("Description") %> </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:FormView>
        </asp:Panel>
    </form>
</body>
</html>

Here is aspx.cs code 这是aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MVCWeb
{
    public partial class SO_GVSample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvParent.DataSource = GetParentData();
                gvParent.DataBind();

                pnlForm.Visible = false;
            }

        }

        #region Events

        protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdSelect")
            {
                gvChild.DataSource = GetChildData().Where(c => c.ParentID == Convert.ToInt32(e.CommandArgument));
                gvChild.DataBind();

                pnlForm.Visible = false;
            }
        }

        protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdCSelect")
            {
                FormView1.DataSource = GetFormData().Where(c => c.ChildID == Convert.ToInt32(e.CommandArgument));
                FormView1.DataBind();

                pnlForm.Visible = true;
            }
        }


        #endregion


        #region Data Source

        private List<ParentClass> GetParentData()
        {
            return new List<ParentClass>()
            {
                new ParentClass{ ID=1, Description="Parent Record 1"},
                new ParentClass{ ID=2, Description="Parent Record 2"},
                new ParentClass{ ID=3, Description="Parent Record 3"},
                new ParentClass{ ID=4, Description="Parent Record 4"},
                new ParentClass{ ID=5, Description="Parent Record 5"}
            };
        }

        private List<ChildClass> GetChildData()
        {
            return new List<ChildClass>()
            {
                new ChildClass{ ID=11, ParentID=1,  Description="Child Record 11"},
                new ChildClass{ ID=12, ParentID=1,  Description="Child Record 12"},
                new ChildClass{ ID=22,ParentID=2, Description="Child Record 22"},
                new ChildClass{ ID=23,ParentID=2, Description="Child Record 23"},
                new ChildClass{ ID=33,ParentID=3, Description="Child Record 3"},
                new ChildClass{ ID=44,ParentID=4, Description="Child Record 4"},
                new ChildClass{ ID=55, ParentID=5,Description="Child Record 55"},
                new ChildClass{ ID=56, ParentID=5,Description="Child Record 56"}
            };
        }

        private List<FormViewClass> GetFormData()
        {
            return new List<FormViewClass>()
            {
                new FormViewClass{ ID=111, ChildID=11,  Description="FormView Record 11"},
                new FormViewClass{ ID=222,ChildID=22, Description="FormView Record 22"},
                new FormViewClass{ ID=333,ChildID=33, Description="FormView Record 3"},
                new FormViewClass{ ID=444,ChildID=44, Description="FormView Record 4"},
                new FormViewClass{ ID=555, ChildID=55,Description="FormView Record 55"},
                 new FormViewClass{ ID=111, ChildID=12,  Description="FormView Record 11"},
                new FormViewClass{ ID=222,ChildID=23, Description="FormView Record 22"},            
                new FormViewClass{ ID=555, ChildID=56,Description="FormView Record 55"},
            };
        }

        public class ParentClass
        {
            public int ID { get; set; }

            public string Description { get; set; }
        }

        public class ChildClass
        {
            public int ID { get; set; }

            public int ParentID { get; set; }

            public string Description { get; set; }
        }

        public class FormViewClass
        {
            public int ID { get; set; }

            public int ChildID { get; set; }

            public string Description { get; set; }
        }

        #endregion


    }
}

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM