User Control Losing State?
-
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'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 figured it out - I'm getting it to work using viewstate. Is this good practice or is there a better way?
-
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.
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---------------------------------------------------