ASP.Net Custom Control - Override "Text" Property - asp.net

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!

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; }
}
}

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!

Related

Is it possible to extend the default HyperLink in ASP.NET?

I was to change some behaviour when the text attribute is set against a hyperlink. I know I can create my own version of like
public class MyHyperLink: System.Web.UI.Hyperlink
{
}
however, is it possible to override HyperLink one so I don't have to use a custom HyperLink class on the front end controls?

Setting a design-time property from the properties window of a custom web server control hangs/crashes Visual Studio 2010

As my title says, I have a set property crash problem.
Here's the scenario:
I have created a simple custom ASP.Net server control that generates some text.
I wanted to give design-time property for that text so its style can be accessed by developers from the properties window.
All the properties in the properties window are working except the ones with the type System.Web.UI.WebControls.Style that I have created.
Here is my property:
[Bindable(true)]
[Category("Appearance")]
[Description("The style for the header")]
[Localizable(true)]
public Style HeaderTextStyle
{
get
{
Style s = (Style)(ViewState["HeaderTextStyle"] == null ? Styles.defaultHeaderStyle : ViewState["HeaderTextStyle"]);
return s;
}
set
{
ViewState["HeaderTextStyle"] = value;
}
}
Oh and Styles.defaultHeaderStyle is just a property from an internal class that returns a new Style.
Let me point that the hanging/crashing occurs only when I CHANGE the property, so it cannot be from the getter.
I won't paste my render control because the error occurs even when I'm not rendering anything.
What is it that causes this?
Thank you.
I found the answer to my problem.
You see, the Style class is a property that has sub-properties and it is called a complex property. Complex properties ( a property that has subproperties) need custom state management to use view state. The Style class need design-time attributes to enable persistence within the control's tags. So what I wrote in my original post will not work.
For complete explanation visit: Server Control Properties Example from MSDN
I managed to implement it using that example. I hope this will be useful to others out there.

Replace existing textbox in project with new one at runtime in asp.net

HI All,
I need your suggestions/idea on this.When we started the project,client said that we want
to accept Potentially Dangerous HTML Tags(like img,script,link etc...) into textbox,and we save textbox value into database.
Now,Client want to allow these tags ,but want to save these values in encoded form(to avoid xss)
Now, I just wanted to know that is there any way in asp.net ,so that I can replace behavior of all the existing textboxes with new SafeTextbox with minimal changes(I don't want to add HtmlEncode or HtmlDecode to each textbox.)
For example
public class NewTextBox:TextBox
{
public override string Text
{
get { return HttpUtility.HtmlDecode(base.Text); }
set { base.Text = HttpUtility.HtmlEncode(value); }
}
}
At runtime
ProjectTextBox replaces with NewTextBox
You could look into TagMapping in ASP.NET.
http://msdn.microsoft.com/en-us/library/ms164641(v=vs.100).aspx
The tagMapping element defines a collection of tag types that are remapped to other tag types at compile time. This remapping causes the mapped type to be used in place of the original tag type for all pages and controls in the ASP.NET application within the scope of the configuration file.
<pages>
<tagMapping>
<add tagType="System.Web.UI.WebControls.TextBox"
mappedTagType="Yournamespace.NewTextBox"
/>
</tagMapping>
You can set ValidateRequests to true at page level or for all pages from web.config.
Not sure if this would help, but have you considered control adapters?
http://www.singingeels.com/Articles/How_To_Control_Adapters.aspx

Cannot access custom properties on nested user control

Ok, please understand the architecture here first.
OurMasterPage.aspx has user control Header.ascx
Header.ascx has a user control in it called LandingPageTopNav
LandingPageTopNav.ascx has a public property named "LandingPage" that is there to be able to set by the user using this control.
And I have a Third.aspx page in which I need to set the LandingPageTopNav property to a LandingPage object
The problem is that I can't get this to work in my ThirdPage.aspx.cs:
Master.LandingPageTopNav.LandingPage = this.landingPage;
Master.LandingPageTopNav.Visible = true;
And that is, I can't get the first line to work where I'm trying to reference the LandingPage property. The second line is fine. My Third.aspx definitely can reference my master page objects otherwise from code-behind.
I'd venture to guess that the LandingPageTopNav property of OurMasterPage doesn't return a value typed as LandingPageTopNav. It probably returns the correct control typed as something more generic (e.g. Control); which is why setting the Visible property works but not the LandingPage property.

Flag control properties as required in design view

I'd like to force the consumer of a control to give a property a value when placing the control on a page.
In VisualStudio when you create an < img > tag without attributes SRC or ALT on a user control, it gets underlined saying that SRC and ALT are required attributes. I assume this is just a special handling of the tag by the editor, but is there a way to define a similar behavior for controls?
If the control had a property defined like this:
public object AProperty
{
get
{
if (ViewState["AProperty"] == null)
{
throw new Exception("AProperty is a required property of this control");
}
return ViewState["AProperty"];
}
set { ViewState["AProperty"] = value; }
}
Is there a way to use a Custom Attribute or something else that would flag in the designer?
You could use the Microsoft.Build.Framework.Required attribute. This would require a value to be set at build time or the build will fail with a message which indicates that the property does not have a value.
I don't believe there is an attribute to indicate that a specific tag must be included in a server control (or at least I don't see any such attribute on the System.Web.UI.HtmlControl.Image class). I believe that the litle underlines are part of the HTML validation of the IDE.
You could always create a custom attribute which throws a warning if a property is missing
While Microsoft.Build.Framework.Required is probably the best answer here, for others who stumble upon this and can't use .NET 4.0, you can also use this method:
http://forums.asp.net/t/1238319.aspx

Resources