all groups > asp.net building controls > july 2003 >
You're in the

asp.net building controls

group:

Custom Property doesn't save values


Custom Property doesn't save values Evgeni Presmann
7/25/2003 12:19:18 PM
asp.net building controls: Hello all,
I've defined a custom control with the property of type string[].
If I assigned values to the property at design time, I can see these values
in the control as expected. However if I compiled the application, all the
values disappear.

Help please

Regards,
Evgeni

public class MediaElement : WebControl
{
private string[] parameters = null;

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string[] Parameters
{
get { return this.parameters; }
set { this.parameters = value; }
}
protected override void Render(HtmlTextWriter output)
{
if (this.parameters != null)
foreach (string parameter in this.parameters)
output.Write(parameter + " ");
else
output.Write( "empty list");
}
}

Re: Custom Property doesn't save values Teemu Keiski
7/26/2003 1:25:33 PM
When values are set at design-time, they are specified declaratively in the
aspx and therefore set for control's properties on every request. If you
want the property to be able to persist/preserve values so that setting is
done programmatically in the code and value cab ge get sometime later (even
on some other postback), you could put the properties to use ViewState. That
is change the property something like this:

public string[] Parameters
{
get {
object o=ViewState["parameters"];
return (o==null)? new string[]{} : (string[])o;
}
set
{
ViewState["parameters"] = value;
}
}

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com

[quoted text, click to view]

Re: Custom Property doesn't save values Evgeni Presmann
7/28/2003 8:14:11 AM
Hello Teemu,

Thank you for this suggestion, but unfortunately it doesn't work. At
design-time after the compilation and at run-time the parameters still
disappear. I suppose, that I've don't declare some attributes for the class
or property to right serialize them, but I've no idea which attibutes must
be needed

Regards,

Evgeni

"Teemu Keiski" <joteke@aspalliance.com> schrieb im Newsbeitrag
news:ebPP9A2UDHA.2164@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Re: Custom Property doesn't save values Teemu Keiski
7/28/2003 2:40:50 PM
Oh yes, try removing the DesignerSerializationVisibility attribute, it
prevents serializing them to the aspx source. However, I have a hunch that
using string array would cause problems because the designer does not know
how to persist them. I might be wrong with that one though. If this doesn't
help, try using some other type for the property to solve the problem.

You could for example create custom collection with custom type to serve the
purpose. If you are interested in that approach, you could check my article:

IList collections
http://www.aspalliance.com/joteke/ilistarticle/article.aspx

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com


[quoted text, click to view]

AddThis Social Bookmark Button