Usercontrol and Variables

Posted on 2004-11-28
Last Modified: 2011-09-20
Im really new to ASP.NET and have a background in ASP VB coding.
I have been reporgramming a webpage from ASP to the last week and basically rewritten most of it.
Forms and formchecks work great but the rest of ASP.NET is kinda abacadabra for me.

I have some website data stored in an Access database (general info about the site, keywords, title etc) i use this info from a table cuz I want to make the site multilingual.
Im trying to write this data to variables in my site which I can use in all scripts on the site.
So I include this ascx file and in this file I try this:

Sub PageLoad(ByVal sender As System.Object, ByVal e As System.EventArgs)
                ........................ 'not important code for this question
      While DBReader.Read()
            Dim strSiteInfoKeywords As String = DBReader("site_info_keywords")
            Dim strSiteInfoKeywordslong As String = DBReader("site_info_keywordslong")
            Dim strSiteInfoHome As String = DBReader("site_info_home")  
      End While
End Sub

So I thought I could use these values in the rest of my site, since this ascx usercontrol is used on each page.
But somehow the variables declared in here cant be used in the ASPX files.

Why is this and how can I fix this????

Variables from includes files in good old ASP could be used vice versa (base file to include, include file to base) without any hassle....
I need this project fixed this comming weeks, this crap gives me some serious headaches :)

Question by:fryslan
    LVL 2

    Accepted Solution

    As long as this data is general for the application you can use the application object to store this data and to retreive it each time a new page is loaded. In order to do this you need to handle the Application_Init event that occurs when the application is initiated. You retreive the data from you Access database and you can store a DataSet or DataTable with the fetched data in your Application object.


    Author Comment


    Thanx alot for that, I thought I could only define those Application objects in the Global.asax, but it works for the rest too. But what about another thing. I define a value Dim intId in the Ascx file, and I wanna use it in the Aspx file that uses that Ascx file? Is there a way to do that, these variables change depending on a Querystring in the URL, so its not smart to use Application object, right??
    LVL 2

    Expert Comment

    You can use the Session object for variables that change for each user across the pages and the Application object for those variables that are not changing.


    Author Comment

    OK, session variables it is :)
    Do I need to close these session variables on every page then too?
    And why cant I declare the var intId like global, so I can use it in all files which are used in that page, just like in good ol ASP.
    LVL 2

    Expert Comment

    Session variables will be available through the user's session. The session will end when the user logs out or when he closes the browser.
    You can use cookie variables if you like to...

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity C# Stream to WorkBook 5 43
    Disable & as shortcut in label 3 26
    Cannot submit form in ASP.NET MVC 3 48
    401 - Error 6 13
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now