Thurein
Thurein

Reputation: 2566

Adding Style property to UserControl

I am having difficulties in adding Style property to a UserControl. There is a parser exception when I try to view the consumer page.

private Style _headerStyle = new Style();
public Style HeaderStyle
{
    get { return _headerStyle ; }
    set
    {
        _headerStyle .CopyFrom(value);
    }
}

Usage:

Style="border: 1px solid blue;"

Error:

Cannot create an object of type 'System.Web.UI.WebControls.Style' from its string representation ...

Upvotes: 1

Views: 942

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460038

A Style instance is not a string and vice-versa. Style.CopyFrom expects a Style as argument and you're passing a String. That's the reason why it cannot be copied to the new style object.

If you want to give your UserControl a border programmatically:

myControl.HeaderStyle.BorderStyle = BorderStyle.Solid;
myControl.HeaderStyle.BorderWidth = new Unit(1);
myControl.HeaderStyle.BorderColor = System.Drawing.Color.Blue;

Upvotes: 1

Erre Efe
Erre Efe

Reputation: 15557

You'r trying to create an ASP.NET Style but declaring a CSS Style, that's the problem. If you want to apply an ASP.NET style you need to call the ApplyStyle method instead:

control.ApplyStyle(styleName);

Upvotes: 0

Related Questions