1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How To Access One UserControl from Another UserControl

How To Access One UserControl from Another UserControl

6 posts, 1 answered
  1. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    30 Aug 2009
    Link to this post
    Dear all,
    Let say a have two user control which is displayed in different page, for example

    UserControl1.ascx
    has one button and displayed in default.aspx
    UserControl2.ascx has one lable and displayed in default2.aspx

    If user click the button in UserControl1.ascx then label.text in UserControl2.ascx would show "TEST"

    so how to access one UserControl from another UserControl programmatically?
    Would any body give me a complete code cause i'm novice about this

    Thanks for help

    Best regards,
    Febry



     
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    30 Aug 2009
    Link to this post
    Hello CafeDelMar,

    Below is a sample that illustrates desired behavior - in this sample we are accessing GenericContent control that is located on different page called "mypage" and we are setting its Content to the current control Textbox1.Text.

           CmsManager cManager = new CmsManager(); 
            IList allPages = cManager.GetPages(); 
            foreach (ICmsPage page in allPages) 
            { 
                if (page.Name.Equals("mypage")) 
                { 
                    ICmsPage editPage = cManager.GetPage(page.ID, trueas ICmsPage; 
                    foreach (ICmsWebControl cwControl in editPage.Controls) 
                    { 
     
                        GenericContent gcControl = cwControl.LoadControl() as GenericContent; 
                        gcControl.Content = Textbox1.Text; 
                        cManager.SaveControl(cwControl); 
                         
                    } 
                   
                } 



    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    05 Sep 2009
    Link to this post
    Hello Ivan

    Thanks for your support anyway

    I'm sorry cause i don't give you information clearly

    By the way i've tried your sample code but i made little modification because i have all page with same page name (default) actually, such as
    "~/home/index/default.aspx"
    "~/trial/activation/default.aspx"
     
    This is what i had done

    1. user control behinde code of "~/trial/activation/default.aspx"
                    so sample code become

                     CmsManager cManager = new CmsManager();
                    IList allPages = cManager.GetPages();
                    foreach (ICmsPage page in allPages)
                    {
                        if (page.DefaultUrl.Equals("~/home/index/default.aspx"))
                        {
                            ICmsPage editPage = cManager.GetPage(page.ID,true) as ICmsPage;
                            foreach (ICmsWebControl cwControl in editPage.Controls)
                            {
                                Label lblLoginErrorMessage = cwControl.LoadControl() as Label;
                                lblLoginErrorMessage.Text = "Account is already activated";
                                cManager.SaveControl(cwControl);
                            }
                        }
                     }
     
    2. lblLoginErrorMessage is located in user control of "~/home/index/default.aspx"

    so i got error when run this code, it says error CS1502: The best overloaded method match for 'Telerik.Cms.CmsManager.GetPage(System.Guid, bool)' has some invalid arguments

    do i miss something??

    Thanks

    Regards,
    Febry

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    06 Sep 2009
    Link to this post
    Hi CafeDelMar,

    One of the parameters under GetPAges method does not return proper values - page.ID should be a Guid, and then we set true for the boolean parameter - we are getting this page for editing.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    12 Sep 2009
    Link to this post
    Hi Ivan

    I found your statement from other thread (http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/set-a-ascx-properties-programmatically.aspx), you said:
    "We do not have API that illustrates setting the properties directly without creating an instance. There are two ways that you can use to achieve the desired functionality:

    1. Set the properties via reflection.
    2. Creating interfaces for your control and then cast it to the corresponding interface."

    Would you give me full sample code to implement two ways you said so i can use it to set user control property programatically?

    As far as i know, i use session to communicate between two user control in different page

    Kind regards,
    Febry


  6. Dido
    Dido avatar
    149 posts
    Registered:
    24 Sep 2012
    14 Sep 2009
    Link to this post
    Hi CafeDelMar,

    I am not very sure you need to actually access the controls themselves. As I understand, you need to transfer some data from one page to another. This is a classical scenario for any web application.

    If the data you need to transmit is simple and short, you can use query string; otherwise, you could use a post response.

    The receiver of the data would check its request and would display the necessary data.

    If transfer your data, you stay blissfully independent on the page that sends the data. Which is good, because you can modify the sender of the data as much as you want without having to modify it.

    If absolutely must access the first page, do so with previous page property. Have in mind that when this page is accessed, ASP.NET re-creates the page with a limited life cycle, which in some occasions might lead to strange results.


    Kind regards,
    Dido
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
Register for webinar
6 posts, 1 answered