Custom Property doesn't save values

E

Evgeni Presmann

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");
}
}
 
T

Teemu Keiski

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
 
E

Evgeni Presmann

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 said:
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

Evgeni Presmann said:
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");
}
}
 
T

Teemu Keiski

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


Evgeni Presmann said:
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 said:
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

Evgeni Presmann said:
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");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top