Binding a Textbox to a specific value in an xml document

F

Flyguy

Hello, I am starting to use ASP.NET in Visual Studio 2005 and can’t figure
out how to bind a textbox control to a specific value inside an xml document.


As an example, I have an xml file on the server containing the following text:

<?xml version="1.0" encoding="utf-8" ?>
<site>
<page1>
<title>This is my title</title>
<body>This is my body</body>
<image1>img1.gif</image1>
<image2>img2.gif</image2>
</page1>
<page2>
<body>This is the body for page two</body>
</page2>
</site>

I want to make a web page with five text boxes and one button. The
following lists the mapping to the values I want to be able to update.

Textbox1 – site/page1/title
Textbox2 – site/page1/body
Textbox3 – site/page1/image1
Textbox4 – site/page1/image2
Textbox5 – site/page2/body

I also want the xml file to be updated by pressing the button. How do I do
this in VS 2005? I am using C#.

Thanks
 
W

Walter Wang [MSFT]

Hi,

Do you mean bind a TextBox to the path of an xml element (such as
"/site/page1/title") or the element's value (such as "This is my title")? I
will assume you're referring to the latter case. Let me know if this is not
the case.

To bind to xml data, you need to use XmlDataSource control
(http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.xmldatas
ource.aspx); and you need to put the TextBox inside a container control
that supports binding to data source controls, such as GridView, FormView,
or DetailsView.

You will also need to modify your xml data as:

<?xml version="1.0" encoding="utf-8" ?>
<site>
<page index="1">
<title>This is my title</title>
<body>This is my body</body>
<image1>img1.gif</image1>
<image2>img2.gif</image2>
</page>
<page index="2">
<body>This is the body for page two</body>
</page>
</site>

This will make the XPath expression "/site/page" to return a list of xml
nodes for binding purpose.

Following example code will bind the first page element to a FormView:

<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/XMLFile.xml" XPath="/site/page"></asp:XmlDataSource>


<asp:FormView ID="FormView1" runat="server"
DataSourceID="XmlDataSource1" >
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%#
XPath("title") %>'></asp:Label>
<br />
<asp:Label id="lblBody" runat="server" Text='<%#
XPath("body") %>'></asp:Label>
<br />
<asp:Label ID="lblImage1" runat="server" Text='<%#
XPath("image1") %>'></asp:Label>
<br />
<asp:Label ID="lblImage2" runat="server" Text='<%#
XPath("image2") %>'></asp:Label>
</ItemTemplate>
</asp:FormView>


However, XmlDataSource doesn't support two-way binding by default, which
means you will have to handle the updating yourself:

#Updating an XML file
http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1396

If you want to use GridView to bind the xml file, you will also need to
watch out that if you're using GridView's default BoundField column type,
it will only bind to the xml node's attributes (in this case, node page's
attributes), to bind to its child node's content, you will have to either
use ItemTemplate to setup each column's binding controls manually, or
create a XSLT file to transform the xml first:

#GridView and XmlDataSource
http://www.codenewsgroups.net/group/microsoft.public.dotnet.framework.aspnet
..webcontrols/topic8546.aspx


Let me know if you need further information on this post. Thanks.

Happy New Year!

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top