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.