Luis
Luis

Reputation: 6001

Apply skin to a control created programmatically

I am creating a Textbox in the codebehind of a page like this:

protected override void OnInit(EventArgs e)
{  
      base.OnInit(e);
      TextBox test = new TextBox();
      test.SkinkId = "MySkin";
      placeHolder.Controls.Add(test);
} 

and in my skin file I have this:

<asp:TextBox
    runat="server"
    SkinId = "MySkin"
    Width="400"
/>

Why is the skin not being applied to the control. If I declare the control in my aspx page it works ok, but if I try to do it programmatically it does not work.

Upvotes: 5

Views: 3408

Answers (3)

Dbuggy
Dbuggy

Reputation: 911

I know this is an old thread. But i want to share what i had to do to in a similar situation. What helped me is to use ApplyStyleSheetSkin(this) after i created the control.

protected override void OnInit(EventArgs e)
{  
    base.OnInit(e);

    TextBox test = new TextBox();
    test.SkinkId = "MySkin";
    test.ApplyStyleSheetSkin(this); // <--
    placeHolder.Controls.Add(test);
}

Upvotes: 6

Luis
Luis

Reputation: 6001

Answer is here: https://connect.microsoft.com/VisualStudio/feedback/details/558947/apply-skin-to-a-control-created-programatically

"The ASP.NET theming system has restrictions on when different theme information can be set. The Theme property can be set during PreInit. However StyleSheetTheme is processed at compilation time and thus cannot overriden programmatically in an event like PreInit."

Upvotes: 0

Daniel Dyson
Daniel Dyson

Reputation: 13230

Does it help if you place your code in the OnPreInit event? You might need to add it to the placeholder later, but you could create the control there.

Upvotes: 1

Related Questions