应用CSS在ASP自定义用户控件的单个实例:NET [英] Apply CSS to single instance of Custom user Control in ASP:NET

查看:125
本文介绍了应用CSS在ASP自定义用户控件的单个实例:NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我创建了一个看起来像这样简单的用户控件

Ok I created simple user control that look like this

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PlaceHolderCMS.ascx.cs" Inherits="Controls_PlaceHolderCMS" %>

<div id="contentPlaceholder" runat="server" class="contentPlaceholderStyle">
</div>

我使用它像这样

<div class="mainField" runat="server">             
  <cms:PlaceHolder ID="PlaceHolderCMS1" runat="server" class="PH1" />
  <cms:PlaceHolder ID="PlaceHolderCMS2" runat="server" />
</div>

但我的问题是CSS,我可以编辑的asociated与整个控件风格,像这样

But my problem is CSS, I can edit styles that is asociated with entire control, like this

.contentPlaceholderStyle
{
    border-style :dashed;
    background-color:transparent;
    border-width:1px; 
    padding: 10px, 10px, 0px, 0px;  
    margin:15px;
    height:100px;
}

但是,当我尝试风格的用户控制的没有发生的单个实例。我想这从样式,像这样

But when i try to style single instance of user control nothing happens. I tried this from stylesheet, like this

.PH1
{    
    height:100px;
}

和从C#,像这样
保护无效的Page_Load(对象发件人,EventArgs五)

and from C#, like this protected void Page_Load(object sender, EventArgs e)

{

PlaceHolderCMS1.Attributes["style"] = "width=" + width + "%; height:" + height + "px;" ;

}

所以我的问题是我怎么可以把我的自定义控件的实例样式属性?我想对整个控制通用风格,并在必要时覆盖它们特定的实例。

So my question is how can I put style attributes for instances of my Custom Control? I want to have general styles for entire control, and override them for particular instances if needed.

感谢您提前....

推荐答案

您的问题是,用户控件不呈现为自己的任何标记。因为它没什么可应用在你的类。所以这是abolutely你为你的控制提供了一些自定义的CssClass属性和分配这部分容器保存你的所有控制的孩子。

Your problem is that UserControl doesn't render any tag for himself. Since that it's nothing to apply your class on. So it's abolutely on you to provide some custom CssClass property for your control and assign this to some container that holds all your control children.

控件的标记:

<div class='<%= CssClass %>' >
    <div id="contentPlaceholder" runat="server" class="contentPlaceholderStyle">
    </div>
</div>

控制的code-背后:

Control's code-behind:

[CssClassProperty]
public string CssClass
{
    get { return (string)(ViewState["CssClass"] ?? ""); }
    set { ViewState["CssClass"] = value; }
}

这篇关于应用CSS在ASP自定义用户控件的单个实例:NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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