Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Usercontrol and Variables

Im really new to ASP.NET and have a background in ASP VB coding.
I have been reporgramming a webpage from ASP to ASP.net 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 :)

  • 3
  • 2
1 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.

fryslanAuthor Commented:

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??
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.

fryslanAuthor Commented:
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.
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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now