Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global or application variables based on conditional statement

Status
Not open for further replies.

zerkat

Programmer
Jul 12, 2007
103
US
Hi,

I am redesigning a site in .net; I come from a Coldfusion background and when designing/writing a web site in coldfusion I could declare a global variable based on conditions in the coldfusion application file (which is coldfusion's equivalent to web.config). For instance, based on the url I would set file paths to graphics etc.

I would like to do that with this site but I read that I can not use conditional statements in the web.config. Is there a way to declare variables based on conditions so that the variables could be accessible to the whole app? If it is possible how would a person go about setting that up?

I am using VS 2005 with vb.net.
Thanks.
 
Im not sure about VB, but you can use the App_Code folder to create your classes, and within those classes you can use structs. There might be other ways to get this done using namespaces, but i dont use Visual Studio.


Code:
public struct ComplaintData
{
    public int ID;
    public int TypeID;
    public string EntryForm;
    public bool IsPrivate;

    public ComplaintData(int id, int typeID, string Form, bool isPrivate)
    {
        this.ID = id;
        this.TypeID = typeID;
        this.EntryForm = Form;
        this.IsPrivate = isPrivate;
    }
}
public struct SiteData
{
    public string FavIcon;
    public string HeadLogo;
    public string SiteTitle;

    public SiteData(string Icon, string Logo, string Title)
    {
        this.FavIcon = Icon;
        this.HeadLogo = Logo;
        this.SiteTitle = Title;
    }
}
public class rtbData : ControlLoading
{
    string conString = ConfigurationSettings.AppSettings["conString"];
    ComplaintData complaint;
    SiteData settings;

    /// <summary>
    /// Sets the Complaint Type By Virtual Directory
    /// "/ppl" = 0, "/cmcs" = 1
    /// </summary>
    public rtbData()
    {
        string Site = HttpContext.Current.Request.Url.AbsolutePath;
        complaint.TypeID = (Site.StartsWith("/ppl")) ? 0 : 1;
        complaint.EntryForm = (Site.StartsWith("/ppl")) ? "entryppl.aspx?id=" : "entry.aspx?crmid=";
        settings.FavIcon = (Site.StartsWith("/ppl")) ? "faviconPPL.ico" : "favicon.ico";
        settings.HeadLogo = string.Format("img/head{0}.gif", complaint.TypeID);
        settings.SiteTitle = (Site.StartsWith("/ppl")) ? "Internal Problem Log" : "Customer Complaints";
    }
...

...
    #region Site Settings
    //expose the structs to the class
    public string SiteFavIcon
    {
        get { return settings.FavIcon; }
    }

    public string SiteLogo
    {
        get { return settings.HeadLogo; }
    }

    public string SiteTitle
    {
        get { return settings.SiteTitle; }
    }

    #endregion
}


then in my default.aspx.cs code behind, i consume them like this...
Code:
public partial class _Default : Page
{
    alerts alert = new alerts();
    rtbData crm = new rtbData();

    public void Page_Init()
    {
        HtmlHead pageHead = (HtmlHead)Page.Header;
        pageHead.Title = crm.SiteTitle;
        HtmlLink favIco = new HtmlLink();
        favIco.Attributes.Add("rel", "shortcut icon");
        favIco.Attributes.Add("href", crm.SiteFavIcon);
        favIco.Attributes.Add("type", "image/x-icon");
        pageHead.Controls.Add(favIco);
        lbAddNewComplaint.NavigateUrl = crm.ComplaintEntryForm + "0";
    }
 
hmm...ok. I'll check out what I can do with the App_Code folder....thanks for the pointer.
 
Anything placed in the app_code folder is accessable throughout the application in C# or VB or whatever language you use. That is not a language specific functionality. If you place a class in the app_code folder, just make sure your functions, etc are Public or Shared.
 
So I looked into using the app_code folder and read that you can basically put whatever you want to in there. When I opened up VS 2003 and went to add a new item to my app_code my options are XML Schema, Report, XML File, SQL Database, Text File and Class Diagram. Not sure which one would be the best fit since all I want to do is set a variable based on the URL of the site. It just takes a simple Sub procedure to do this. Would I still want to use a class for this?

This is the code that I wrote for checking the URL:

Protected Sub Page_Load(ByVal sender AS Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Path As String
Dim serverURL = Request.Url.ToString
If serverURL = " Then
Path = "../subdirectory/someFile.aspx"
Else
Path = "../someOtherDirectory/someOtherFile.aspx"
End If
End Sub


Thanks for all your help. Everyone in this forum has been very helpful....
 
The app_code folder does not exist in VS2003, it is new to 2005. You will have to create a public class at the root.
 
oops - sorry - just double checked - I am using vs 2005. I was really tired when I wrote that response...before I finished my first cup of coffee...
 
i also see that what i thought was an app_code folder is actually app_data. That explains the options I get when I try to add an existing item. VS didn't create an app_code folder when I created the site - it created app_data. I created the site as an ajax enabled web site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top