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

asp.net开发自定义控件

2012年03月09日 ⁄ 综合 ⁄ 共 2076字 ⁄ 字号 评论关闭

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

namespace Custom
{
 /// <summary>
 /// WebCustomControl1 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:Rendered runat=server></{0}:Rendered>")]
 public class Rendered : Control, IPostBackDataHandler, IPostBackEventHandler
 {
  
  private String text1;
  private String text2;
  private String text = "点击提交按钮看是否匹配.";
  private int number = 100;
     

  private int Sum
  {
   get
   {        
    return Int32.Parse(text1) +
     Int32.Parse(text2);
   }  
  }
     
  public int Number
  {
   get
   {
    return number;
   }
   set
   {
    number = value;
   }
  }
     
     
  public String Text
  {
   get
   {
    return text;
   }
   set
   {  
    text = value;
   }        
  }
     
  public event CheckEventHandler Check;
     
  protected virtual void OnCheck(CheckEventArgs ce)
  {
   if (Check != null)
   {
    Check(this,ce);
   }
  }
     
  public virtual bool LoadPostData(string postDataKey,
   NameValueCollection values)
  {
   text1 = values[UniqueID + "t1"];
   text2 = values[UniqueID+ "t2"];
   Page.RegisterRequiresRaiseEvent(this);
   return false;
  }
     
  public virtual void RaisePostDataChangedEvent()
  {
  }
     
  public void RaisePostBackEvent(string eventArgument)
  {
        
   OnCheck(new CheckEventArgs(Sum - Number));
  }
     
     
  protected override void Render(HtmlTextWriter output)
  {
   output.Write ("<h3>第一个数字 : ");
   output.Write("<INPUT type= text name = " + this.UniqueID + "t1" +
    " value = '0' >");
   output.Write("</h3><br>");  
   output.Write ("<br><h3>第二个数字: ");
   output.Write("<INPUT type= text name = " + this.UniqueID + "t2" +
    " value =  '0' >");
   output.Write("</h3><br>");  
   output.Write("<br><br><input type= submit name = " +
    this.UniqueID + " value= '提交'>");
   output.Write("<br><br><span style='height:50px;width:500px;'>"
    + Text + "</span>");
  }
 } 
 public class CheckEventArgs : EventArgs
 {
  private bool match = false;
           
  public CheckEventArgs (int difference)
  {
   if (difference == 0)
   {
    match = true;
   }
  }
  public bool Match
  {
   get
   {
    return match;
   }
  }
 }
     
 public delegate void CheckEventHandler(object sender, CheckEventArgs ce);

}

抱歉!评论已关闭.