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

Posted on 2007-10-10
Last Modified: 2013-11-07

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?
Question by:fizzer666
    LVL 27

    Expert Comment

    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



    Author Comment

    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.

    Author Comment

    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?
    LVL 27

    Accepted Solution

    just replace ("Content") with your content page name
    Dim tempimg As Image = TryCast(Me.Master.FindControl("Content").FindControl("imgCheckOn"), Image)
    LVL 27

    Expert Comment

    Opps after that line you can do

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    This video teaches users how to migrate an existing Wordpress website to a new domain.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now