Can a web user control (.ascx) use a CSS file for styling?

Here the source of the empty .acsx file that I created is called LogOnBox.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LogOnBox.ascx.cs" Inherits="ECommerce.Views.Shared.LogOnBox" %> 

I want to drag some labels, text fields, etc. and give them style using a CSS file. How to link CSS with acsx file?

+2
c # css asp.net-mvc-2 ascx
source share
3 answers

CSS is linked to the page, not to the control. But you can use CSS in the control when the page links to it.

+3
source share

Just specify the css file in the header of the page containing the user control.

+1
source share

In my cases, I would like to insert the css and js file into the dll for this, I follow the steps below and it works.

step 1 Place the following code on top of your page behind

  [assembly:WebResource("YourProjectName.Assets.Plugins.Dropzone.dist.dropzone.js", "application/x-javascript")] [assembly:WebResource("YourProjectName.Assets.Plugins.Dropzone.dist.dropzone.css","text/css")] namespace Sanay.Suip.UserControls { public partial class FileUpload : UserControlBase { 

Step 2

And in the inInitial case below.

 `protected override void OnInit(EventArgs e) { base.OnInit(e); string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectName.Assets.Plugins.Dropzone.dist.dropzone.css") + "\" type=\"text/css\" rel=\"stylesheet\" />"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "cssFile", css, false); ClientScriptManager csm = Page.ClientScript; csm.RegisterClientScriptResource(GetType(), "YourProjectName.Assets.Plugins.Dropzone.dist.dropzone.js"); } 

`

0
source share

All Articles