现在的位置: 首页 > 综合 > 正文

ASP.NET 的自定义控件>显示继承控件图形的问题

2013年06月23日 ⁄ 综合 ⁄ 共 1191字 ⁄ 字号 评论关闭
 前些天自己学习自定义控件的时候,继承了一个系统自带的按钮控件,可是当用到的时候在html页面却不显示该控件的按钮样式,下面是.NET自动生成的代码:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Webwodeceshi
{
 /// <summary>
 /// WebCustomControl1 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
 public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
 {
  private string text;
 
  [Bindable(true),
   Category("Appearance"),
   DefaultValue("")]
  public string Text
  {
   get
   {
    return text;
   }

   set
   {
    text = value;
   }
  }

  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   output.Write(Text);//****AAA***//
  }
 }
}
假如我要继承一个按钮控件,并且在IE中能够显示出来,要必改的有三个地方

一。把本控件默认继承的类改成>>System.Web.UI.WebControls.类名

二。可以把text属性删除,因为一般不需要重写

三。也是最主要的一点,自定义控件如果要显示继承控件的图形,需要重写父类的

protected override void Render(HtmlTextWriter output)
  {
   output.Write(Text);//****AAA***//
  }

这个方法的作用是输出控件样式到浏览器,如果是默认的话,只会显示一个Text文本值,这样是不行的,要把这个方法改成

  protected override void Render(HtmlTextWriter output)
  {
   base.Render(output);
  }

base.Render这个方法就是调用父类的方法,这样你继承的button控件或其它的控件才能显示出他本来的面目!

 

抱歉!评论已关闭.