I have a C# website that has a a user control where I initialize a series of private variables at the top of the user control:
using System.IO;
namespace FF.Core
{
public partial class _Job : System.Web.UI.UserControl
{
private static long _JobID = 0;
private static string _JobNumber = String.Empty;
I have dragged this user control on an .aspx page the mark up is as follows:
<%@ Register Src="_Job.ascx" TagName="_Job" TagPrefix="uc1" %>
<uc1:_JobDataEntry ID="_Job" runat="server" />
My application will launch multiple pages that contain this user control. It appears that after the first instance is launched the user control is not reinitialized ie the _JobID is not set back to 0 instead it contains the value of the previous loaded _JobID. I was under the impression because I'm launching a new page the _JobID would be set back to 0. I could initialize my variables in page load but I wanted to understand why this is happening and understand the best way to reuse a user control. Each of my new pages is being launched in a new browser window "target=_blank"
Thanks !
You have static variables, and those keep their state as long as you web app is running. Static variables are shared among all instances of a given class. That works for something like winforms, but not in web apps. Generally avoid static variables in web apps and web services, and also in class libraries that will be used by web apps/services.
Simply make those a private NOT static variable. And do a search for static variable in your app, and convert them to instance variables (unless you have a really good reason to keep them static.