【问题标题】:Giving property to custom control but its not working将属性赋予自定义控件但它不起作用
【发布时间】:2015-10-08 19:16:19
【问题描述】:

我创建了自定义控件,该控件具有标签和星号指示必需。我想更改标签的颜色。所以我将 ForeColor 属性提供给控制但它不适用。

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

我在控件中公开的属性仅用于应用属性但未应用其他属性。

谁能帮我解决这个问题。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    检查一下,您是否从 Asp.net Label 控件中检索了您的自定义控件。如果不尝试从它继承,那么它应该可以工作。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-23
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        • 2014-12-23
        • 2016-10-13
        相关资源
        最近更新 更多