关闭

asp.net(c#)自定义控件

265人阅读 评论(0) 收藏 举报

实现自定义文本框:
-------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace ClassLibrary1
{
 [DefaultProperty("s"),
 ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
 public class WebCustomControl1 : System.Web.UI.WebControls.TextBox
 {

  [Bindable(true),
  Category("Appearance"),
  DefaultValue("")]
  public string s
  {
   get
   {
    return s;
   }

   set
   {
    s = value;
   }
  }
/// <summary>
/// 重写TextBox的Text属性
/// </summary>
  public override string Text
  {
   get
   {
    return (string)ViewState["Text"]==null?"":(string)ViewState["Text"];
   }
   set
   {
    ViewState["Text"] = value;
   }
  }

/// <summary>
/// 实现接口IPostBackDataHandler的类
/// </summary>
/// <param name="postDataKey"></param>
/// <param name="postCollection"></param>
/// <returns></returns>
  public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
  {
   String presentValue = Text;
   String postedValue = postCollection[postDataKey];

   if (presentValue == null || !presentValue.Equals(postedValue))
   {
    Text = postedValue;
    return true;
   }

   return false;
  }
  /// <summary>
  /// 重写Render函数,实现数据的输出
  /// </summary>
  /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {
   output.Write("<INPUT type= text id=/"t1/" name = "+this.UniqueID+ " value = " + this.Text + " onblur=/"return checkbox()/">");
   string span="<span id=/"t2/"><font color=#000000 size=2>请输入五个以上的字符</font></span>";
   //定义一个span元素
   string jscript="/n<script>/nfunction checkbox(){/n/tvar t=document.getElementById('t1');"+
    "/n/tvar s=document.getElementById('t2');/n/tif(t.value.length<5){"+
    "/n/t/ts.innerHTML='<font color=#FF0000 size=2>长度不符合要求</font>';}"+
    "/n/telse{/n/t/ts.innerHTML='<font color=Teal size=2>输入正确</font>';/n}/n</script>";
   //实现对文本框的数据判断的js代码
   string s="/n"+span+"/n"+jscript;
   output.Write(s);//数据输出
        }
    }

--------------------------------------------------------------------------------------------------------------------------------------------

实现自定义按钮控件
-------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Web.UI;

namespace CustomControls

 public class MyButton: Control, IPostBackEventHandler
 {    
  // Defines the Click event.
  public event EventHandler Click;
     
  // Invokes delegates registered with the Click event.
  protected virtual void OnClick(EventArgs e)
  {    
   if (Click != null)
   {
    Click(this, e);
   }
  }
     
  // Method of IPostBackEventHandler that raises change events.
  public void RaisePostBackEvent(string eventArgument)
  {    
   OnClick(EventArgs.Empty);
  }
     
  protected override void Render(HtmlTextWriter output)
  {    
   output.Write("<INPUT TYPE=submit name=" + this.UniqueID +
    " Value='Click Me' />");
  }
 }   
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:520次
    • 积分:13
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    阅读排行
    评论排行