User Controls, Master Pages, CSS and putting it all together
-
I have a master page (MasterPage.master) and it's called by Default.aspx. Then I added a web user control (WebUserControl.ascx), I tested it all and everything was working great. I decided to add some color to the user control by way of CSS (StyleSheet.css), now I have problems. I don't want to add a link to the MasterPage head since not all pages using the master need the css. How do I programmatically have my WebUserControl.ascx.vb add a link tag to my page head? I think I explained this well, but if not just post your questions and I'll try to answer them. Thanks for any help offered.
-
I have a master page (MasterPage.master) and it's called by Default.aspx. Then I added a web user control (WebUserControl.ascx), I tested it all and everything was working great. I decided to add some color to the user control by way of CSS (StyleSheet.css), now I have problems. I don't want to add a link to the MasterPage head since not all pages using the master need the css. How do I programmatically have my WebUserControl.ascx.vb add a link tag to my page head? I think I explained this well, but if not just post your questions and I'll try to answer them. Thanks for any help offered.
Hi, There are two ways available to resolve your issue. 1. Simple and easy way. Put all your CSS styles in a style sheet and save into your project. string should be: Add the following code to your control's page load event: Page.ClientScript.RegisterClientScriptBlock(GetType(), , , false); 2. Some more work but efficient. Create a function in for your master page like, public void AddCssLink(string cssfile) { HtmlGenericControl styleCtrl = new HtmlGenericControl("link"); styleCtrl.Attributes.Add("href", "Styles/" + cssfile); styleCtrl.Attributes.Add("rel", "stylesheet"); styleCtrl.Attributes.Add("type", "text/css"); Page.Header.Controls.add(styleCtrl); } from your control's page load event, Master.AddCssLink(""); Happy programming. With Best Regards, P.Anbuselvan IT Consultant Emirates Group IT, UAE
-
I have a master page (MasterPage.master) and it's called by Default.aspx. Then I added a web user control (WebUserControl.ascx), I tested it all and everything was working great. I decided to add some color to the user control by way of CSS (StyleSheet.css), now I have problems. I don't want to add a link to the MasterPage head since not all pages using the master need the css. How do I programmatically have my WebUserControl.ascx.vb add a link tag to my page head? I think I explained this well, but if not just post your questions and I'll try to answer them. Thanks for any help offered.
I think there are many ways to do what you want. You can programmatically access the Header[^] of the current page using the Page.Header[^] property and simply add the stylesheet. You can also do as someone said by injecting text or control (should use the
HtmlLink
Class instead of the generic one).