Results 1 to 3 of 3

Thread: User Controls

  1. #1
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default User Controls

    when using User Controls (ascx), how do you refer to a function declared within the main aspx page?

    basically, i have a general database connection function i'd like to refer to...

    or does this go against what a user control is? ie, should everything be dumped into the suer control so it has no dependency on the top page, and can simply be placed on any .net page and work...?? - coz that's now what i've done!

    coming back to this, there are still general functions used throughout (ie, a little function to change quotes etc) should these be in the global.asax file?

    SO! my question is, where do i store global functions in asp.net? in a 'utility' class? but where does this class go?

    many thanks.

  2. #2
    Join Date
    Jun 2005
    Posts
    1,081
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    OK, this is not as easy to accomplish, however you said you have some general database function in the main page. First question is does this code appear on every page (or at least would it be beneficial to have on every page)? If so you create your own class that inherits the Page class. You can then reference the function in a user control like the code below (it is possible to reference the main page class, but it is trickier in ASP.Net 2 due to the way the namespaces are created):

    C# Code
    Code:
    MyPageClass myPage = (MyPageClass)this.Page;
    myPage.TheDbFunction();
    I'm actually thinking that this Database Generic Function should probably be implemented in a static/shared class which simply means you put a file in the App_Code folder, and would look like:

    C# .Net v2 Code (static / shared classes not available in v1).
    Code:
    public static class MyDbRoutines
    {
        public static void MyGenericFunction()
        {
              //code for generic function
        }
    }
    C# .Net v1 Code
    Code:
    public class MyDbRoutines
    {
        ///<summary>
        /// Private constructor so class cannot be instantiated
        ///</summary>
        private MyDbRoutines()
        {}
     
        public static void MyGenericFunction()
        {
              //code for generic function
        }
    }
    Of course, this should explain what you need to do for a generic 'Utility' class. As your code gets bigger you could look at putting this code into a seperate assembly which can then be referenced in the web application and allow you to have multiple websites using the same code-base. As an example of this I have a common library that I include with all projects, this library provides several functions that I commonly using, for example a routine to CheckFilenames for invalid characters. As you develop more and more code you would start to create multiple libraries (I have been using .Net since early alpha and now have several assemblies I link in depending on functionality I need).

  3. #3
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    I appreciate the time for you to answer my queries Sol, I just need some simple guidance!

    Just to confirm - I can create my own Namespaces with functions and plonk them into App_Code and all web pages which import my created namespace will be able to access the functions, ie: mynamespace.myfuction(myvariables)

    ...and this will work for 1.1 and 2.0? (I tinker at home with RF and use Donhost during the working week) Donhost don't support 2.0.

    If this is the case... oh so simple. I've been reading I needed to compile .vb files and put them into the /bin.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. user control nightmare...
    By psykik in forum ASP.NET
    Replies: 5
    Last Post: 20th June 2006, 02:26 PM
  2. Fancy a spot of user testing?
    By nick in forum For Sale, Sites and Services
    Replies: 31
    Last Post: 18th April 2006, 04:42 PM
  3. New user seeking WordPress help
    By markcannon in forum General Technical Support
    Replies: 3
    Last Post: 23rd February 2006, 10:46 AM
  4. I like what I see - user feedback request
    By hazydavy in forum Sales and Service Feature Enquiries
    Replies: 7
    Last Post: 6th September 2005, 11:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •