给予财产自定义控制,但它不起作用

时间:2015-10-07 05:01:01

标签: c# asp.net

我创建了自定义控件,其中标签和星号表示必需。我想更改label.so的颜色。我给ForeColor属性控制但不适用。

<asp:LabelwithRequired ID="MessageLabelwithRequired" 
runat="server" Text="Message" Required="True" Forecolor="Red"></asp:LabelwithRequired>

我在控制中仅暴露了属性的属性,但其他属性未应用。

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

只需检查一下,您是否从Asp.net Label控件中检索了自定义控件。如果没有尝试从它继承,它应该工作。

答案 1 :(得分:1)

尝试创建一个服务器控件。然后你需要将它添加到引用中,并将工具箱添加为组件。然后你可以使用拖放来使用它。它还在属性窗口中显示属性调用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);
        }
    }
}

Asp.net网站表单代码

<%@ 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>