.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Page level global variables --- *always* bad style?

Posted By:      Posted Date: October 03, 2010    Points: 0   Category :ASP.Net

Creating an interface that we'll use internally to view and modify contents of a database.  The page uses the Wizard Control and a dropdown list to determine which clients information to display in the Wizard.

Current, I have a page level variable like this:

public static String selectedclient;

In this way, I can easily preserve which client is selected from the dropdown list between postbacks.  As it is, the program works fine.  The functionality is fulfilled.  I just keep reading that such public static page level variables are a bad idea, and that it has something to do with multithreading.  However, I've yet to see a problem or have any issues.

I really want to do this program "right" (good style), as this finished product will be used for years.  Is it going to come back to haunt me in the future if I leave it like it is?  If I need to change it, what would be a good way to preserve the same functionality?

One thing I'm thinking is that, any place where I need the selectedclient information, I could just use ClientDDList.SelectedValue.ToString().  But before I go and replace every selectedclient with ClientDDL.SelectedValue.ToString(), I'd like some advice on this issue.


View Complete Post

More Related Resource Links

Page level variables and style

Making my first ASP.NET/C# program which utilizes three ASP.NET Calendar controls and allows the user to choose the year/month from dropdown lists and then adjusts the three calendars accordingly.  i.e. if the user chooses August, Calendar1 will be June, Calendar2 will be July, and Calendar3 will  be August, and then pulling dates of events from an SQL database and changing the colors of days on the calendars of the days.I got it working like I want it --- the program fulfills its function.  However, the code is absolutely horrible and hard to follow.  I want to learn some good style and clean it up, but I'm not even sure where to begin.  For starters, I had to pretty much declare every variable throughout the program at page level and static, otherwise the values were getting reset to 0 and blank upon postback.  So the top of my code looks like: public partial class _Default : System.Web.UI.Page { public static String selectedclient; public static int selectedyear; public static int selectedmonthminus2; public static int selectedmonthminus1; public static int selectedmonth; public static String selectedyearstring; public static String selectedmonthminus2string; public static String selectedmonthminus1string; public static String selectedm

ASP.NET 4.0 Menu Top Level selected item style


I want to apply a css class to the level 1 menu item (parent) when it or any or it's children are selected.

I've looked at MSDN documentation and any number of examples and walkthroughs but I haven't come up with a way of managing this. I was thinking something like this might work but the style is not being applied to the parent. To further complicate matters, if I try to apply a css style to StaticSelectedStyle it doesn't pick it up at all.

Menu Markup:

<StaticSelectedStyle  Font-Bold="True" ForeColor="#3333CC" />

Code Behind:

        protected void MenuItemClick_NavMenu(Object sender, MenuEventArgs e)


            MenuItem parentItem = e.Item.Parent;

            if (parentItem != null)


                parentItem.Selected = true;



Wiki page style sheet in SP 2007


Is there any way I can add CSS information with the HTML behind a Wiki page?

I wan't to make one pagethat serves as a templatesfor future posts, and I like to control the layout of this post and subsequent posts through a stylesheet.


external content not load in page like style and js in asp.net mvc when i use area


i am working on a asp.net mvc 2 website who have a area and i have a problem with them.

i use different different url routing for application settings. i have two folder called style

first folder location in main directory  /Style/style.css

second folder location in Area area/style/style.css

everything is work fine in application but when i used style.css from main directory in my area's views then style not worked. when i track them from firebug then i got error "The resource could not be found" but path is correct.

sombody tell me to use absolute path to solve this issue. how i can use absolute path to declare resource url in views.

any suggestion , sollution on this issue who can help me to solve this issue.

Web Part working fine on the top-level page but 'Not Safe' error on lower-level pages.


I have created a Web Part which reads and parses the SharePoint logs in order to display usage information of the page it resides on.  The Web Part works perfectly on the top page, reading and displaying the information desired.  However, once the Web Part is added to any page lower in the site heirarchy (sub-page) the same Web Part throws the following error:

Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe.

Show Error Details
Hide Error Details

[UnsafeControlException: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe.]
  at Microsoft.SharePoint.ApplicationRuntime.SafeControls.GetTypeFromGuid(Guid guid)
  at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)

Of course the current version of the Web Part is in the SafeControls list on the web.config.

The Web Part was originally designed to return site activity only for the top default.aspx page and worked fine but started to fail in the manner described above once I added the following line of code to return a SPCo

Global Variables and data types

Hi, I have a small issue with the datatype problems. This is what my scenario is. I have a global variable "Qtr" declared as "Int32" datatype. Next, i have a ActiveXscript with VBScript Language. With the below piece of code. All it does, it will calculate and assign the calculated value to the global variable "Qtr". But due to some datatype conversion problem it is not displaying the Calculated value. It is showing null. But the value in the local variable is getting displayed. Can anyone figure out what the problem is all about. I have the option of taking the String datatype for the global variable but in the next step in the workflow , we have an stored procedure call i.e. an execute sql task where we will be call a stored procedure EXEC sp ? where the parameter datatype is an INT in the database. That is why i am finding problem.  I have used the work around i.e. using SSIS advanced expressions and avoided the parameter mapping and all. Function Main() 'msgbox("test") Dim Qtr Dim currentYear 'Constants qYears = 3 Qtr= (currentYear - qYears) * 10 + 4 msgbox(Qtr) msgbox("Assigning the value to the variable") DTSGlobalVariables("Qstart").Value =  Cint(Qtr) msgbox("Display Value in the variable") MsgBox(DTSGlobalVariables("Qtr").Value)      End Function Can a

Global variables between different JS files

I have two different Javascript files, A.js and B.js. I would like to make a variable available in A.js to B.js. A.js is invoked first and the coding in it just listens for events. B.js is always running and doing something. How do I share a variable defined in an A.js function for B.js to use? I have tried finding the answer in google but its taking forever to find a pertinent scenario.

Using Global.asax to Set Session Variables Pulled From SQLServer

I have a custom class that pulls user information from a SQL Server table. Now that I'm expanding the site from a single page to others. I want to shift the code to the Session_Start event so that the information is always loaded when the site is accessed. How do I call my custom code from global.asax since its script and not a class. 

Unable to build connection string using Package Level variables from the AcquireConnection fonction

Hi all! In SSIS 2008, within a script component trying to refer to a package level variables that was added to the ReadOnlyVariables collection of the component, the following fails within the AcquireConnnection function and I get this error: Error 1 Validation error. Test1: Test1: Microsoft.SqlServer.Dts.Pipeline.ReadOnlyVariablesNotAvailableException: The collection of variables locked for read access is not available at this point.     à Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.HandleUserException(Exception e)     à Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.AcquireConnections(Object transaction)     à Microsoft.SqlServer.Dts.Pipeline.ManagedComponentHost.HostAcquireConnections(IDTSManagedComponentWrapper100 wrapper, Object transaction)   string connectionString = "DSN=" + this.Variables.MYDSN; Conn = new OdbcConnection(connectionString); Conn.Open();     If I move the previous code in the PreExecute function, it runs fine though.   Then, if I move the connectionString variable to the class definition, and populate it from within the PreExecute function and leave the last 2 lines in the AcquireConnection function, I get the following error: Error 1 Validation error. Test1: Test1: System.InvalidOperationException: The ConnectionString property

Application level event without global.asax

I have a need to add an application level event (like Application_Start) but not in global.asax.  The reason that I can't use global.asax is because this is a web application for which I don't have source code but I want to add some functionality to it.  I need to insert an application start code and was wondering it there was anyway to do it.  I'm not sure that if I add a class or aspx page to the sie if it will dynamically compile since this is a web application.  I know this is a crazy request but let me know what you think.Also, this web site was written in .NET 2.0.Thanks.

New Page dialog takes master page style

I am designing a master page for my companies intranet. I started out with the simple master page that is included. I built my html and created a custom style sheet. Everything appears to work just fine. Except when I click Site Actions > New Page and I get the New Page dialog box, the box takes the style of my master page. It includes the banner and the navigation bar and squishes everything together. The textboxes are still there and they work just fine, but how do I get that box to not take the master page layout?

accessing package level variables in a Script Component in 2008 version


how to access the package level variables within a script component in SSIS 2008

RSS - Is there a way to display all new content across several subsites on a top level home page ?


I have an Intranet site that contains several departmental intranet sites.

Is it possible to have a web part on the homepage of the top level intranet that displays all recently added content to the various sub sites ?



Error: There are no page layout files available in the master page library of the top-level site.

I exported one of the subsite using stsadm and imported into a new site collection, I could access the new site collection but whenever I try to create a new page in my new site collection I get the following error.

There are no page layout files available in the master page library of the top-level site. Add one or more page layouts to this library before you create a page or set page properties.

Please help. Thanks.

Page Level Security


How do I limit what User/Groups can see a page in SharePoint 2007? Within on particular SITE, I want to be able to say, for example, only Users in the X group can see this page.

I see on the Page Setting the "Audience Targeting" option, but that does not restrict anyone outside that group from clicking "All Site Content" then finding and opening the page from under Pages.

Can someone point me in the right direction?

Thanks in advance.

Todd C - MSCTS SQL Server 2005 - Please mark posts as answered where appropriate.

Style Sheet for Child Page in C#.net


Dear All

I am using Master Page for my applications and i am using external Style Sheet (ie style.css).

Child pages not inherit the master page style sheet.

Please let me know how can i call the style sheet for my child pages.

Thanks and Regards


How to get the Package level Variable collections (Global Variable) in Script Task


Hi All,

I have the requirement like read all the Variables in Script Component Task, which includes variables available at package level and their values, global variables declared at package.


Currently are two ways to read the variables but which are not suitable for my requirement


1) Mentioning the variables at Script designer window as ReadOnlyVariables or ReadWriteVariables



ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend