Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. User Control Losing State?

User Control Losing State?

Scheduled Pinned Locked Moved ASP.NET
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave Sexton
    wrote on last edited by
    #1

    I've created a web user control with a few properties that I want to assign to a different object during postback but it seems like the user control gets re-instantiated or something and passes back as null. What am I doing wrong? I've tried with a simpler user control and the same thing happens. Here's the code I'm using. Hope it makes it clearer. WebUserControl.ascx WebUserControl.ascx.cs private string _Text; public string Text // property to assign later { get { return _Text; } set { _Text = value; } } public event EventHandler ValueChanged; protected void OnValueChanged(EventArgs e) { if (ValueChanged != null) { ValueChanged(this, e); } } protected void Button1_Click(object sender, System.EventArgs e) { Text = TextBox1.Text; // assign property value OnValueChanged(e); // raise event } Then, after dropping the user control onto the web form... Default.aspx

    So far so good... Default.aspx.cs string _MyText; // want to assign to this protected void WebUserControl1_ValueChanged(object sender, EventArgs e) { btnAssign.Text = WebUserControl1.Text; // This works } protected void btnAssign_Click(object sender, EventArgs e) { _MyText = WebUserControl1.Text; //This says WebUserControl1.Text = null } I'm sure it's something really stupid that I'm forgetting to do or maybe the way I've gone about it but it's got me stuck & I'd really appreciate someone telling me where I've missed it. Thanks.

    D I 2 Replies Last reply
    0
    • D Dave Sexton

      I've created a web user control with a few properties that I want to assign to a different object during postback but it seems like the user control gets re-instantiated or something and passes back as null. What am I doing wrong? I've tried with a simpler user control and the same thing happens. Here's the code I'm using. Hope it makes it clearer. WebUserControl.ascx WebUserControl.ascx.cs private string _Text; public string Text // property to assign later { get { return _Text; } set { _Text = value; } } public event EventHandler ValueChanged; protected void OnValueChanged(EventArgs e) { if (ValueChanged != null) { ValueChanged(this, e); } } protected void Button1_Click(object sender, System.EventArgs e) { Text = TextBox1.Text; // assign property value OnValueChanged(e); // raise event } Then, after dropping the user control onto the web form... Default.aspx

      So far so good... Default.aspx.cs string _MyText; // want to assign to this protected void WebUserControl1_ValueChanged(object sender, EventArgs e) { btnAssign.Text = WebUserControl1.Text; // This works } protected void btnAssign_Click(object sender, EventArgs e) { _MyText = WebUserControl1.Text; //This says WebUserControl1.Text = null } I'm sure it's something really stupid that I'm forgetting to do or maybe the way I've gone about it but it's got me stuck & I'd really appreciate someone telling me where I've missed it. Thanks.

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      I figured it out - I'm getting it to work using viewstate. Is this good practice or is there a better way?

      1 Reply Last reply
      0
      • D Dave Sexton

        I've created a web user control with a few properties that I want to assign to a different object during postback but it seems like the user control gets re-instantiated or something and passes back as null. What am I doing wrong? I've tried with a simpler user control and the same thing happens. Here's the code I'm using. Hope it makes it clearer. WebUserControl.ascx WebUserControl.ascx.cs private string _Text; public string Text // property to assign later { get { return _Text; } set { _Text = value; } } public event EventHandler ValueChanged; protected void OnValueChanged(EventArgs e) { if (ValueChanged != null) { ValueChanged(this, e); } } protected void Button1_Click(object sender, System.EventArgs e) { Text = TextBox1.Text; // assign property value OnValueChanged(e); // raise event } Then, after dropping the user control onto the web form... Default.aspx

        So far so good... Default.aspx.cs string _MyText; // want to assign to this protected void WebUserControl1_ValueChanged(object sender, EventArgs e) { btnAssign.Text = WebUserControl1.Text; // This works } protected void btnAssign_Click(object sender, EventArgs e) { _MyText = WebUserControl1.Text; //This says WebUserControl1.Text = null } I'm sure it's something really stupid that I'm forgetting to do or maybe the way I've gone about it but it's got me stuck & I'd really appreciate someone telling me where I've missed it. Thanks.

        I Offline
        I Offline
        Imran Khan Pathan
        wrote on last edited by
        #3

        assign property value on pageload event of WebUserControl.ascx.cs protected void Page_Load(object sender, EventArgs e) { Text = TextBox1.Text; // assign property value } best regard Pathan

        ---------------------------------------------------

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups