Kerala Microsoft Users Group
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.

Usercontrol communication

Latest post 03-24-2009 2:31 AM by Anuraj. 4 replies.
  • 03-23-2009 7:42 AM

    Usercontrol communication

    Hi All,

         I have developed a User control for displaying of Ads in my website, and I have placed multiple controls in my page and I do not want to duplicate my ads, for eg: if I have three controls and only one ad available only one controls should work and load, the other two should not. My present implementation will duplicate twice in other controls. Is there any way to know the first control has already loaded the Ad. How we handle this scenoria in Asp.net pages

    Thanks,

    Rejeev.

     

     

     

    • Post Points: 25
  • 03-23-2009 9:19 AM In reply to

    Re: Usercontrol communication

    I dont think you can do it from the User Control itself. Better to use Page Level functions. Because basically the user controls are seperate components, they didn't know whether user control loaded or not. Otherwise you may need to use session or View state, but I am not sure

    Page Level

    For each user control
    Check for the AD -
    If exists exit For / Else Continue
    Next

    User Control Level

    UserControl_Load event
    if session("Ad") = 1
    True - me.visible = false
    False - Set session("Ad")
    End if

    I recommend to use Page Level

    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.

    • Post Points: 25
  • 03-23-2009 9:31 AM In reply to

    Re: Usercontrol communication

    Hello,

    You should never get User Controls to communicate directly with one another. To do so violates the whole point of User Controls: that they are self-contained controls that can be placed on any page, as many times as necessary, no matter what else may be on that page.

    Please find the sample code from the following URL.

    http://forums.asp.net/p/887610/933681.aspx

     

     

    Regards

    Sreejumon [MVP]

    http://sreesharp.com/blog

    • Post Points: 25
  • 03-24-2009 1:13 AM In reply to

    Re: Usercontrol communication

    Thanks Anuraj and Sreejumon, since I have only one usercontrol in the page I was planning to communicate with these I don't think so thats possible and not effective way as you all suggest.

    If we are going for Page level as Anuraj said, the user controls loads after page loads right, then how we check the whether the control has loaded the Ads and also the page is passing the filters(like country,state,region etc) to the UC to load the Ads and Ads are loaded from inside the UC. In this scenario how we check whether any controls have loaded this Ads.

    I am totally stuck here please help

    Thanks,

    Rejeev.

     

    • Post Points: 25
  • 03-24-2009 2:31 AM In reply to

    Re: Usercontrol communication

    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.

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.