Can anyone give me some pointers on how to display the results of an XPath query in a textbox using code (C#)? My datascource seems to (re)bind correctly once the XPath query has been applied, but I cannot find how to get at the resulting data.
Any help would be greatly appreciated.
views:
1153answers:
3Some more information would be nice to have to be able to give you a decent answer. Do you have any existing code snippets you could publish here?
The general idea is to use the XmlDataSource.XPath property as a filter on the XmlDataSource.Data property. Did you try displaying the contents of the Data prop in your textbox?
Based on a slection in a DropDownList, when the SelectedIndexChanged event fires, the XPath for an XMLDataSource object is updated:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
XMLds.XPath = "/controls/control[@id='AuthorityType']/item[@text='" + ddl.SelectedValue + "']/linkedValue";
XMLds.DataBind();
}
The XPath string is fine, I can output and test that it is working correctly and resolving to the correct nodes. What I am having problems with, is getting at the data that is supposedly stored in the XmlDataSource; specifically, getting the data and outputting it in a TextBox. I'd like to be able to do this as part of the function above, i.e.
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
XMLds.XPath = "/controls/control[@id='AuthorityType']/item[@text='" + ddl.SelectedValue + "']/linkedValue";
XMLds.DataBind();
myTextBox.Text = <FieldFromXMLDataSource>;
}
Thank you for your time.
XMLDataSource is designed to be used with data-bound controls. ASP.NET's TextBox is not a data-bound control. So to accomplish what you want you either have to find a textbox control with data binding or display the result in some other way.
For example, you could use a Repeater control and create your own rendering template for it.
<asp:Repeater id="Repeater1" runat="server" datasource="XMLds">
<ItemTemplate>
<input type="text" value="<%# XPath("<path to display field>")%>" />
</ItemTemplate>
</asp:Repeater>