Sharing data between controls?

Last post 03-11-2007 8:07 AM by littlecharva. 2 replies.

Sort Posts:

  • Sharing data between controls?

    03-09-2007, 9:51 AM
    • Member
      122 point Member
    • littlecharva
    • Member since 12-16-2003, 2:46 AM
    • Posts 31

    Hi,

    I'm using the followin basic ASP.NET:

    <%@ Register TagPrefix="Test" Namespace="Test.Controls" Assembly="Test.Controls" %>
    <html>
    <body>
    <Test:ContentBox id="Content" runat=server/>
    </body>
    </html>

    The Custom Control simply opens an XML file loads some data and then renders itself depending upon the data.  It works fine and I love it as it's nice and clean in the aspx file as you can see.

    I want to now add another control in that controls the title of the page:

    <%@ Register TagPrefix="Test" Namespace="Test.Controls" Assembly="Test.Controls" %>
    <html>
    <head>
    <title><Test:TitleBox id="Title" runat=server/></title>
    </head>
    <body>
    <Test:ContentBox id="Content" runat=server/>
    </body>
    </html>

    The problem I have now, is that I'm opening and parsing the XML file twice, and I have another idea of where else I want to use the data on the page, so it'll be opened three times.

    Is there an easy way to share the data between the Controls once I've parsed it from the XML file, without having to resort to adding a Page_Load event to do the legwork?

    I really like the fact that my page doesn't have a code behind file or any code within it, just a few components and I'd like to keep it that clean and simple.

    Littlecharva

    "Test" Namespace="Test.Controls" Assembly="Test.Controls" %>
  • Re: Sharing data between controls?

    03-09-2007, 11:56 AM
    • Member
      22 point Member
    • pzolja2
    • Member since 01-31-2007, 10:27 PM
    • Posts 24

    If you're using 2.0 you can just simply use Page.Title, but I'm guessing you're looking for a more universal solution. There are two approaches that I can think of right now. One would be to use Page.Items and store whatever information you want in there (you could store a whole class). However, Page.Items will only work in 2.0 and up. Of course you can always use the Session variable or the ViewState and store it there... but that's a bit sloppy Smile. Another way would be to have a main user control and have the other user controls search for the main control. The main control would expose some properties that the secondary controls can use. If you're a perfectionist you'd probably wrap this in interfaces as well... There are several ways to do it, with each having its ups and downs. It also depends on the scope of your solution. Would this be used in other projects, perhaps by other people? If so I'd go for the last method as it's not dependant on an object (Session / Page.Items) that could be altered by a different control; however, it's more involved... If it's used only in your project storing stuff in Page.Items it's not so "bad." Have a good one.

  • Re: Sharing data between controls?

    03-11-2007, 8:07 AM
    • Member
      122 point Member
    • littlecharva
    • Member since 12-16-2003, 2:46 AM
    • Posts 31

    Thanks pzolja2, I think we're using 2.0, but I find the interfaces option quite interesting as I've never really put them to use, and this seems like a good opportunity.

    Thanks again,
    Littlecharva

Page 1 of 1 (3 items)