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?
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?
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.
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Opps after that line you can do
tempimg.SkinID="CheckedOn"
tempimg.SkinID="CheckedOn"
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