ASp.net c# sql

April 20, 2010

Back button refresh page

There are situation wherein you need to reload the page/control state after browser’s “Back” button is pressed.

For example: If you have a checkbox, If someone clicks on checkbox  postback and selectedindexchange/checkedchange event is fired. Once browser’s back button is pressed, page is refreshed but still the checkbox is checked.

Below is the solution for this.

protected override void OnInit(EventArgs e)

    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);

        base.OnInit(e);
    }

April 15, 2010

Bind XML to Nested Repeater

A simple method to bind nested repeaters with xml upto level3 ( you can go upto n level with this method).
Easiest and simplest method.
<!– Xml file to be used –>
<Root>
<Level1 Name=”Level1″ id=”L1″>
<Level2 Name=”Level2″ id=”L2″ >
<Level3 Value=”28″ id=”L3-1″ sortkey=”1″></Level3>
<Level3 Value=”28″ id=”L3-2″  sortkey=”2″></Level3>
</Level2>
</Level1>
</Root>
<!– End XML –>
<asp:Repeater runat=”server” ID=”rpt1″>
<ItemTemplate>
<%# ((System.Xml.XmlNode)Container.DataItem).Attributes[“Name”].Value%> //Level1
<asp:Repeater runat=”server” ID=”rpt2″  DataSource='<%# ((System.Xml.XmlNode)Container.DataItem).SelectNodes(“Level2”) %>’> //Bind level2 nodes
<ItemTemplate>
<%# ((System.Xml.XmlNode)Container.DataItem).Attributes[“Name”].Value%> //Level2
<asp:Repeater runat=”server” ID=”rpt3″  DataSource='<%# ((System.Xml.XmlNode)Container.DataItem).SelectNodes(“Level3”) %>’> //Bind level3 nodes
<ItemTemplate>
<%# ((System.Xml.XmlNode)Container.DataItem).Attributes[“Value”].Value%> //Level3
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Now Bind the Level1 Repeater with below code..
XmlDocument doc = new XmlDocument();
doc.Load(MapPath(“../../test.xml”)); //Path of your xml
XmlNodeList nodes =
doc.SelectNodes(“Root/Level1”); //The first level you need to bind
rpt1.DataSource = nodes;
rpt1.DataBind();

Create a free website or blog at WordPress.com.