Find Label control inside Nested ASP.Net DataList using C#

Last Reply 2 months ago By kalpesh

Posted 2 months ago

i have been able to creat different method for inserting the two different IDs, now my challenge is on txtcommentpost in Datalist GetUSERCOMMENT2, i want to insert CommentID Column of TABLE A into table B ID column, i tried to do so and its not inserting, it can only insert ID Column of A table into CommentID Column of B table.

Code for Datalist GetUSERCOMMENT2

        TextBox textBox = sender as TextBox;

        Label label = sender as Label;
        if (textBox.ID == "txtcommentpost")
           // int comment2ID = Convert.ToInt32((textBox.NamingContainer.FindControl("lblId2") as Label).Text);
          int lblcomid = Convert.ToInt32((textBox.NamingContainer.FindControl("LBLCOM") as Label).Text);
            string comment = textBox.Text.Trim();
            string name = (textBox.NamingContainer.FindControl("lblnestname") as Label).Text;
            string username = (textBox.NamingContainer.FindControl("lblshId") as Label).Text;
           string imagename = (textBox.NamingContainer.FindControl("FImageName2") as Label).Text;
           Insert(lblcomid, comment, name, username, imagename, "PostComment2");



      <asp:DataList ID="GetUSERCOMMENTS2" runat="server" OnItemDataBound="GetUSERCOMMENTS2_ItemDataBound" Width="100%">
                              <div class="media-left"  style="">
        <article class="timeline-entry">

            <div class="timeline-entry-inner">

                <div class="timeline-icon bg-warning " style="">
                    <i class="entypo-feather">

                <div class="timeline-label">
                    <h2 style="margin-bottom:2px"><a href="#" style=" margin-bottom:2px">
                          <asp:Label ID="lblfname" Text='<%#Eval("fName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Black" Font-Size="Smaller" />
                        <asp:Label ID="lblUserNamecom" Text='<%#Eval("UserName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Gray" Font-Size="Smaller" />
                        <asp:Label ID="LBLCOM" runat="server" Text='<%#Eval("CommentID") %>' Visible="true" />
                       //  <asp:Label ID="lblId2" runat="server" Text='<%#Eval("ID") %>' Visible="false" />
                         <asp:Label ID="lblComments2" runat="server" Text='<%#Eval("Comments") %>'  ForeColor="#333333" CssClass="" Font-Names="Arial" Font-Size="Smaller" Font-Bold="False" />
Post Table

ID Username  Posts

10  mic22       hello world


Comment A table

CommentID       Comments   ID

4               Nice one     10


Comment B table

ID   Comment2   CommentID

8    i like that     1
Posted 2 months ago Modified on 2 months ago
micah says:
Post Table
ID Username  Posts
10  mic22       hello world
Comment A table
CommentID       Comments   ID
4               Nice one     10
Comment B table
ID   Comment2   CommentID
8    i like that     1

You need Comment B table Column ColumnId value as 10 from Comment A column CommentId value as it’s his parent DataList?

If yes and you need top access that value after if (textBox.ID == "txtcommentpost2") code then add below line to get parent DataList comment id of current child DataList by below code as implement it as per your logic.

int ParentCommentid = Convert.ToInt32(((textBox.NamingContainer.FindControl("Commentnest") as Label).Parent.Parent.Parent.Parent.FindControl("LBLCOM") as Label).Text);



Posted 2 months ago

So show me what the commentId column Value you are using on design or in code.
You need to paste proper code with proper table structure.

From your shared code there is parent dataList which have Label name as LBLCOM whose values sets by CommentID column from database.

You are not clear about your own code logic and understandability.

First be clear about it and then think what to implement.

As from your design it’s clear that parent commentId is stored in LBLCOM label in parent dataList.

micah says:
 <asp:Label ID="LBLCOM" runat="server" Text='<%#Eval("CommentID") %>' Visible="true" />


I agree, here is the link: