【问题标题】:Parent-Child Gridview in ASP.netASP.net 中的父子网格视图
【发布时间】:2011-07-12 10:17:57
【问题描述】:

我在 ASP.net 3.5 页面中有两个 GridView。我将 HyperLink 字段作为第一个 GridView 中的字段之一。

单击此超链接时,我需要通过将一些值传递给方法 showAllRecords(来自超链接的值)来调用显示第二个网格

我该怎么做?

谢谢

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以为 GridView1(主 GridView)尝试这样的 TemplateField

    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName"  CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    

    在 GridView1 的 RowCommand 中,您可以获取 CommandArgument 并为 GridView2(子 GridView)设置 DataSource。

    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.
        }
    }
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      首先您需要处理第一个网格上的 SelectedIndexChanged 事件,然后从超链接中获取值。超链接是 DataKey 吗?如果是,那么您通过GridOne.SelectedDataKey.Values["key"] 获得它,否则通过valuefromGridOne = GridOne.SelectedRow.Cells[num].Text 获得实际单元格,其中数字是单元格编号。拥有它后,您可以通过处理 objectDataSource 的 Selecting 事件(假设您使用它来绑定数据)并像这样传递值 e.InputParameters["dataKey"] = valuefromGridOne;

      ,将值传递给第二个网格

      【讨论】:

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