Click here to Skip to main content
15,898,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

XML
<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>


XML
<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>


XML
<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>
Posted
Updated 23-Sep-13 19:37pm
v3
Comments
CodeBlack 24-Sep-13 1:20am    
can you share the aspx code for listview1 and xml
karthik mushyam 24-Sep-13 1:28am    
Plz Check it i attached mt aspx code
karthik mushyam 24-Sep-13 1:23am    
Plz Check it i attached mt aspx code
Thanks7872 24-Sep-13 1:26am    
Use reply button while making comment.
karthik mushyam 24-Sep-13 1:27am    
ok

1 solution

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:
XML
<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
C#
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; }
    }
}
 
Share this answer
 
v2
Comments
karthik mushyam 24-Sep-13 2:36am    
for (int i = 0; i < 10; i++) here 9 rows are assigning to the listview since i<10
when i add the additional row .8 rows are disappearing .and when i delete a single row total listview is deleting ..when the listbox fields are with data how a user can enter the data
CodeBlack 24-Sep-13 3:11am    
There will be 10 rows from 0 to 9.

and here I have added for loop just to create sample datasource for listview as it is a sample code. you have to assign real datasource which you need for the listview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900