嘗試創建一個服務器控件。然後您需要將它添加到引用中,並將其作爲組件添加到工具箱中。然後您可以使用拖放操作來使用它。此外,還會在屬性窗口中顯示屬性調用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 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>