Check this code, this will work fine, if I understand your requirement correctly
protected void Page_Load(object sender, EventArgs e)
{
//Here is the Logic of Getting ads from DB
List<String> Ads = new List<string>();
Ads.Add("Ad1");
Ads.Add("Ad2");
Ads.Add("Ad3");
//Here is the Logic of Getting ads from DB
Ads ctrl;
Session["AdId"] = string.Empty; //Initializing the session variable.
foreach (string item in Ads) //Looping
{
ctrl = Page.LoadControl("Ads.ascx") as Ads; //Loading the usercontrol
//Checking the first item from the collection is same as the value in the session.
if (!item.Equals(Session["AdId"].ToString(),
StringComparison.CurrentCultureIgnoreCase))
{
Session["AdId"] = item; //If not, updates the session
ctrl.AdId = item; //Setting the Property of the user control.
this.MyAds.Controls.Add(ctrl); //Loading it
}
}
}
My User control contains a property and I am using session for it. And if the Datas, Ad1, Ad2 etc, change it Ad1, Ad1 then only one control will load. And I am loading the user controls to a Panel.
Thanks
Anuraj P
http://www.dotnetthoughts.net
THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.