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

Webmatrix, passing variable through pages

Hello experts!

I have a table in the database. User cannot change values there, they are kind of hard coded.
I want this table to populate <select> list, I know how to do it on the page where <select> list is placed.

The thing is I would like to populate variable:

var myQuery = db.Query("SELECT * FROM tyoes");

and make this variable visible for many pages to use it to populate many <select> lists on many pages.

How can I do it?

Also: do I have to initialize database on every page I use it?

var db = Database.Open("StarterSite");

Thank you

panJames

0
panJames
Asked:
panJames
  • 2
1 Solution
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you can use session variables to save data that multiple pages will need access to.

www.w3schools.com/asp/asp_sessions.asp

if you a dropdown box thats not going to change throughout the pages, i recommend building it once, and then storing the html for it in a session variable, kind of like this:

session("dropdownName") = "<select name='dropdown'>" & _
                                               "     <option value='1'>option 1</option>" & _
                                               "     <option value='2'>option 3</option>" & _
                                               "     <option value='3'>option 3</option>" & _
                                               "</select>"

then you can write out the dropdown on any page by simply doing:

<%=Session("dropdownName")%>

I would look into session timeouts as well if you take this approach, because if your session expires, you'll lose the data for the dropdown.

to answer your second question, technically no, you dont need to, you could store the database connection object in a session variable, but this is not common practice as it would waste resources.

The answer above assumes you're using classic asp
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
if you're using .NET, you can use sessions as well:

http://msdn.microsoft.com/en-us/library/ms178581.aspx
0

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.

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