如何使用Web用户控件在aspx页面的头部显示CSS [英] How to Show CSS in head part of aspx page using Web User Control

查看:50
本文介绍了如何使用Web用户控件在aspx页面的头部显示CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用4.0版开发asp.net网站。为此,我使用Web用户控件和简单的aspx页面,我使用CSS,Javascript在web用户控件使用后,当我添加.aspx页面检查源代码显示在正文部分而不是头部分。所以请告诉我如何使用在头部的Web用户控件显示中使用的CSS。

Hello,

I am developing website in asp.net using 4.0 version. For this i am using Web user control and simple aspx page and i am using CSS,Javascript in web User Control after using this when i add in .aspx page after checking source code shows in body part instead of Head Part. So Please tell me how can i use CSS which is using in web user control show in Head Part.

推荐答案

有几种方法可以做到这一点,但它不是很必要。



我有可能在页面上出现多次的控件(例如自定义文本框)。如果我每次包含控件时都包含脚本,那么就会出错。我改为使用ScriptManager控件为我管理这些。

There are a couple of ways to do this, but it isn't very necessary.

I have controls that may appear more than once on the page (custom textboxes for eg). If I include the script every time I include the control then things go wrong. I instead use the ScriptManager control to manage these for me.
public partial class PassengerManagement : System.Web.UI.UserControl
{
  private const string PassengerManagementJsName = "CustomPassengerManagementJS";
  private const string PassengerManagementJsUrl = "/Resources/js/custom.passengerMgmt.js";

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);

    Type cstype = Page.GetType();

    ClientScriptManager cs = Page.ClientScript;

    if (!cs.IsClientScriptIncludeRegistered(cstype, PassengerManagementJsName))
      cs.RegisterClientScriptInclude(cstype, PassengerManagementJsName, PassengerManagementJsUrl);
  }
}





这确保脚本只包含一次。我可以用同样的方式对 IsClientScriptBlockRegistered 做同样的事情。



脚本管理器将是第一个控件表单,这就是javascript包含和块出现的地方。



至于css,你应该以这样的方式设计它,使每个类,控制,命名控件样式是唯一的,因此包括主页面标题中的所有css。要么是这个,要么在主页面头部包含第二个ContentPlaceholder,以便子页面可以将通用css添加到页面头部。真的不需要这样做来控制javascript。



我希望有所帮助^ _ ^

Andy



This ensures that the script is included once and only once. I can do the same with IsClientScriptBlockRegistered in the same way.

The script manager will be the first control within the form, and that is where the javascript includes and blocks will appear.

As for the css, You should design it in such a way that makes every class, control, named control style are unique so include all css in the Master page header. Either this or include a second ContentPlaceholder in the Master page head so that child pages can add generic css to the page head. Doing this for controls javascript really isn't needed.

I hope that helps ^_^
Andy


这篇关于如何使用Web用户控件在aspx页面的头部显示CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆