User control in a user control

Hello all,

I have created a user control with some checkboxes, and public properties to get the values of these checkboxes. I have also created a second user control using the first user control; this second control also access the public properties of the first one.

My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem.

Thank you in advance for your help.


It is when you give from yourself that you truly give.

IT Images
0
RobHood
8/9/2007 4:12:48 PM
📁 asp.net.getting-started
📃 91979 articles.
⭐ 4 followers.

💬 5 Replies
👁️‍🗨️ 1465 Views

RobHood:

My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem.

 

By "wrong values" do you mean that the values of the inner User Control's properties revert back to their declaratively set or default values, rather than the values that have been programmatically set by the outer User Control?

Can you show us the code you use for the properties of the inner User Control? If my understanding of what you mean by "wrong values" is correct, then it may be that you are not backing the property values to viewstate, which will cause any programmatic property changes to be lost on postback.


Happy Programming!


-- Scott Mitchell
-- [email protected]
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
0
Scott
8/9/2007 4:23:35 PM

Thank you for your quick reply. 

here is part of the code in the inner control:

 

 

private Boolean _bEncryptConnectionString;

public Boolean bEncryptConnectionString
{
   get { return _bEncryptConnectionString; }
   set { _bEncryptConnectionString = value; }
}

protected void cbEncryptConnectionStrings_CheckedChanged(object sender, EventArgs e)
{
   UncheckAll(sender);
   bEncryptConnectionString = cbEncryptConnectionStrings.Checked;
}

 

You are right about reverting back to the default value. but I have already enabled viewstate to both controls inner and outer at design time, or is there another property that I should set?

 Thanks again,


It is when you give from yourself that you truly give.

IT Images
0
RobHood
8/9/2007 4:40:28 PM

You have to manually save and retrieve the property value from the ViewState collection in your User Control property in order to have the value persisted across postback. Ditto for properties in custom, compiled controls. For common, scalar properties like bools, strings, ints, and so on, you can just stuff that value into the ViewState collection. For complex types (i.e., properties whose value is an instance of a custom class with a slew of properties) you need to employ view state serialization within that custom class itself.

The good news is that you have a simple example here. Just update the property to the following:

  

1    public Boolean bEncryptConnectionString
2    {
3       get { 
4          object b = ViewState["EncryptConnectionString"];
5          if (b == null)
6             return false; // return whatever you want the default value to be if this property isn't set explicity
7          else
8             return (bool) ViewState["EncryptConnectionString"];
9       }
10      set { ViewState["EncryptConnectionString"] = value; }
11   }

 

Note that there's no need for the private member variable  _bEncryptConnectionString anymore.


Happy Programming!


-- Scott Mitchell
-- [email protected]
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
0
Scott
8/9/2007 5:29:07 PM

Not only you have given me a clue but you also the solution :)

I wish you good luck in your life

Thank you


It is when you give from yourself that you truly give.

IT Images
0
RobHood
8/9/2007 5:55:54 PM

Great!

Let me leave you with an article I would invite you to read for a more thorough understanding of view state in ASP.NET: Understanding ASP.NET View State. It was written for 1.x, but the majority of the concepts discussed are exactly the same in 2.0.

Happy Programming! 


Happy Programming!


-- Scott Mitchell
-- [email protected]
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
0
Scott
8/9/2007 6:08:58 PM
Reply: