databinding xml to dropdownlist

J

Joe Gass

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.XmlElement</option>
<option value="System.Xml.XmlElement">System.Xml.XmlElement</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
 
S

Steven Cheng[MSFT]

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).Attributes["id"].Value %>' >
<%#((System.Xml.XmlNode)Container.DataItem).Attributes["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.)
 
Joined
Aug 28, 2008
Messages
19
Reaction score
0
You can do it with a few line of codes using the dataset. Try this tutorial here. http : / /w w w.itjungles.com/dropdownlist/the-easiest-way-to-populate-drop-down-list-with-xml-in-c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top