Session or viewstate, default value

How can i set the default value of my variable stored in session or viewstate before the first call

My code is:

public class xxxxxxxxxxxxx: System.Web.UI.UserControl{

 

public xxxxxxxxxxxxxxxxx()

{

}

 

public xxxxxxxxxxxxxx(string user, Connection connection...)

{

}

...

public string User{

               get

                         {

                                 return (string)Session["User"];

                         }

             set

                       {

                                    Session["User"] = value;

                        }

              }

...

 

private void Page_Load(object sender, System.EventArgs e)

...

protected override void OnLoad(EventArgs e)

...

 }

Explain better: from my app i want create this object without using the constructor with parameters, but the noparameters one. In this case i want a default user.

How can i do it Where must i write the assignment of default value

Thx




Answer this question

Session or viewstate, default value

  • kensai

    What do you want to do, setting a default value for a class member

    Then you should do this:


    public class xxxxxxxxxxxxx: System.Web.UI.UserControl{
    {
     private string _myString;
     private int _myInt;

     public xxxxxxxxxxxxxxxxx()
     {
      // Put your defaul initialization values here.
      _myString = "Default string";
      _myInt = 0;
     }
     
     public xxxxxxxxxxxxxx(string user, Connection connection...)
     
     {
     
     }
     
     public string User
     {
      get
      {
       return (string)Session["User"];
      }
      set
      {
       Session["User"] = value;
      }
     }
    }

     


  • PainDeer

    self-explaining mah...

    if utente="pippo" for default where have i to put

    thx



  • Moldau04

    In fact i put them in OnLoad method but i have some problem with null Session the first time..how can i solve

     

    THX!!!



  • Mark@New Hudson Technologies,LLC

    But i have not constructor in web user control... in fact when i move my control in a form, i have not the new statement...

    I have modify to have



  • your evil twin

    Some self-explaining code:


    public class xxxxxxxxxxxxx: System.Web.UI.UserControl{
    {
     public xxxxxxxxxxxxxxxxx()
     {
      // Put your defaul initialization values here.
     }
     
     public xxxxxxxxxxxxxx(string user, Connection connection...)
     
     {
     
     }
     
     public string User
     {
      get
      {
       return (string)Session["User"];
      }
      set
      {
       Session["User"] = value;
      }
     }
    }

     


  • George Hardy

    For the user in Session!

  • Jonathon Rossi

    What are the problems then Can you be more specific

  • samiha

    Then you can put it in your Load event, but remember to check first if the Session is null!


  • asaad


    public class xxxxxxxxxxxxx: System.Web.UI.UserControl{
    {
     public xxxxxxxxxxxxxxxxx()
     {
      if(Session["User"] == null)
      {
        Session["User"] = "MyDefaultValue";
      }
     }
     
     public xxxxxxxxxxxxxx(string user, Connection connection...)
     
     {
     
     }
     
     public string User
     {
      get
      {
       return (string)Session["User"];
      }
      set
      {
       Session["User"] = value;
      }
     }
    }

     


  • adlammons

    In web user control:

    public GridLines VisibleGrid

    {

    get

    {

    return (GridLines) ViewState["VisibleGrid"];

    }

    set

    {

    ViewState["VisibleGrid"] = value;

    DataGrid1.GridLines = value;

    }

    }

    protected override void OnLoad(EventArgs e)

    {

    base.OnLoad (e);

    if (Page.IsPostBack)

    {

    return;

    }

    InizializzaComponente();

    }

    private void InizializzaComponente()

    {

    VisibleGrid = GridLines.None;

    }

    ______________________________________

     

    In my form (web application)

    if i set

    myWebUserControl.VisibleGrid = GridLines.Vertical;

    it doesn't work!!!



  • JA

    Ok, i solved in this way (from your code)

    private bool _Flag = true;

    public bool Flag

    { get{ _Flag = (bool) ViewState["Flag"];

    return _Flag;

    }

    set{

    _Flag = value;

    ViewState ["Flag"] = _Flag;

    }

    }

     

    in init or onload method:

    ViewState["Flag"] = _Flag;

     

     



  • Atanas

    i put in Page_Load and it works...or better now i can't modify from my app...help!

  • Session or viewstate, default value