Could you hire me? Contact me if you like what I’ve done in this article and think I can create value for your company with my skills.

August 25, 2007 / by Zsolt Soczó

Az aljas XmlDataSource cachelés

Az egyik kedves ismerősöm keresett meg egy problémával, amiben véletlenszerűen gond volt egy DataBind-olt DropDownList-tel.

Kicsit körbejárva a problémát kiderült, hogy az XmlDataSource alapban cache-el keményen, ami statikus XML fájlok esetén nem lenne nagy gond, sőt, de dinamikus bemenetnél alattomos hibákat okozhat.
Az alábbi kódot párszor futtatva azonnal látszik a cache-elés okozta hiba, míg a xds.EnableCaching = false; sor hatására megszűnik.

[source:xhtml]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>




Untitled Page




[/source]

[source:csharp]
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
int a;
string[] gy = { “alma”, “korte” };

protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource xds = new XmlDataSource();
//xds.EnableCaching = false;
this.Controls.Add(xds);
xds.ID = “d”;
a = new Random().Next(0, 2);

xds.Data = string.Format(
@”


“,
a, gy[a]);
xds.XPath = “Data/Row”;
ize.DataSourceID = “d”;
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);

int b = int.Parse(ize.Items[0].Value);
if (b != a)
{
throw new InvalidOperationException(
string.Format(
“Baj van, elvárt érték a lista első elemében: {0}, tényleges: {1}”,
a, b));
}
}
}

[/source]

Could you hire me? Contact me if you like what I’ve done in this article and think I can create value for your company with my skills.