tags:

views:

196

answers:

3

I have the following Page_Load function...

protected void Page_Load(object sender, EventArgs e)
{
  XmlDataSource1.Data = GetXmlFromFile(Request.QueryString["file"]);
  XmlDataSource1.DataBind();

  Repeater1.DataBind();                      
}

The page in which this Page_Load resides is called by a parent page. Each time this Page_Load gets called the value of "file" in the query string will be different. So each time I will be receiving the XML contents from a different file which are inserted into the XmlDataSource and then bound against by the Repeater. Getting the XML data from the file works great but when I bind against the repeater that only works the first time through Page_Load. Each time after that when this code is executed the same results from the first XML file are displayed in the repeater.

What am I missing here. How can I get the XML data to be bound against the repeater on each page load instead of just the first one?

+1  A: 

Tried creating a new XmlDataSource on each load?

protected void Page_Load(object sender, EventArgs e)
{
  XmlDataSource source = new XmlDataSource();
  source.ID = "MyDS";
  source.Data = GetXmlFromFile(Request.QueryString["file"]);
  source.DataBind();

  Repeater1.DataSource = source;
  Repeater1.DataBind();                      
}
zincorp
I thought this idea would've worked for sure. But no. Same issue.
Rob Segal
Did you remove the existing XmlDatasource/references to it from the markup before trying this?
zincorp
Yeh. I was actually receiving errors if I didn't remove them first.
Rob Segal
Out of sheer curiosity, set the source.ID to a newly generated value each Page_Load. I have a suspicion that there's some funkiness going on with how the XmlDataSource caches its data.
zincorp
Your curiosity was spot on! That worked. Thanks so much for your help.
Rob Segal
+1  A: 

I tried to duplicate the problem with a simple implementation that follows your main concept.

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["file"] != null)
    {
        XmlDataSource XmlDataSource1 = new XmlDataSource();
        XmlDataSource1.DataFile = "~/App_Data/" + Request.QueryString["file"];
        Repeater1.DataSource = XmlDataSource1;
        Repeater1.DataBind();
    }
}

Markup:

<asp:Repeater runat="server" ID="Repeater1" DataSourceID="XmlDataSource1">
    <ItemTemplate>
        <asp:Label runat="server" ID="lbl" Text='<%# Eval("Name") %>'></asp:Label>
    </ItemTemplate>
</asp:Repeater>

This works when I browse to "Default.aspx?file=West.xml" where west.xml is an xml file in my App_Data folder. When I enter another existing xml file in the query string, it performs as expected. It's quick n' dirty, but maybe it will give you some idea when comparing it against your existing code.

Joel Harris
Yeh I'm not sure why this method would not work for me. Maybe because I am setting the data of the xml data source to a string value instead of a file? zincorp's answer worked for me but I appreciate the feedback.
Rob Segal
That's correct. The DataFile property will only map to a physical file, whereas Data will allow dynamic XML
zincorp
A: 

On further investigation turns out the problem is a caching flag which is set to default true on XmlDataSource's. Other data source types like SqlDataSource do not have this flag enabled.

This is more fully outlined in the following article http://geekswithblogs.net/ranganh/archive/2006/04/28/76638.aspx

Upon disabling the caching feature everything works as expected.

Rob Segal