C# 将属性赋予自定义控件但其不起作用
我已经创建了自定义控件,它需要标签和星号指示。我想更改标签的颜色。所以我将ForeColor属性赋予控件,但它不应用C# 将属性赋予自定义控件但其不起作用,c#,asp.net,C#,Asp.net,我已经创建了自定义控件,它需要标签和星号指示。我想更改标签的颜色。所以我将ForeColor属性赋予控件,但它不应用 <asp:LabelwithRequired ID="MessageLabelwithRequired" runat="server" Text="Message" Required="True" Forecolor="Red"></asp:LabelwithRequired> 我在控件中公开的属性仅适用于正在应用的属性,而其他属性不适用 <
<asp:LabelwithRequired ID="MessageLabelwithRequired"
runat="server" Text="Message" Required="True" Forecolor="Red"></asp:LabelwithRequired>
我在控件中公开的属性仅适用于正在应用的属性,而其他属性不适用
<asp:LabelwithRequired ID="MessageLabelwithRequired"
runat="server" Text="Message" Required="True" Forecolor="Red"></asp:LabelwithRequired>
谁能帮我解决这个问题。请检查,您是否已从Asp.net标签控件检索到自定义控件。如果没有,请尝试从它继承,然后它应该可以工作。尝试创建一个服务器控件。然后您需要将它添加到引用中,并将其作为组件添加到工具箱中。然后您可以使用拖放来使用它。此外,它还会在属性窗口中显示属性调用
TextColor
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("CustomeLable", "CsLable")]
namespace ServerControl2
{
[DefaultProperty("Lable")]
[DisplayName("Custome Lable")]
[ToolboxData("<{0}:CustomeLable runat=server></{0}:CustomeLable>")]
public class CustomeLable : CompositeControl
{
Panel p;
Label lbl;
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
return lbl.Text.Replace('*',' ').Trim();
}
set
{
lbl.Text = value + " *"; // you asked above
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Black")]
[Localizable(true)]
public Color TextColor
{
get
{
return lbl.ForeColor;
}
set
{
lbl.ForeColor = value;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
p = new Panel();
lbl = new Label();
lbl.Text = "Custome Lable *";
p.Controls.Add(lbl);
base.CreateChildControls();
}
protected override void RecreateChildControls()
{
EnsureChildControls();
}
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
lbl.RenderControl(writer);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
[组件:标记前缀(“可定制”、“可标记”)]
命名空间服务器控制2
{
[默认属性(“标签”)]
[显示名称(“自定义标签”)]
[ToolboxData(“”)
公共类可自定义:CompositeControl
{
p小组;
标签lbl;
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串文本
{
得到
{
返回lbl.Text.Replace('*','.Trim();
}
设置
{
lbl.Text=value+“*”;//您在上面问过
}
}
[可装订(真实)]
[类别(“外观”)]
[默认值(“黑色”)]
[可本地化(正确)]
公共颜色文本颜色
{
得到
{
返回lbl.ForeColor;
}
设置
{
lbl.ForeColor=值;
}
}
受保护的覆盖无效CreateChildControls()
{
控件。清除();
p=新面板();
lbl=新标签();
lbl.Text=“定制标签*”;
p、 控制。添加(lbl);
base.CreateChildControls();
}
受保护的重写void RecreateChildControls()
{
ensureChildControl();
}
受保护的覆盖无效渲染(HtmlTextWriter编写器)
{
AddAttributesToRender(编写器);
lbl.渲染控制(编写器);
}
}
}
Asp.net Web表单代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ATButtonBarControl.Default" %>
<%@ Register assembly="ServerControl2" namespace="ServerControl2" tagprefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:CustomeLable ID="CustomeLable1" runat="server" />
<br />
</div>
</form>
</body>
</html>