473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Data Source = xmlDoc.SelectNo des("/engines/engine")
ddlEngines.Data Bind()

I get
<select name="ddlEngine s" id="ddlEngines" >
<option value="System.X ml.XmlElement"> System.Xml.XmlE lement</option>
<option value="System.X ml.XmlElement"> System.Xml.XmlE lement</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 2156
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.Xm lNode)Container .DataItem).Attr ibutes["id"].Value %>' >
<%#((System.Xml .XmlNode)Contai ner.DataItem).A ttributes["name"].Value
%></option>
</ItemTemplate>
<FooterTemplate >
</select>
</FooterTemplate>
</asp:Repeater>
=========code behind========= =
private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{
BindData();
}
}
private void BindData()
{
string xmldata = @"<engines><eng ine name='test1' id='1' /><engine
name='test2' id='2' /></engines>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml data);
XmlNodeList nodes = doc.SelectNodes ("/engines/engine");
rptXml.DataSour ce = 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
2169
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 (parent, child, etc.). In the code behind file in page_load event I fill the data adapter and databind the State dropdownlist. I also set the on_change event for the Cities dropdownlist after a state is selected. However, When I run this page in the...
2
3551
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 (parent, child, etc.). In the code behind file in page_load event I fill the data adapter and databind the State dropdownlist. I also set the on_change event for the Cities dropdownlist after a state is selected. However, When I run this page in the...
4
1326
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 demand more. Usually you would also like to bind the SelectedValue to something else. Common scenario: You have an ADDRESS table with a foreign key to a STATE table. You join on an integer or uniqueidentifier key value, but the STATE table is what...
2
1715
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 not make a selection. However, if I attempt to add a ListItem to the Items collection when databinding is used it is removed and replaced by the item in the databinding source. Is there any simple way to add an extra ListItem, or do I have to build...
0
1300
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 at Design Time, you are restricted to using one of the 5 xxxDataSource controls. The data in our websites are usually obtained via a set of webservices and are contained in a set of strongly typed relational DataSets.
1
14314
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. Both the grid and the dropdown box are retrieving and displaying data fine, I just cant bind the two together. I followed the instructions in the help document called 'Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web...
0
1329
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 an administrator. On my web form, I want a checkbox labelled "Is Admin" which will take the appropriate value from the Access database depending on which value is chosen in the dropdownlist. I'm using a checkboxlist in this case as it has...
3
1346
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 line of code that can prevent any item to be selected initially.
0
1325
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, which is hooked up to a GridView. The contents of the DropDownList are being built from an ObjectDataSource using business objects. Problem is, I'm binding the DropDownList's SelectedValue to a property of type int?, and and where adding...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.