locked
Problem fetching the value of a complex List in Eval method RRS feed

  • Question

  • User-377150470 posted

    So I have a class called Student -

    {

    int rollNo {get; set;}

    string name {get; set;}

    int branchId {get; set;}

    List<Branch> studentBranchName {get; set;}

    }

    And the class Branch -

    {

    int branchId {get; set;}

    string branchName {get; set;}

    }

    The studentBranchName would contain a integer value which will be related to the branchName in class Branch.

    In my aspx page, I have a repeater whose data source is associated with the Student class. I want to display the branch name in the Eval method inside the repeater. 

    How can I do that?

    Thursday, September 12, 2019 7:01 PM

Answers

  • User-719153870 posted

    Hi siddharth,

    In my aspx page, I have a repeater whose data source is associated with the Student class. I want to display the branch name in the Eval method inside the repeater. 

    This description might be a little confusing, but is below this what you want?

    If so, please refer to below code:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table border="1" style="border-collapse: collapse">
                    <tr>
                        <td>rollNo</td>
                        <td>name</td>
                        <td>branchName</td>
    
                    </tr>
                    <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="Label4" runat="server" Text='<%#Eval("rollNo") %>'></asp:Label></td>
                                <td>
                                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label></td>
                                <td>
                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("studentBranchName") %>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
            </div>
        </form>
    </body>
    </html>

    .CS:

    List<Student> slist = new List<Student>();
            protected void Page_Load(object sender, EventArgs e)
            {
                List<Branch> blist = new List<Branch>();
                for (int i = 0; i < 5; i++)
                {
                    Branch branch = new Branch()
                    {
                        branchId = i,
                        branchName = "a" + i.ToString()
                    };
                    blist.Add(branch);
                }
                for (int j = 0; j < 5; j++)
                {
                    Student student = new Student()
                    {
                        rollNo = j,
                        name = "Sam" + j.ToString(),
                        branchId = 1,
                        studentBranchName = blist,
                    };
                    slist.Add(student);
                }
    
    
    
                Repeater2.DataSource = slist;
                Repeater2.DataBind();
    
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                Repeater repeater1 = e.Item.FindControl("Repeater1") as Repeater;
                Label label1 = e.Item.FindControl("Label4") as Label;
                Label branchLabel = e.Item.FindControl("Label1") as Label;
                List<Branch> testList = slist.
        Where(x => x.rollNo == Convert.ToInt32(label1.Text.ToString())).Select(i => i.studentBranchName).FirstOrDefault().ToList();
                var branchName = string.Join(",", testList.Select(i => i.branchName).ToArray());
                branchLabel.Text = branchName;
            }
    
            public class Student
            {
                public int rollNo { get; set; }
                public string name { get; set; }
                public int branchId { get; set; }
                public List<Branch> studentBranchName { get; set; }
            }
    
            public class Branch
            {
                public int branchId { get; set; }
                public string branchName { get; set; }
            }

    Hope this would help you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2019 3:24 AM

All replies

  • User-719153870 posted

    Hi siddharth,

    In my aspx page, I have a repeater whose data source is associated with the Student class. I want to display the branch name in the Eval method inside the repeater. 

    This description might be a little confusing, but is below this what you want?

    If so, please refer to below code:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table border="1" style="border-collapse: collapse">
                    <tr>
                        <td>rollNo</td>
                        <td>name</td>
                        <td>branchName</td>
    
                    </tr>
                    <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="Label4" runat="server" Text='<%#Eval("rollNo") %>'></asp:Label></td>
                                <td>
                                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label></td>
                                <td>
                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("studentBranchName") %>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
            </div>
        </form>
    </body>
    </html>

    .CS:

    List<Student> slist = new List<Student>();
            protected void Page_Load(object sender, EventArgs e)
            {
                List<Branch> blist = new List<Branch>();
                for (int i = 0; i < 5; i++)
                {
                    Branch branch = new Branch()
                    {
                        branchId = i,
                        branchName = "a" + i.ToString()
                    };
                    blist.Add(branch);
                }
                for (int j = 0; j < 5; j++)
                {
                    Student student = new Student()
                    {
                        rollNo = j,
                        name = "Sam" + j.ToString(),
                        branchId = 1,
                        studentBranchName = blist,
                    };
                    slist.Add(student);
                }
    
    
    
                Repeater2.DataSource = slist;
                Repeater2.DataBind();
    
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                Repeater repeater1 = e.Item.FindControl("Repeater1") as Repeater;
                Label label1 = e.Item.FindControl("Label4") as Label;
                Label branchLabel = e.Item.FindControl("Label1") as Label;
                List<Branch> testList = slist.
        Where(x => x.rollNo == Convert.ToInt32(label1.Text.ToString())).Select(i => i.studentBranchName).FirstOrDefault().ToList();
                var branchName = string.Join(",", testList.Select(i => i.branchName).ToArray());
                branchLabel.Text = branchName;
            }
    
            public class Student
            {
                public int rollNo { get; set; }
                public string name { get; set; }
                public int branchId { get; set; }
                public List<Branch> studentBranchName { get; set; }
            }
    
            public class Branch
            {
                public int branchId { get; set; }
                public string branchName { get; set; }
            }

    Hope this would help you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2019 3:24 AM
  • User-377150470 posted

    This was very helpful. Thanks a lot, you're a genius!

    Monday, September 16, 2019 3:09 AM
  • User-377150470 posted

    This was very helpful. Thanks a lot, you're a genius!

    Monday, September 16, 2019 3:11 AM