0

我的页面上有 2 个网格视图:在 GridView1 上,我有一列带有选择链接、ID 和名称,如下所示:

Select | Id | Name
select | 101 | Jack
select | 102 | Cath

现在,我要做的是,假设我从第一行中单击了选择 Jack,现在我的 GridView2 将显示 Jack 订购的产品,如下所示:

Id | ProductID
101 | 111
101 | 222
101 | 333

如果我选择了 Cath,GridView2 将更改 Cath 订购的显示产品:

Id | productID
102 | 111
102 | 333
102 | 555

简而言之,我试图根据从 GridView1 中选择的行来填充 GridView2。我正在使用带有 C# 的 asp.net。

4

4 回答 4

0

当您在第一个网格中选择行时,然后在网格的 selectIndexChanged 事件中获取主键 ID 的值并存储在 hiddenField 如果您使用过 Datakey 然后使用

 SelectedDatakey

取 SelectedDatakey 的值并存储在 hiddenfield 中

protected void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {

   hiddenfield1.value = CustomersGridView.SelectedDataKey.Value;

  }

现在,在您填充第二个网格的查询中,传递您存储在隐藏字段中的第一个网格的键值,并根据您的查询结果填充第二个网格

于 2013-06-07T11:29:44.903 回答
0

您可以在为“选择”添加的链接按钮上添加 onClick 方法。

   <asp:GridView ID="Gridview1" runat="server"
                    AllowPaging="true" PageSize="15" RowStyle-Wrap="true" EmptyDataRowStyle-ForeColor="Red"
                    AutoGenerateColumns="false" GridLines="None">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                            <asp:LinkButton ID ="li_Select" runat="server" Text="Select" OnClick="li_Select_Click"></asp:LinkButton>                                   
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lbl_ID" Text='<%#Eval("ID") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lbl_Name" Text='<%#Eval("Name") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

                </asp:GridView>                  

现在根据单击的行 ID 过滤数据或从数据库中获取数据。

  protected void li_Select_Click(object sender, EventArgs e)
{

    LinkButton lnkbtn = (LinkButton)sender;
    GridViewRow row = (GridViewRow)lnkbtn.NamingContainer;
    Label lbl_ID = (Label)row.FindControl("lbl_ID");

  // You can fetch the data from the database on the basis of selected User ID .
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Product_ID");
    for (int i = 100; i <= 110; i++)
    {
        DataRow dr = dt.NewRow();
        dr["ID"] = i;
        dr["Product_ID"] = "P_" + i;
        dt.Rows.Add(dr);
    }
    // In this method you pass the id of the user, and datatable fetched from the    database of all products. or you can pass the id in the storedprocedure to get data of selected user only and then bind it here .
    GetData(lbl_ID.Text,dt);
}
protected void GetData(string ID, DataTable dt)
{       

    DataView dv = dt.DefaultView;
    dv.RowFilter = "ID=" + ID;
    Gridview2.DataSource = dv;
    Gridview2.DataBind();

}
于 2013-06-07T12:13:14.953 回答
0

我认为最好的方法是使用UpdatePanel绑定第二个网格,使用它你会有一个优势,没有回发(实际上是回发,但用户不会注意到它......)......

而如果你不使用UpdatePanel,那么在回发中绑定后就无法看到数据(除非你是通过Javascript来做的,这很麻烦)......这是实现它的示例代码:

ASPX 页面:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="GridView2" runat="server" >
            <Columns>

            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server">
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:LinkButton ID="asd" Text='Select' runat="server" OnClick="link_click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

代码背后:

protected override void PageLoad(object sender, System.EventArgs e)
{

    if (!IsPostBack)
    {

        GridView1.DataSource = getDataSource();
        GridView1.DataBind();

    }
}
private DataTable getDataSource()
{

    SqlConnection conn = new SqlConnection("Server=192.168.1.1;DATABASE=dummy;UID=****;PWD=*****;");    //Example connString
    SqlCommand comm = new SqlCommand("SELECT * FROM Table1", conn);
    SqlDataAdapter ad = new SqlDataAdapter(comm);

    DataTable ta = new DataTable();
    ad.Fill(ta);

    return ta;
}

protected void button_click(object sender, EventArgs e)
{
    LinkButton asd = (LinkButton)sender;
    GridViewRow row = (GridViewRow)asd.NamingContainer;     //Gets the selected Row

    string user_id = row.Cells[2].Text;     //Use this to get any value you want.
    //Can now use the user_name to get the data for the grid 2, and update the panel
    GridView2.DataSource = getDataSource2(user_id);
    GridView2.DataBind();
    UpdatePanel1.Update();
}
private DataTable getDataSource2(string user_id)
{
    string sql = "SELECT * FROM TABLE2 WHERE user_id = @user_id";
    SqlConnection conn = new SqlConnection("Server=sqlserver\\sql2008;DATABASE=esm80;UID=sa;PWD=sa;");    //Example connString
    SqlCommand comm = new SqlCommand();
    comm.CommandText = sql;
    comm.Parameters.AddWithValue("@name", user_id);
    comm.Connection = conn;

    SqlDataAdapter ad = new SqlDataAdapter(comm);

    DataTable ta = new DataTable();
    ad.Fill(ta);

    return ta;
}

现在解释一下UpdatePanel,一旦数据绑定到它,GridView2 就会更新 gridView2(这将显示新绑定的数据)。GridView1的UpdatePanel存在是为了防止来自链接按钮的回发。

希望这能回答你的问题。

于 2013-06-07T13:08:03.843 回答
0

这是我的示例代码......

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Data> DataList = new List<Data>() { new Data { id = 1, id2 = 2 }, new Data { id = 3, id2 = 4 } };
        GridView1.DataSource = DataList;
        GridView1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow ) btn.NamingContainer;

    Label slNoLabel = (Label) row.FindControl("slNoLabel");
    // function to get data based on label vlue
    GridView2.DataSource=GetData(slNoLabel.Text);
    GridView2.DataBind();

}

DataTable GetData(string value)
{
DataTable tbl = new DataTable ();
    //   Calling DB 
    return tbl;
    }
}

public class Data
{
    public int id { get; set; }
    public int id2 { get; set; }
}

在用户界面中

 <div>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Select" OnClick="Button1_Click" Text="Select" />
                    <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:GridView ID="GridView2" runat="server"></asp:GridView>
</div>
于 2013-06-07T11:39:05.660 回答