473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

databinding xml to dropdownlist

I'd like to bind some xml to a dropdownlist

<engines>
<engine name="test1" id="1" />
<engine name="test2" id="2" />
</engines>

If I do:

ddlEngines.DataSource = xmlDoc.SelectNodes("/engines/engine")
ddlEngines.DataBind()

I get
<select name="ddlEngines" id="ddlEngines">
<option value="System.Xml.XmlElement">System.Xml.XmlElemen t</option>
<option value="System.Xml.XmlElement">System.Xml.XmlElemen t</option>
</select>

So it is binding ok, I just need to set the datatextfield and datavaluefield
to the sttributes name and id
I'm not sure how this is achievable
Thanks

Joe Gass
Nov 19 '05 #1
1 2136
Hi Joe,

Welcome to ASP.NET newsgroup.
As for the dropdownlist and xmlnodelist databinding problem you mentioned,
this is because the DropDownList is not a templated databound control(like
datalist ,repeater...), it's a normal databound control , the binded
dataitem need to provide strong-typed "Property", so that we can specify
the "DataTextField", "DataValueField" for it.
As for your scenario, the binded datasource is a XmlNodeList which contains
list of XmlNode type. DropDownList can directly bind with such type and
retreive further info from the XmlNode's Attributes (or sub node....).

I think we have the following alternative means currently:

1. Still use DropDownList, however instead of databinding, we need to
manually use foreach loop to add ListItems into DropDownList according to
the XmlNodeList

2. Still using DataBinding approach, but we need to use template databound
control such as DataList or Repeater. For example, below is a simple
example that use Repeater control to bind a XmlNodeList and display a
select box:

======aspx=========
<asp:Repeater id="rptXml" runat="server">
<HeaderTemplate>
<select id="lstXml">
</HeaderTemplate>
<ItemTemplate>
<option value='<%#
((System.Xml.XmlNode)Container.DataItem).Attribute s["id"].Value %>' >
<%#((System.Xml.XmlNode)Container.DataItem).Attrib utes["name"].Value
%></option>
</ItemTemplate>
<FooterTemplate>
</select>
</FooterTemplate>
</asp:Repeater>
=========code behind==========
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
private void BindData()
{
string xmldata = @"<engines><engine name='test1' id='1' /><engine
name='test2' id='2' /></engines>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmldata);
XmlNodeList nodes = doc.SelectNodes("/engines/engine");
rptXml.DataSource = nodes;
rptXml.DataBind();
}

=============================

Hope helps. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Leon Shaw | last post by:
have two Dropdownlist and one which is containing States and one containing Cities. I know how to do all the little databinding task in visual studios interface as well as set the relationship...
2
by: Leon Shaw | last post by:
have two Dropdownlist and one which is containing States and one containing Cities. I know how to do all the little databinding task in visual studios interface as well as set the relationship...
4
by: JV | last post by:
It's easy to databind a listbox or dropdownlist if all you want is to fill it with a list of values. There are plenty of examples in the online help. Unfortunately, real world applications...
2
by: Nathan Sokalski | last post by:
I have several DropDownList controls on my page that use databinding. However, I want to give users the option of selecting a choice such as "None Selected" or something else that shows they did...
0
by: Simon Gregory | last post by:
I am currently attempting to figure out how the new databinding stucture works in ASP.NET 2.0 after working with v1.0 & v1.1 for several years. It seems that if you wish to do set up databinding...
1
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. ...
0
by: brian.newman | last post by:
I've created a dropdownlist whose values are drawn from an Access database. This list of items represents a user account. In the same record is a boolean field which records whether the user is...
3
by: Tor Inge Rislaa | last post by:
DropDownList and Databinding I have a DropDownList on my WebFrom, bound to a data source. When the form is loaded the first Item in the list of the control is selected by default. Is there a...
0
by: =?Utf-8?B?cGhpbGptY2c=?= | last post by:
I've seen this question asked 100 times, but with no reasonable answer, other than to extend a control etc. Sorry if I'm going over old ground. I've a DropDownList inside a FormView control,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.