locked
Find Parent Datalist Hidden Value control from nested gridview link button click RRS feed

  • Question

  • User1587720337 posted

    I am trying to find the hidden field control which is inside the datalist from the gridview linkbutton command event. (Note: the gridview is under the datalist itself).

    ASPX Code:

    <asp:DataList ID="DataList1" runat="server" Width="900px" GridLines="Horizontal" RepeatColumns="2" RepeatDirection="Horizontal" CellPadding="3"
    BorderWidth="1px" BorderStyle="None" BorderColor="#E7E7FF" BackColor="White" OnItemCommand="DataList1_ItemCommand">
    <ItemTemplate>
    <!--------------------------------------------------------------------------------------------------->


    <!--end .margin-bottom-xxl -->
    <div class="list-results" >
    <%--<div >--%>
    <div class="col-xs-12 col-lg-12 hbox-xs">
    <div class="hbox-column width-2">
    <img class="img-circle img-responsive pull-left" src="../App_Themes/Images/ProfileImages/Default.jpg" alt="" />
    </div>
    <!--end .hbox-column -->
    <div class="hbox-column v-top">
    <div class="clearfix">
    <div class="col-lg-12 margin-bottom-lg">
    <a class="text-lg text-medium" href="#"><%# DataBinder.Eval(Container.DataItem, "FirstName") %> <%# DataBinder.Eval(Container.DataItem, "LastName") %> <%# DataBinder.Eval(Container.DataItem, "ID") %></a>


    <asp:HiddenField ID="hfFirstName" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />


    </div>
    </div>
    <div class="clearfix opacity-75">
    <div class="col-md-5">
    <span class="glyphicon glyphicon-phone text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "Phone1") %>
    </div>
    <div class="col-md-7">
    <span class="glyphicon glyphicon-envelope text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "username") %>


    <!--set page size-->
    <div class="margin-bottom-xl" style="margin-bottom: 5px !important; padding-bottom: 0px !important; padding-top: 0px !important;">
    <asp:Label ID="Label1" runat="server" CssClass="text-light text-lg" Text="Filtered results: " Visible="false"></asp:Label>
    <asp:Label ID="Label2" runat="server" CssClass="text-light text-lg" Text="34" Font-Bold="true" Visible="false"></asp:Label>
    <%--<span class="text-light text-lg">Filtered results <strong>34</strong></span>--%>
    <div class="btn-group btn-group-sm pull-right" >



    <!--start page size-->
    <ul class="dropdown-menu dropdown-menu-right animation-dock" role="menu" style="background-color: lightgray !important; !important; overflow-y: scroll !important; height:150px !important ">
    <li>
    <asp:GridView ID="gvList1" BorderColor="Transparent" runat="server" DataSourceID="dsi_lstRecipientsGroups" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>

    <asp:LinkButton ID="lnkAdd" runat="server" Text=<%#Eval("GroupName")%> OnClick="AddGroup"
    CommandArgument='<%# Eval("GroupID") %>' ></asp:LinkButton>


    <%-- <asp:LinkButton ID="lnkAdd" runat="server" Text=<%#Eval("GroupName")%>
    CommandName="Del"></asp:LinkButton>--%>
    </ItemTemplate>
    <ItemStyle Width="240px" />
    </asp:TemplateField>

    </Columns>
    </asp:GridView>
    </li>
    </ul>

    </div>
    </div>
    <!--end page size-->

    </div>
    </div>
    <!--end .hbox-column -->
    </div>
    <!--end .hbox-xs -->
    </div>
    </ItemTemplate>
    </asp:DataList>

    aspx.cs code

    protected void AddGroup(object sender, EventArgs e)
    {
    string id = ((sender as LinkButton).CommandArgument).ToString();

    try
    {

    using (SqlConnection conn = new SqlConnection(strConnection))
    {
    if (conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    using (SqlCommand cmd = new SqlCommand("SPU_GroupContactMapping", conn))
    {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "SPU_GroupContactMapping";
    cmd.Parameters.Add(new SqlParameter("@ContactID", SqlDbType.Int)).Value = 432; Need to find the contact id/first name from the datalist
    cmd.Parameters.Add(new SqlParameter("@GroupID", SqlDbType.Int)).Value = Convert.ToInt32(id);
    cmd.Parameters.Add(new SqlParameter("@ClientID", SqlDbType.Int)).Value = Convert.ToInt32(Session["ClientID"]);
    cmd.ExecuteNonQuery();
    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "Contact deleted successfully.", true);
    Response.Redirect("Contacts.aspx");
    }
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }

    }

    Monday, August 26, 2019 5:49 AM

Answers

  • User-719153870 posted

    Hi sudhir.bhart@hotmail.com,

    Need to find the contact id/first name from the datalist

    According to your description, you are trying to get hiddenfield's value from datalist1 in the linkbutton event which is in the gridview which is in the datalist1.

    Press F12 in your browser and you will find there's a paired relationship between gridview's client id and the hiddenfield's client id.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DataList ID="DataList1" runat="server" Width="900px" GridLines="Horizontal" RepeatColumns="2" RepeatDirection="Horizontal" CellPadding="3"
                    BorderWidth="1px" BorderStyle="None" BorderColor="#E7E7FF" BackColor="White" OnItemDataBound="DataList1_ItemDataBound" >
                    <ItemTemplate>
                        <!--------------------------------------------------------------------------------------------------->
    
                        <!--end .margin-bottom-xxl -->
                        <div class="list-results">
                            <%--<div >--%>
                            <div class="col-xs-12 col-lg-12 hbox-xs">
                                <div class="hbox-column width-2">
                                    <img class="img-circle img-responsive pull-left" src="../App_Themes/Images/ProfileImages/Default.jpg" alt="" />
                                </div>
                                <!--end .hbox-column -->
                                <div class="hbox-column v-top">
                                    <div class="clearfix">
                                        <div class="col-lg-12 margin-bottom-lg">
                                            <a class="text-lg text-medium" href="#"><%# DataBinder.Eval(Container.DataItem, "FirstName") %> <%# DataBinder.Eval(Container.DataItem, "LastName") %> <%# DataBinder.Eval(Container.DataItem, "ID") %></a>
                                            <asp:HiddenField ID="hfFirstName" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
                                        </div>
                                    </div>
                                    <div class="clearfix opacity-75">
                                        <div class="col-md-5">
                                            <span class="glyphicon glyphicon-phone text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "Phone1") %>
                                        </div>
                                        <div class="col-md-7">
                                            <span class="glyphicon glyphicon-envelope text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "username") %>
                                            <!--set page size-->
                                            <div class="margin-bottom-xl" style="margin-bottom: 5px !important; padding-bottom: 0px !important; padding-top: 0px !important;">
                                                <asp:Label ID="Label1" runat="server" CssClass="text-light text-lg" Text="Filtered results: " Visible="false"></asp:Label>
                                                <asp:Label ID="Label2" runat="server" CssClass="text-light text-lg" Text="34" Font-Bold="true" Visible="false"></asp:Label>
                                                <%--<span class="text-light text-lg">Filtered results <strong>34</strong></span>--%>
                                                <div class="btn-group btn-group-sm pull-right">
                                                    <!--start page size-->
                                                    <ul class="dropdown-menu dropdown-menu-right animation-dock" role="menu" style="background-color: lightgray !important;  !important; overflow-y: scroll !important; height: 150px !important">
                                                        <li>
                                                            <asp:GridView ID="gvList1" BorderColor="Transparent" runat="server" AutoGenerateColumns="false" >
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                        <ItemTemplate>
    
                                                                            <asp:LinkButton ID="lnkAdd" runat="server" Text='<%#Eval("GroupName")%>' OnClick="AddGroup"
                                                                                CommandArgument='<%# Eval("GroupID") %>'></asp:LinkButton>
                                                                            <%-- <asp:LinkButton ID="lnkAdd" runat="server" Text=<%#Eval("GroupName")%> 
    CommandName="Del"></asp:LinkButton>--%>
                                                                        </ItemTemplate>
                                                                        <ItemStyle Width="240px" />
                                                                    </asp:TemplateField>
                                                                </Columns>
                                                            </asp:GridView>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                            <!--end page size-->
                                        </div>
                                    </div>
                                    <!--end .hbox-column -->
                                </div>
                                <!--end .hbox-xs -->
                            </div>
                    </ItemTemplate>
                </asp:DataList>
            </div>
        </form>
    </body>
    </html>

    .CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("ID"), new DataColumn("Phone1"), new DataColumn("username") });
                dt.Rows.Add("AA", "aa", "1", "000", "Aa");
                dt.Rows.Add("BB", "bb", "2", "000", "Bb");
                dt.Rows.Add("CC", "cc", "3", "000", "Cc");
                dt.Rows.Add("DD", "dd", "4", "000", "Dd");
                dt.Rows.Add("EE", "ee", "5", "000", "Ee");
                dt.Rows.Add("FF", "ff", "6", "000", "Ff");
                dt.Rows.Add("GG", "gg", "7", "000", "Gg");
    
                DataList1.DataSource = dt;
                DataList1.DataBind();
            }
    
            protected void AddGroup(object sender, EventArgs e)
            {
                try
                {
                    int sign = 0;
                    LinkButton linkbutton = (LinkButton)sender;
                    sign = Convert.ToInt32( linkbutton.ClientID.Substring(18,1));
                    HiddenField fn = DataList1.Items[sign].FindControl("hfFirstName") as HiddenField;
                    Response.Write("<script>alert('"+ fn.Value + "')</script>");//fn.values should be what you want
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }
    
            protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("GroupName"), new DataColumn("GroupID") });
                dt1.Rows.Add("AAA", "12");
                dt1.Rows.Add("BBB", "13");
                dt1.Rows.Add("CCC", "14");
                dt1.Rows.Add("DDD", "15");
                dt1.Rows.Add("EEE", "16");
    
                GridView gvList1 = e.Item.FindControl("gvList1") as GridView;
    
                gvList1.DataSource = dt1;
                gvList1.DataBind();
            }

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 7:18 AM
  • User1587720337 posted

    Thank you, It's working now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 3:43 PM

All replies

  • User-719153870 posted

    Hi sudhir.bhart@hotmail.com,

    Need to find the contact id/first name from the datalist

    According to your description, you are trying to get hiddenfield's value from datalist1 in the linkbutton event which is in the gridview which is in the datalist1.

    Press F12 in your browser and you will find there's a paired relationship between gridview's client id and the hiddenfield's client id.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DataList ID="DataList1" runat="server" Width="900px" GridLines="Horizontal" RepeatColumns="2" RepeatDirection="Horizontal" CellPadding="3"
                    BorderWidth="1px" BorderStyle="None" BorderColor="#E7E7FF" BackColor="White" OnItemDataBound="DataList1_ItemDataBound" >
                    <ItemTemplate>
                        <!--------------------------------------------------------------------------------------------------->
    
                        <!--end .margin-bottom-xxl -->
                        <div class="list-results">
                            <%--<div >--%>
                            <div class="col-xs-12 col-lg-12 hbox-xs">
                                <div class="hbox-column width-2">
                                    <img class="img-circle img-responsive pull-left" src="../App_Themes/Images/ProfileImages/Default.jpg" alt="" />
                                </div>
                                <!--end .hbox-column -->
                                <div class="hbox-column v-top">
                                    <div class="clearfix">
                                        <div class="col-lg-12 margin-bottom-lg">
                                            <a class="text-lg text-medium" href="#"><%# DataBinder.Eval(Container.DataItem, "FirstName") %> <%# DataBinder.Eval(Container.DataItem, "LastName") %> <%# DataBinder.Eval(Container.DataItem, "ID") %></a>
                                            <asp:HiddenField ID="hfFirstName" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
                                        </div>
                                    </div>
                                    <div class="clearfix opacity-75">
                                        <div class="col-md-5">
                                            <span class="glyphicon glyphicon-phone text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "Phone1") %>
                                        </div>
                                        <div class="col-md-7">
                                            <span class="glyphicon glyphicon-envelope text-sm" style="font-size: 14px !important;"></span>&nbsp;<%# DataBinder.Eval(Container.DataItem, "username") %>
                                            <!--set page size-->
                                            <div class="margin-bottom-xl" style="margin-bottom: 5px !important; padding-bottom: 0px !important; padding-top: 0px !important;">
                                                <asp:Label ID="Label1" runat="server" CssClass="text-light text-lg" Text="Filtered results: " Visible="false"></asp:Label>
                                                <asp:Label ID="Label2" runat="server" CssClass="text-light text-lg" Text="34" Font-Bold="true" Visible="false"></asp:Label>
                                                <%--<span class="text-light text-lg">Filtered results <strong>34</strong></span>--%>
                                                <div class="btn-group btn-group-sm pull-right">
                                                    <!--start page size-->
                                                    <ul class="dropdown-menu dropdown-menu-right animation-dock" role="menu" style="background-color: lightgray !important;  !important; overflow-y: scroll !important; height: 150px !important">
                                                        <li>
                                                            <asp:GridView ID="gvList1" BorderColor="Transparent" runat="server" AutoGenerateColumns="false" >
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                        <ItemTemplate>
    
                                                                            <asp:LinkButton ID="lnkAdd" runat="server" Text='<%#Eval("GroupName")%>' OnClick="AddGroup"
                                                                                CommandArgument='<%# Eval("GroupID") %>'></asp:LinkButton>
                                                                            <%-- <asp:LinkButton ID="lnkAdd" runat="server" Text=<%#Eval("GroupName")%> 
    CommandName="Del"></asp:LinkButton>--%>
                                                                        </ItemTemplate>
                                                                        <ItemStyle Width="240px" />
                                                                    </asp:TemplateField>
                                                                </Columns>
                                                            </asp:GridView>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                            <!--end page size-->
                                        </div>
                                    </div>
                                    <!--end .hbox-column -->
                                </div>
                                <!--end .hbox-xs -->
                            </div>
                    </ItemTemplate>
                </asp:DataList>
            </div>
        </form>
    </body>
    </html>

    .CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("ID"), new DataColumn("Phone1"), new DataColumn("username") });
                dt.Rows.Add("AA", "aa", "1", "000", "Aa");
                dt.Rows.Add("BB", "bb", "2", "000", "Bb");
                dt.Rows.Add("CC", "cc", "3", "000", "Cc");
                dt.Rows.Add("DD", "dd", "4", "000", "Dd");
                dt.Rows.Add("EE", "ee", "5", "000", "Ee");
                dt.Rows.Add("FF", "ff", "6", "000", "Ff");
                dt.Rows.Add("GG", "gg", "7", "000", "Gg");
    
                DataList1.DataSource = dt;
                DataList1.DataBind();
            }
    
            protected void AddGroup(object sender, EventArgs e)
            {
                try
                {
                    int sign = 0;
                    LinkButton linkbutton = (LinkButton)sender;
                    sign = Convert.ToInt32( linkbutton.ClientID.Substring(18,1));
                    HiddenField fn = DataList1.Items[sign].FindControl("hfFirstName") as HiddenField;
                    Response.Write("<script>alert('"+ fn.Value + "')</script>");//fn.values should be what you want
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }
    
            protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("GroupName"), new DataColumn("GroupID") });
                dt1.Rows.Add("AAA", "12");
                dt1.Rows.Add("BBB", "13");
                dt1.Rows.Add("CCC", "14");
                dt1.Rows.Add("DDD", "15");
                dt1.Rows.Add("EEE", "16");
    
                GridView gvList1 = e.Item.FindControl("gvList1") as GridView;
    
                gvList1.DataSource = dt1;
                gvList1.DataBind();
            }

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 7:18 AM
  • User1587720337 posted

    Thank you, It's working now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 3:43 PM