c# - Settings and Getting Session Variables stored in class -


i'm trying create class holds session information, set , @ will.

here call session["sessioninformation"] = new sessionvar.usersession();

and here class

public static class sessionvar {  public class usersession {       public string fullname     {         get;         set;     }     public string homeurl     {         get;         set;     }     public bool validuser     {         get;         set;     }      public int countid     {         get;         set;     }  } } 

lets in example have combobox called countid_combobox , on selectedindexchanged want set users specific session["sessioninformation"] countid value of combobox. how done?

protected void countid_combobox_selectedindexchanged(object sender, telerik.web.ui.radcomboboxselectedindexchangedeventargs e) {     //set session variable here } 

also, there way foreach loop strings in class. want see if of variables in class null.

so you've created typed usersession class handle session variables. that's nice, specially if have big project, lots of forms , (junior) developers.

you know important part: each new session, we'll have one, and one usersession object.

now, let's move on part use it. can retrieve object's reference using ((sessionvar.usersession)session["sessioninformation"]). let's dive it:

  1. session["something"] returns object
  2. you want usersession object
  3. we cast using ((sessionvar.usersession)session["sessioninformation"]) , done!

now, let's that, later on, decide move object session viewstate (i'm not saying idea, mind you). if have code scattered around project, it'll pain manage change.

so, idea have base page, pages inherit. like:

public class basepage : system.web.ui.page { ... } 

and place code retrieve usersession there, like:

public class basepage : system.web.ui.page {     public usersession currentusersession     {                 {             usersession usersession = null;              if (session["usersession"] == null)             {                 usersession = new usersession();                 session["usersession"] = usersession;             }             else                 usersession = (usersession)session["usersession"];              return usersession;         }         private set { }     } } 

bonus: note that, on code above, i'm suggesting way ensure 1 , 1 object current user.

finally, in order get list of string properties empty, can use following method (place inside usersession):

    public list<string> getemptystringattributes()     {         list<string> emptystringattributes = new list<string>();          type type = this.gettype();          foreach (system.reflection.propertyinfo property in type.getproperties())         {             if (property.propertytype == typeof(string))             {                 string value = property.getvalue(this) string;                  if (string.isnullorwhitespace(value))                     emptystringattributes.add(property.name);             }         }          return emptystringattributes;     } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -