locked
getting dataKey from parent control RRS feed

  • Question

  • User-153404742 posted

    Hi,

    I have a <asp:ListView> control within which I have a child GridView control.  I would like to get DataKeyValue of the ListView within the child gridview's rowDataBound event.  It's working by gettting lvGrids.DataKeys[0].Value; however, it grabs the same value inside both of the dynamic listView controls generated.

    So I have the following:

     <asp:ListView ID="lvGrids" runat="server" DataKeyNames="LVId" OnItemDataBound="lvGrids_ItemDataBound">
                <ItemTemplate>

                                 <asp:GridView ID="gvChildGrid" runat="server" DataKeyNames="GVId" OnRowDataBound="gvChildGrid_RowDataBound">   </asp:GridView>

    </ItemTemplate>

    </asp:ListView>          

    I would like to get the LVId inside gvChildGrid_RowDataBound event.  So my code builds two ListView but inside the gvChildGrid_RowDataBound, the following code always returns the LVId of the first grid.

    lvGrids.DataKeys[0].

    Thursday, December 10, 2020 10:00 PM

All replies

  • User-939850651 posted

    Hi inkaln,

    According to the limited code you provided, I am not sure whether your problem is completely reproduced. If my guess is correct, you can try to get the index of the ListViewItem and then get the LVId.

    This is my test:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable LVData = new DataTable();
                    LVData.Columns.Add("LVId");
                    LVData.Rows.Add(1);
                    LVData.Rows.Add(2);
                    lvGrids.DataSource = LVData;
                    lvGrids.DataBind();
                }
            }
    
            protected void lvGrids_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                if (e.Item.ItemType == ListViewItemType.DataItem)
                {
                    GridView grid = e.Item.FindControl("gvChildGrid") as GridView;
    
                    DataTable GVData = new DataTable();
                    GVData.Columns.Add("GVId");
                    GVData.Columns.Add("Col1");
                    GVData.Columns.Add("Col2");
                    GVData.Rows.Add(1, "Col11", "Col21");
                    GVData.Rows.Add(2, "Col12", "Col22");
                    GVData.Rows.Add(3, "Col13", "Col23");
                    grid.DataSource = GVData;
                    grid.DataBind();
                }
            }
    
            protected void gvChildGrid_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    ListViewItem item = e.Row.Parent.Parent.Parent as ListViewItem;
                    string dataKey = lvGrids.DataKeys[item.DataItemIndex][0].ToString();
                }
            }
    <asp:ListView ID="lvGrids" runat="server" DataKeyNames="LVId" OnItemDataBound="lvGrids_ItemDataBound">
                    <ItemTemplate>
                        <asp:GridView runat="server" ID="gvChildGrid" DataKeyNames="GVId" OnRowDataBound="gvChildGrid_RowDataBound"></asp:GridView>
                    </ItemTemplate>
                </asp:ListView>

    If I misunderstood something, please post the complete code, which will help us solve the problem.

    Best regards,

    Xudong Peng

    Friday, December 11, 2020 9:17 AM