Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2296
  • Last Modified:

ASP.Net - How to Set the SkinID of a Control in code behind?


I have an image on my web page with a skinID.  Based on a condition I want to alter the SkinID at runtime.  I used this:

 Me.imgCheckOn.SkinID = "CheckedOn"         where imgCheckOn is an ASP Image.

I get the following error:
The 'SkinId' property can only be set in or before the Page_PreInit event for static controls. For dynamic controls, set the property before adding it to the Controls collection.

The problem is, if I put the code in the page_PreInit event imgCheckOn does not exist at this point.  Any ideas?
  • 3
  • 2
1 Solution
when do you add this control to the control collection?
what condition you want to check before changing the SkinID?

more details or code used will be helpful in identifying a solution for the issue


fizzer666Author Commented:
The control is on my aspx page at design time so I don't programmatically add it to the control collection.  

I have 3 skinID's for image controls.  Each one points to a different image url.  I have a function which goes to the DB to get a value.  Based on this value I pick my Skin ID.  This part is not the problem, this all works fine.  I have actually removed this part for testing and I am just trying to change to another skinID value.

All I was originally doing was trying to set the SkinID of the image control in the Load event.  However, I got the error that The SkinID can only be set in or before the Page_PreInit event.  So, I moved "Me.imgCheckOn.SkinID = "CheckedOn" " to the Pre_init event.  The problem here is that the control doesn't seem to exist.  So how can I ever set the SkinID programmatically?

So, the only code that I have is as follows:

Front end:
<asp:Image ID="imgCheckOn" runat="server" SkinID="checkOnOff" />

Code behind:
   Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
           Me.imgCheckOn.SkinID = "CheckedOn"
    End Sub

Hope that makes more sense now.
fizzer666Author Commented:
Sorry for the double post.  I've just tested this in a new website and it works.  It doesn't work because in my site I am using a master page.  So how can I get it working with a Master page?
just replace ("Content") with your content page name
Dim tempimg As Image = TryCast(Me.Master.FindControl("Content").FindControl("imgCheckOn"), Image)
Opps after that line you can do

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now