ایجاد ListView های تو در تو Asp.Net

پنجشنبه 28 خرداد 1394

در این مقاله قصد داریم تا نحوه ایجاد لیست ها تو در تو را در Asp.Net پیاده سازی کنیم .

ایجاد ListView های تو در تو Asp.Net

در مرحله اول یک کلاس برای آدرس مربوط به مشتریان ایجاد میکنیم

public class Address
{
          public string Country
          {
                   get;
                   set;
          }
          public string State
          {
                   get;
                   set;
          }
          public string City
          {
                   get;
                   set;
          }
} 

 

در مرحله دوم یک کلاس برای مشتریان ایجاد میکنیم

public class Customer
{
          public string CustomerName
          {
                   get;
                   set;
          }
          public string Email
          {
                   get;
                   set;
          }
          public string Website
          {
                   get;
                   set;
          }
          public List<Address> Address
          {
                   get;
                   set;
          }
}

 

در مرحله صفحه Customer.Aspx  را ایجاد میکنیم

<asp:ListView runat="server" ItemPlaceholderID="placeHolderCustomer" ID="LstCustomers">
            <LayoutTemplate>
                <div>
                    <asp:PlaceHolder runat="server" ID="placeHolderCustomer" />
                </div>
            </LayoutTemplate>
            <ItemTemplate>
                 Customer Name:<asp:Label ID="Label1" Text='<%# Eval("CustomerName") %>' runat="server" /><br />
                 Email:<asp:Label ID="Label2" Text='<%# Eval("Email") %>' runat="server" /><br />
                 Website:<asp:Label ID="Label3" Text='<%# Eval("Website") %>' runat="server" />
                <asp:ListView ID="ListView1" runat="server" DataSource='<%# Eval("Address") %>' ItemPlaceholderID="addressPlaceHolder">
                    <LayoutTemplate>
                        <div style="padding-left: 30px;">
                            <table border="1" cellpadding="0" cellspacing="0">
                                <asp:PlaceHolder runat="server" ID="addressPlaceHolder" />
                             </table>
                        </div>
                        <hr />
                    </LayoutTemplate>
                    <ItemTemplate>

                        <tr>
                            <td>
                                <asp:Label ID="Label4" Text='<%# Eval("Country") %>' runat="server" />
                            </td>
                            <td>
                                <asp:Label ID="Label3" Text='<%# Eval("State") %>' runat="server" />
                            </td>
                            <td>

                                <asp:Label ID="Label5" Text='<%# Eval("City") %>' runat="server" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:ListView>
            </ItemTemplate>
        </asp:ListView> 

 

و در مرحله آخر و در رویداد Load صفحه لیستی میسازیم و به ListView متصل میکنیم

<asp:ListView runat="server" ItemPlaceholderID="placeHolderCustomer" ID="LstCustomers">
            <LayoutTemplate>
                <div>
                    <asp:PlaceHolder runat="server" ID="placeHolderCustomer" />
                </div>
            </LayoutTemplate>
            <ItemTemplate>
                 Customer Name:<asp:Label ID="Label1" Text='<%# Eval("CustomerName") %>' runat="server" /><br />
                 Email:<asp:Label ID="Label2" Text='<%# Eval("Email") %>' runat="server" /><br />
                 Website:<asp:Label ID="Label3" Text='<%# Eval("Website") %>' runat="server" />
                <asp:ListView ID="ListView1" runat="server" DataSource='<%# Eval("Address") %>' ItemPlaceholderID="addressPlaceHolder">
                    <LayoutTemplate>
                        <div style="padding-left: 30px;">
                            <table border="1" cellpadding="0" cellspacing="0">
                                <asp:PlaceHolder runat="server" ID="addressPlaceHolder" />
                             </table>
                        </div>
                        <hr />
                    </LayoutTemplate>
                    <ItemTemplate>

                        <tr>
                            <td>
                                <asp:Label ID="Label4" Text='<%# Eval("Country") %>' runat="server" />
                            </td>
                            <td>
                                <asp:Label ID="Label3" Text='<%# Eval("State") %>' runat="server" />
                            </td>
                            <td>

                                <asp:Label ID="Label5" Text='<%# Eval("City") %>' runat="server" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:ListView>
            </ItemTemplate>
        </asp:ListView> 

Step 4

Add the following code in Page Load of Customer.aspx:

List<Customer> objCustomers = new List<Customer>()
{
          new Customer()
          {
                    CustomerName="Customer 1",
                    Email="Customer1@Customer.com",
                    Website="www.Customer1.com",
                    Address=new List<Address>()
                    {
                             new Address()
                             {
                                      Country="Country1",
                                      State="State1",
                                      City="City1",
                             },
                             new Address()
                             {
                                      Country="Country1",
                                      State="State1",
                                      City="City2",
                             },
                             new Address()
                             {
                                      Country="Country1",
                                      State="State2",
                                      City="City3",
                             }
                    }
 
          },
          new Customer()
          {
                    CustomerName="Customer 2",
                    Email="Customer2@Customer.com",
                    Website="www.Customer2.com",
                    Address=new List<Address>()
                    {
                             new Address()
                             {
                                      Country="Country2",
                                      State="State2",
                                        City="City4",
                             },
                             new Address()
                             {
                                      Country="Country2",
                                      State="State2",
                                      City="City5",
                             },
                             new Address()
                             


                                      Country="Country2",
                                      State="State2",

                                      City="City6",
                             }
                    }
          },
          new Customer()
          {
                    CustomerName="Customer 3",
                    Email="Customer3@Customer.com",
                    Website="www.Customer3.com",
                    Address=new List<Address>()
                    {

                             new Address()
                             {
                                      Country="Country3",
                                      State="State4",

                                      City="City7",
                             },
                             new Address()
                             {
                                      Country="Country3",
                                      State="State5",
                                      City="City8",

                             },

                             new Address()
                             {
                                      Country="Country3",
                                      State="State6",
                                      City="City9",
                             }
                    }
          }
};

LstCustomers.DataSource = objCustomers;
LstCustomers.DataBind();

 

فایل های ضمیمه

ایمان مدائنی

نویسنده 1299 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

تاکنون هیچ کاربری از این پست تشکر نکرده است

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید