Domanda

I'm having problem with adding style attributes to the control in my custom web control. Below is a very simple custom web control just to illustrate the problem:

[ParseChildren(true)]
[ToolboxData("<{0}:SomeControl runat=\"server\"></{0}:SomeControl>")]
public class SomeControl : WebControl
{
    public CheckBox MyCheckbox { get; set; }

    protected override void CreateChildControls()
    {
        MyCheckbox = new CheckBox { Text = "Here is some text" };

        MyCheckbox.Style.Add("some", "style");
        Controls.Add(MyCheckbox);

        base.CreateChildControls();
    }
}

When used on a page I get the following output:

<span><span style="some:style;"><input id="ctl03" type="checkbox" name="ctl03" /><label    for="ctl03">Here is some text</label></span></span>

Why is the style attribute on the span tag and not on the input tag?

È stato utile?

Soluzione

Here are two ways to add styles to a checkbox, the way that you are using will try to render for the browser UserAgent detected and thus wraps the checkbox in a span which has the style attribute.

The other way will add the style attribute directly to the checkbox.

MyCheckbox.InputAttributes.Add("some", "style");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top