|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Embedding CSS with inline codeIs it possible to embed a css file and use it as webresource when it's having
some code inside? An example would be: ..Container { background-color : <% =BackgroundColor %>; } when "BackgroundColor" is a property in my custom server control? Thank you. Hi Sean,
No, it is not possible because the Style tag is not a server control that can runat=â€serverâ€. The Head can runat=â€server’ though. Technically you can try something like this in the page markup: <HEAD id="PageHeadSection" runat="server"> </HEAD> Then in the codebehind: Protected PageHeadSection As HtmlGenericControl Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim StyleTag As New HtmlGenericControl styletag.InnerHtml = "<style>.Container{Background-color:" + strBackgroundColor + "}</style>" PageHeadSection.Controls.Add(styletag) End Sub Though this is technically possible, I prefer (in terms of separating the styling from the code) to add the style names dynamically to the server controls but keep the style definitions static. For example you can create all the possible styles for the container: ..ContainerRed{/* definitinon*/} ..ContainerBlue{/* definitinon*/} Then in the codebehind assign the appropriate container style to the objects cssClass property. Show quoteHide quote "Sean" wrote: > Is it possible to embed a css file and use it as webresource when it's having > some code inside? An example would be: > > .Container > { > background-color : <% =BackgroundColor %>; > } > > when "BackgroundColor" is a property in my custom server control? > > Thank you. Hi Phillip,
One of the things that I saw in ASP.NET 2.0 is that you can run certain code in CSS files for web resource (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/custwebcon.asp) and that was achieved with inline coding ( <% %> ). I'm not sure if it's only web resources that can be "code-injected", or not. Show quoteHide quote "Phillip Williams" wrote: > Hi Sean, > > No, it is not possible because the Style tag is not a server control that > can runat=â€serverâ€. The Head can runat=â€server’ though. Technically you can > try something like this in the page markup: > > <HEAD id="PageHeadSection" runat="server"> > </HEAD> > > Then in the codebehind: > > Protected PageHeadSection As HtmlGenericControl > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > Dim StyleTag As New HtmlGenericControl > styletag.InnerHtml = "<style>.Container{Background-color:" + > strBackgroundColor + "}</style>" > PageHeadSection.Controls.Add(styletag) > > End Sub > > Though this is technically possible, I prefer (in terms of separating the > styling from the code) to add the style names dynamically to the server > controls but keep the style definitions static. For example you can create > all the possible styles for the container: > > .ContainerRed{/* definitinon*/} > .ContainerBlue{/* definitinon*/} > > Then in the codebehind assign the appropriate container style to the objects > cssClass property. > > > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Sean" wrote: > > > Is it possible to embed a css file and use it as webresource when it's having > > some code inside? An example would be: > > > > .Container > > { > > background-color : <% =BackgroundColor %>; > > } > > > > when "BackgroundColor" is a property in my custom server control? > > > > Thank you. Hi Sean,
Thanks for posting this link for such an interesting article. I will spend some time reading it. The information I gave you earlier applies to ASP.NET1.1. The Style tag in ASP.NET2.0 however can indeed runat="server" and therefore I managed to do something like this: <style runat="server" id="StyleTag"></style> and in the codebehind, I tried this and it worked: StyleTag.InnerText += ".Container{background-color:red;"}; But using your original expression in ASP.NET 2.0 brought up interesting issues. I got first an error pointing to the partial class that is generated by ASP.NET for the webpage about a missing semicolon. When I added this the code ran but I got an empty value (instead of the value "Red"). When I stepped through the code it seems that the @__RenderStyleTag (which assigns the codebehind value) runs after the Page_PreRenderComplete and the value did not appear on the browser. This is an interesting issue. I will keep looking into it and perhaps some one from Microsoft would comment also. Show quoteHide quote "Sean" wrote: > Hi Phillip, > > One of the things that I saw in ASP.NET 2.0 is that you can run certain code > in CSS files for web resource > (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/custwebcon.asp) and that was achieved with inline coding ( <% %> ). > I'm not sure if it's only web resources that can be "code-injected", or not. > > "Phillip Williams" wrote: > > > Hi Sean, > > > > No, it is not possible because the Style tag is not a server control that > > can runat=â€serverâ€. The Head can runat=â€server’ though. Technically you can > > try something like this in the page markup: > > > > <HEAD id="PageHeadSection" runat="server"> > > </HEAD> > > > > Then in the codebehind: > > > > Protected PageHeadSection As HtmlGenericControl > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles MyBase.Load > > Dim StyleTag As New HtmlGenericControl > > styletag.InnerHtml = "<style>.Container{Background-color:" + > > strBackgroundColor + "}</style>" > > PageHeadSection.Controls.Add(styletag) > > > > End Sub > > > > Though this is technically possible, I prefer (in terms of separating the > > styling from the code) to add the style names dynamically to the server > > controls but keep the style definitions static. For example you can create > > all the possible styles for the container: > > > > .ContainerRed{/* definitinon*/} > > .ContainerBlue{/* definitinon*/} > > > > Then in the codebehind assign the appropriate container style to the objects > > cssClass property. > > > > > > > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Sean" wrote: > > > > > Is it possible to embed a css file and use it as webresource when it's having > > > some code inside? An example would be: > > > > > > .Container > > > { > > > background-color : <% =BackgroundColor %>; > > > } > > > > > > when "BackgroundColor" is a property in my custom server control? > > > > > > Thank you. Hi Phillip,
I was thinking maybe WebResource can run on embedded resources and inject code that is related to the entire assembly, but not to a specific instance of a class that resides in that assembly? This is only my speculation, and I'm not so sure about it. Would be nice to have someone from Microsoft to respond. And thank you for helping with the issue. Sean Show quoteHide quote "Phillip Williams" wrote: > Hi Sean, > > Thanks for posting this link for such an interesting article. I will spend > some time reading it. > > The information I gave you earlier applies to ASP.NET1.1. The Style tag in > ASP.NET2.0 however can indeed runat="server" and therefore I managed to do > something like this: > > <style runat="server" id="StyleTag"></style> > > and in the codebehind, I tried this and it worked: > > StyleTag.InnerText += ".Container{background-color:red;"}; > > But using your original expression in ASP.NET 2.0 brought up interesting > issues. I got first an error pointing to the partial class that is generated > by ASP.NET for the webpage about a missing semicolon. When I added this the > code ran but I got an empty value (instead of the value "Red"). When I > stepped through the code it seems that the @__RenderStyleTag (which assigns > the codebehind value) runs after the Page_PreRenderComplete and the value did > not appear on the browser. > This is an interesting issue. I will keep looking into it and perhaps some > one from Microsoft would comment also. > > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Sean" wrote: > > > Hi Phillip, > > > > One of the things that I saw in ASP.NET 2.0 is that you can run certain code > > in CSS files for web resource > > (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/custwebcon.asp) and that was achieved with inline coding ( <% %> ). > > I'm not sure if it's only web resources that can be "code-injected", or not. > > > > "Phillip Williams" wrote: > > > > > Hi Sean, > > > > > > No, it is not possible because the Style tag is not a server control that > > > can runat=â€serverâ€. The Head can runat=â€server’ though. Technically you can > > > try something like this in the page markup: > > > > > > <HEAD id="PageHeadSection" runat="server"> > > > </HEAD> > > > > > > Then in the codebehind: > > > > > > Protected PageHeadSection As HtmlGenericControl > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As > > > System.EventArgs) Handles MyBase.Load > > > Dim StyleTag As New HtmlGenericControl > > > styletag.InnerHtml = "<style>.Container{Background-color:" + > > > strBackgroundColor + "}</style>" > > > PageHeadSection.Controls.Add(styletag) > > > > > > End Sub > > > > > > Though this is technically possible, I prefer (in terms of separating the > > > styling from the code) to add the style names dynamically to the server > > > controls but keep the style definitions static. For example you can create > > > all the possible styles for the container: > > > > > > .ContainerRed{/* definitinon*/} > > > .ContainerBlue{/* definitinon*/} > > > > > > Then in the codebehind assign the appropriate container style to the objects > > > cssClass property. > > > > > > > > > > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Sean" wrote: > > > > > > > Is it possible to embed a css file and use it as webresource when it's having > > > > some code inside? An example would be: > > > > > > > > .Container > > > > { > > > > background-color : <% =BackgroundColor %>; > > > > } > > > > > > > > when "BackgroundColor" is a property in my custom server control? > > > > > > > > Thank you.
ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
"Protect" or Disable Editing of A Webcontrol ViewState properties and mapped properties don't work well togethe How do I create a custom webcontrol that can have xml inside it's tag? cache and detailsview, all data controls problem SmartNavigation Problem/Bug Custom TreeNode for asp.net 2.0 treeview control Menu Control - Templates What is the code for file upload? Multiple onclick Events |
|||||||||||||||||||||||