tags:

views:

348

answers:

3

I'm setting up a simple page that just displays the contents of an XML file and allows the user to filter by keyword.

I'm a serious .NET newb, but I've got an XmlDataSource set up:

<asp:XmlDataSource ID="RSSFeedDataSource" runat="server"  DataFile="test.xml"
        XPath="/rss/channel/item[contains(title,"theKeyword")]"></asp:XmlDataSource>

So I want to allow users to type in a keyword and change the data source based on that.

I figured I'd do a postback with the keyword from a text input, grab that keyword and somehow insert into the XPath expression...

I'm going for simplicity at this point, but would this be the simplest approach for this?

A: 

You might be able to do it from within the page itself, like so:

<asp:XmlDataSource ID="RSSFeedDataSource" runat="server" 
  DataFile="test.xml"
  XPath="/rss/channel/item[contains(title,"<%=Request.QueryString["filter"] %>")]"></asp:XmlDataSource>

If that doesn't work, why not just set the XPath property in the code-behind using the query string or post values?

casperOne
A: 

Method to grab it via code behind and a button postback

ASP Page:

<asp:TextBox id="txtKeyword" runat="server" />
<asp:Button id="btnKeyword" OnClick="btnKeyword_Click" runat="server" />

Code behind:

public void btnKeyword_Click(Object sender, EventArgs e)
{
  // Assuming C#
  // Retrieve the keyword from the text box
  string keyword = txtKeyword.Text

  // Next step would be to modify the XPath of your XmlDataSource
  RSSFeedDataSource.XPath = "/rss/channel/item[contains(title," + keyword + ")]"
}
Gavin Miller
Worked like a charm, thank you
A: 

on the postback, you should be able to do

RSSFeedDataSource.XPath = "/rss/channel/item[contains(title,'" + txtKeyword.Text + "')]";

assuming the keyword is coming from a server control with id txtKeyword

Jason