将xml数据绑定到listview中的下拉列表 [英] Bind xml data to dropdownlist which is in listview

查看:65
本文介绍了将xml数据绑定到listview中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于绑定到下拉列表的第一行Xml数据,当我将另一行添加到列表视图时.xml数据在fisrt行中被清除,并且从第二行开始没有数据显示。

protected void BindDataToGridviewDropdownlist()

{

DataSet dsDept = new DataSet

dsDept.ReadXml(Server.MapPath(XMLFile2.xml));

foreach(listview1.Items中的var list)

{

if(list.ItemType == ListViewItemType.DataItem)

{

DropDownList ddf =(DropDownList)list.FindControl(ddldatatype);



ddf.DataSource = dsDept;

ddf.DataTextField =value;

ddf.DataBind();

}

}



Plz建议我这个代码有什么问题



For first row Xml data in binding to the dropdownlist ,when i add the another row to the list view .xml data is cleared in fisrt row and no data showing from second row onwards.
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();
}
}

Plz suggest me what is the problem with this code

<asp:ListView ID="listview1" runat="server">
       <LayoutTemplate>
           <table border="0px" cellpadding="1">
               <tr style="background-color: #E5E5FE">
                   <th>
                       Order
                   </th>
                   <th>
                       Column Title
                   </th>
                   <th>
                       DataType
                   </th>
                   <th>
                       Exapmles
                   </th>
                   <th>
                       Options
                   </th>
                   <th>
                      Delete
                   </th>
               </tr>
               <tr id="itemplaceholder" runat="server">
               </tr>
           </table>
       </LayoutTemplate>
       <ItemTemplate>







<tr>

              <td>
                  <asp:Label ID="txtorder" Text='<%#Eval("Order") %>' runat="server"></asp:Label>
              </td>
              <td>
                  <asp:TextBox ID="txtcolumnname" Text='<%#Eval("Columnname") %>' runat="server" Width="150"></asp:TextBox>
              </td>

              <td>
              <asp:DropDownList ID="ddldatatype" runat="server" Width="160"   >

              </asp:DropDownList>
              </td>
              <td>
                  <asp:DropDownList ID="ddlexamples"  runat="server" Width="160">
                    <asp:ListItem Text="test" Value="2"></asp:ListItem>
                  </asp:DropDownList>
              </td>
              <td>
              <asp:TextBox ID="txtoptions" Text='<%#Eval("Options") %>' runat="server" Width="150" ></asp:TextBox>
              </td>
              <td>
             <asp:CheckBox ID="chkdel" runat="server" />
              </td>
          </tr>
      </ItemTemplate>
      <AlternatingItemTemplate>
          <tr style="background-color: #EFEFEF">
              <td>







<asp:Label ID="txtorder" Text='<%#Eval("Order") %>' runat="server"></asp:Label>
              </td>
              <td>
                  <asp:TextBox ID="txtcolumnname" Text='<%#Eval("Columnname") %>' runat="server" Width="150"></asp:TextBox>
              </td>
              <td>
              <asp:DropDownList ID="ddldatatype" runat="server" Width="160" >

              </asp:DropDownList>
              </td>
              <td>
                  <asp:DropDownList ID="ddlexamples" runat="server" Width="160">
                    <asp:ListItem Text="test" Value="2"></asp:ListItem>
                  </asp:DropDownList>
              </td>
              <td>
              <asp:TextBox ID="txtoptions"  Text='<%#Eval("Options") %>' runat="server" Width="150"></asp:TextBox>
              </td>
              <td>
             <asp:CheckBox ID="chkdel" runat="server" />
              </td>
          </tr>







XMl




XMl

<DataTypes>
  <DataType value="Name">
    <DataItems>
      <MaleName>MaleName(John)</MaleName>
      <FemaleName>FemaleName(Shermilla)</FemaleName>
      <AnyGender>AnyGender</AnyGender>
    </DataItems>
  </DataType>

  <DataType value="Email">
    <DataItem>
      <Not>Does not exists</Not>
    </DataItem>
  </DataType>

  <DataType value="Phone">
    <DataItem>
      <A>India</A>
      <B>Canada</B>
      <C>Australia</C>
    </DataItem>
  </DataType>

  <DataType value="Date">
    <DataItems>
      <Hh>Date Format</Hh>
    </DataItems>
  </DataType>

  <DataType value="Company">
    <DataItem>
      <Aa>Does not exists</Aa>
    </DataItem>
  </DataType>

  <DataType value="City">
    <DataItem>
      <Bb>No examples available</Bb>
    </DataItem>
  </DataType>

  <DataType value="Postal">
    <DataItem>
      <Cc>No examples availabe</Cc>
    </DataItem>
  </DataType>

  <DataType value="Country">
    <DataItem>
      <Gg>No examples available.</Gg>
    </DataItem>
  </DataType>
  <DataType value="Latitude/Longitude">
    <DataItem>
      <Ff>
        No examples available.
      </Ff>
    </DataItem>
  </DataType>
  <DataType value="Street Address">
    <DataItem>
      <Ee> No examples available.</Ee>
    </DataItem>
  </DataType>
  <DataType value="Region">
    <DataItem>
      <Dd>No examples available.</Dd>
    </DataItem>
  </DataType>
  <DataType value="Fixed Number of Words"></DataType>
  <DataType value="Random Number of Words"></DataType>
  <DataType value="Alpha Numeric"></DataType>
  <DataType value="Auto Increment"></DataType>
  <DataType value="Number Range"></DataType>
  <DataType value="GUID"></DataType>
  <DataType value="Currency"></DataType>
  <DataType value="Constant"></DataType>
  <DataType value="Composite"></DataType>
  <DataType value="Custom List"></DataType>
</DataTypes>

推荐答案

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; }
    }
}


这篇关于将xml数据绑定到listview中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆