2

How can render dynamic css from custom control based on user settings?I read this article but it is not I want.Style class hasnt got all css properties and direct write style isnt good approach.

2
  • You can set any CSS property you want as a control property and render it in the control based on what the user wants? Can you be more specific on what you want to style? Commented Nov 5, 2012 at 23:39
  • yes,I want to render user settings. Commented Nov 5, 2012 at 23:42

2 Answers 2

3

If your styles change on a per user basis, and you want full control over what is rendered, writing the styles yourself is a perfectly good approach.

This is how you can do it:

    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
        var styleStr = "{color:red}";
        writer.AddAttribute(HtmlTextWriterAttribute.Name, "style");
        writer.AddAttribute(HtmlTextWriterAttribute.Value, styleStr);
        base.AddAttributesToRender(writer);
    }
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to apply style not to one element you can do this:

 protected override void OnPreRender(EventArgs e)
    {
        ...
        Page.Header.Controls.Add(new LiteralControl("<style type='text/css'> .x { border-style:solid }</style>"));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.