0

This would seem to be very simple, but it's not working for me. I desparately need to know how to override the "Text" property of a server control I created, which inherits from "Label". When the control is dropped into an ASP Web form, I want the text property to already be set to a certain value. I tried:

[Browsable(true), Bindable(true), Category("Behavior"), Localizable(true)]
[DefaultValue("00:00:00")]
public override string Text{get; set;}

But it doesnt work; the "Text" property shows up blank - and when I try to edit it, I can change it to anything except the value specified in the "DefaultValue" attrib. This property is supposed to be overridable.

I also need to be able to set the "ID" property so it's set to a specific value when dropped on the form. Is this possible?

Any suggestions would be greatly appreciated!

Charles
  • 50,943
  • 13
  • 104
  • 142
galaxis
  • 925
  • 8
  • 10
  • This is similar to or duplicate of: [http://stackoverflow.com/questions/3657131/default-value-in-asp-net-server-control][1] [1]: http://stackoverflow.com/questions/3657131/default-value-in-asp-net-server-control –  Jan 26 '12 at 21:58

2 Answers2

3

For this you can use the ToolBoxDataAttribute:

Specifies the default tag generated for a custom control when it is dragged from a toolbox in a tool such as Microsoft Visual Studio.

By default, the visual designer of a tool such as Visual Studio, creates an empty tag. This is a tag representing a control in its default state, when the control is dropped from the toolbox of a visual designer onto the design surface. To specify initial default values, a control can make use of this attribute. You can use this attribute to customize the initial HTML content that is placed in the designer when the control is dragged from the toolbox onto the form.

It would look something like this for your control:

[ToolBoxData("<{0}:TimeLabel ID='TL1' Text='00:00:00' runat='server' />")]
public class TimeLabel : System.Web.UI.WebControls.Label
{
  [DefaultValue("00:00:00")]
  public override string Text
  {
    get { return ViewState["Text"] != null ? (string)ViewState["Text"] : "00:00:00"; }
    set { ViewState["Text"] = value; }
  }
}
Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
  • Hey @michielvoo (sry for associating your name to the wrong comment!). Thx for the suggestion, it worked! I reviewed the documentation and now (I think!) have a full understanding of all things "HTML generation" fromn a custom control. Thx for steering me in right direction – galaxis Jan 27 '12 at 01:12
  • Oh just a footnote; I also removed the "DefaultValue" attribure as it seemed extraneous. – galaxis Jan 27 '12 at 01:15
  • @user1172173 The `[DefaultValue]` attribute is helpful for editors because it's used by Visual Studio in the properties window. Any value other than "00:00:00" will be highlighted in **bold** by Visual Studio. – Michiel van Oosterhout Jan 27 '12 at 07:46
  • Understood, thx @michielvoo. Yeah after reviewing the doc on this attrib, kinda came to the same conclusion. As a quick follow-up on my other question, was disheartened to learn that the attrib doesn't seem to work for the "ID" property; it still wants to create its own by using the control class name plus a sequence number – galaxis Jan 27 '12 at 13:38
  • Sorry to clarify, I meant the class-level attribute "ToolBoxData" didn't seem to save the "ID" I supplied (created its own default)... – galaxis Jan 27 '12 at 13:42
  • @user1172173 Ah yes, I can understand why it would do that. Visual Studio wants to prevent duplicate IDs most likely. PS. You can delete your comment on the other answer, no? – Michiel van Oosterhout Jan 27 '12 at 13:43
0

The DefaultValue attribute is intended to display a default value in Properties Tab. So when you launch website without specify a value for your Text property .net uses this one. IMHO you have not chance to do that. Sorry!

ArtCava
  • 21
  • 7