Link to home
Start Free TrialLog in
Avatar of fizzer666
fizzer666

asked on

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

Hi,

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?
Avatar of Sammy
Sammy
Flag of Canada image

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

Sammy

Avatar of fizzer666
fizzer666

ASKER

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Opps after that line you can do
tempimg.SkinID="CheckedOn"