Accessing Global Objects inside a User Control

    Date: 10/31/06 (Asp Dot Net)    Keywords: database, asp

    I have a question. I have created a class (DBClass) to act as a handler for interactions with a database. Creating an instance of that class opens a connection to the database, and the object then has a number of methods for handing transactions with the database.

    Initially, my thought was to include the class definition file (which also creates a single instance of the object, called DB) at the beginning of each document. Then, I thought, I'd be able to reference the DB object anywhere in the page to interact with the database.

    So, this is (a simplified version of) what my main page looks like:

    
    <%@ Register TagPrefix="inc" TagName="title" Src="title.ascx" %>
    
     
      Side Bar Template
     
     
      
     
    
    

    The dbclass.aspx file contains the code that defines the DBClass class and creates a single instance of that class called DB.

    Now, the title.ascx file defines a User Control that prints the title of the page, by looking up the URL of the page in a database and retrieving the corresponding title. That look-up in the database is handled by the DB object:

    <%@ Control Language="VB" ClassName="TitleControl" %>
       

    <% Response.Write( DB.GetNameFromUrl( Request.ServerVariables("URL") ) ) %>

    But when I run the main page, it gives a compilation error then trying to compile the Title Control, because I reference the DB object without declaring it inside that file (title.ascx).

    Any idea how I can get the user control to be "aware" of the main DB object I've defined in the calling page?

    Thanks for any help or insight..

    Source: http://community.livejournal.com/aspdotnet/78710.html

« Automatic capitalization in... || Free icons for ASP.NET apps »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home