且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将xml数据绑定到listview中的下拉列表

更新时间:2021-08-15 17:56:31

The issue is that your list view does not contain any data. For that you will have to first bind the ListView ’listview1’ then bind the drop down. I have created a sample code for the same as mentioned below :



ASPX Code:

The issue is that your list view does not contain any data. For that you will have to first bind the ListView 'listview1' then bind the drop down. I have created a sample code for the same as mentioned below :

ASPX Code:
<asp:ListView ID="listview1" runat="server">
               <LayoutTemplate>
                   <table border="0px" cellpadding="1">
                       <tr style="background-color: #E5E5FE">
                           <th>Order
                           </th>
                           <th>ColumnTitle
                           </th>
                           <th>DataType
                           </th>
                       </tr>
                       <tr id="itemplaceholder" runat="server">
                       </tr>
                   </table>
               </LayoutTemplate>
               <ItemTemplate>
                   <tr>
                       <td>
                           <asp:Label ID="txtorder" Text='<%#Eval("OrderNo") %>' runat="server"></asp:Label>
                       </td>
                       <td>
                           <asp:TextBox ID="txtcolumnname" Text='<%#Eval("ColumnTitle") %>' runat="server" Width="150"></asp:TextBox>
                       </td>
                       <td>
                           <asp:DropDownList ID="ddldatatype" runat="server" Width="160">
                           </asp:DropDownList>
                       </td>
                   </tr>
               </ItemTemplate>
               <AlternatingItemTemplate>
                   <tr style="background-color: #EFEFEF">
                        <td>
                           <asp:Label ID="txtorder" Text='<%#Eval("OrderNo") %>' runat="server"></asp:Label>
                       </td>
                       <td>
                           <asp:TextBox ID="txtcolumnname" Text='<%#Eval("ColumnTitle") %>' runat="server" Width="150"></asp:TextBox>
                       </td>
                       <td>
                           <asp:DropDownList ID="ddldatatype" runat="server" Width="160">
                           </asp:DropDownList>
                       </td>
                   </tr>
               </AlternatingItemTemplate>
           </asp:ListView>







ASPX.cs Code




ASPX.cs Code

public partial class XmlBinding : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var orderList = new List<order>();
            for (int i = 0; i < 10; i++)
            {
                orderList.Add(
                    new Order()
                        {
                            OrderNo = i,
                            ColumnTitle = "Title_" + i
                        }
                    );
            }

            this.listview1.DataSource = orderList;
            this.listview1.DataBind();

            this.BindDataToGridviewDropdownlist();
        }

        protected void BindDataToGridviewDropdownlist()
        {
            DataSet dsDept = new DataSet();

            dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
            foreach (var list in listview1.Items)
            {
                if (list.ItemType == ListViewItemType.DataItem)
                {
                    DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");

                    ddf.DataSource = dsDept;
                    ddf.DataTextField = "value";
                    ddf.DataBind();
                }
            }
        }
    }

    public class  Order
    {
        public int OrderNo { get; set; }

        public string ColumnTitle { get; set; }
    }
}