复合控件的开发
发布日期:2022-02-05 18:27:40 浏览次数:7 分类:技术文章

本文共 11349 字,大约阅读时间需要 37 分钟。


复合控件
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
namespace Custom
{
 [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);
}
  public void Sum_Checked (Object sender,CustomControls.CheckEventArgs e)
  {
   if (e.Match == true)
    Composite1.Text = "<h2> 您输入的匹配!</h2>";
   else
    Composite1.Text = "对不起,您输入的数字的和不等于内置数字!";
  }  
用户控件和复合控件
MyUserControl.ascx
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CompositeControl
{
 public abstract class MyUserControl : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.TextBox box1;
  protected System.Web.UI.WebControls.TextBox box2;
  protected System.Web.UI.WebControls.Button button;
  protected System.Web.UI.WebControls.Label label;
  private int Sum
  {
   get
   {
    EnsureChildControls();
    return Int32.Parse(box1.Text) +
     Int32.Parse(box2.Text);
   }                
  }      
  public int Number = 100;   
  public string Text
  {
   get
   {
    EnsureChildControls();
    return label.Text;
   }
   set
   {
    EnsureChildControls();
    label.Text = value;
   }
  }     
  public event CheckEventHandler Check;    
  public virtual void OnCheck(CheckEventArgs e)
  {
   if (Check != null)
   {
    Check(this,e);
   }
  }      
  public void Button_Clicked(Object sender, EventArgs e)
  {
   OnCheck(new CheckEventArgs(Sum - Number));
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }
 }
 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);


  public void Sum_Checked (Object sender,CheckEventArgs e)
  {
   if (e.Match == true)
    MyUserControl1.Text = "<h2> 您输入的匹配!</h2>";
   else
    MyUserControl1.Text = "对不起,您输入的数字的和不等于内置数字!";
  }

自定义控件使用
myComposite.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomControls
{
 /// <summary>
 /// WebCustomControl1 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:Composite runat=server></{0}:Composite>")]
 public class Composite : Control, INamingContainer
 //标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。这仅是一个标记接口。
 {
  private int number = 100;
  private Label label;
           
  public int Number
  {
   get
   {
    return number;
   }
   set
   {
    number = value;
   }
  }            
  private int Sum
  {
   get
   {
    EnsureChildControls();
    return Int32.Parse(((TextBox)Controls[1]).Text) +
     Int32.Parse(((TextBox)Controls[4]).Text);
   }                
  }
  public string Text
  {
   get
   {
    EnsureChildControls();
    return label.Text;
   }
   set
   {
    EnsureChildControls();
    label.Text = value;
   }
  }          
  public event CheckEventHandler Check;         
  protected virtual void OnCheck(CheckEventArgs ce)
  {
   if (Check != null)
   {
    Check(this,ce);
   }
  }            
  protected override void CreateChildControls()
  {                  
   Controls.Add(new LiteralControl("<h3>第一个数字 : "));                  
   TextBox box1 = new TextBox();
   box1.Text = "0";
   Controls.Add(box1);                  
   Controls.Add(new LiteralControl("</h3>"));                  
   Controls.Add(new LiteralControl("<h3>第二个数字 : "));
   TextBox box2 = new TextBox();
   box2.Text = "0";
   Controls.Add(box2);             
   Controls.Add(new LiteralControl("</h3>"));             
   Button button1 = new Button();
   button1.Text = "提交";
   Controls.Add(new LiteralControl("<br>"));
   Controls.Add(button1);
   button1.Click += new EventHandler(this.ButtonClicked);          
   Controls.Add(new LiteralControl("<br><br>"));
   label = new Label();
   label.Height = 50;
   label.Width = 500;
   label.Text = "点击提交按钮看是否匹配";
   Controls.Add(label);                 
  }            
  protected override void OnPreRender(EventArgs e)
  {
   ((TextBox)Controls[1]).Text = "0";
   ((TextBox)Controls[4]).Text = "0";
  }            
  private void ButtonClicked(Object sender, EventArgs e)
  {
   OnCheck(new CheckEventArgs(Sum - Number));
  }
 }
 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);
}

  public void Sum_Checked (Object sender,Custom.CheckEventArgs e)
  {
   if (e.Match == true)
    Rendered.Text = "<h2> 您输入的匹配!</h2>";
   else
    Rendered.Text = "对不起,您输入的数字的和不等于内置数字!";
  } 


复合控件的事件处理
EventBubbler.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CompositeEvent
{
 [DefaultProperty("Text"),
  ToolboxData("<{0}:EventBubbler runat=server></{0}:EventBubbler>")]
 public class EventBubbler : Control, INamingContainer
 {
  private int number = 100;
  private Label label;
  private TextBox box1;
  private TextBox box2;      
  public event EventHandler Click;
  public event EventHandler Reset;
  public event EventHandler Submit;   
  public string Label
  {
   get
   {
    EnsureChildControls();
    return label.Text;
   }
   set
   {
    EnsureChildControls();
    label.Text = value;
   }
  }
    public int Number
  {
   get
   {
    return number;
   }
   set
   {
    number = value;
   }
  }      
  public string Text1
  {
   get
   {
    EnsureChildControls();
    return box1.Text;
   }
   set
   {
    EnsureChildControls();
    box1.Text = value;
   }
  }      
  public string Text2
  {
   get
   {
    EnsureChildControls();
    return box2.Text;
   }
   set
   {
    EnsureChildControls();
    box2.Text = value;
   }
  }       
  protected override void CreateChildControls()
  {         
   Controls.Add(new LiteralControl("<h3>第一个数字 : "));         
   box1 = new TextBox();
   box1.Text = "0";
   Controls.Add(box1);         
   Controls.Add(new LiteralControl("</h3>"));         
   Controls.Add(new LiteralControl("<h3>第二个数字: "));         
   box2 = new TextBox();
   box2.Text = "0";
   Controls.Add(box2);         
   Controls.Add(new LiteralControl("</h3>"));         
   Button button1 = new Button();
   button1.Text = "点击";
   button1.CommandName = "Click";
   Controls.Add(button1);         
   Button button2 = new Button();
   button2.Text = "重置";
   button2.CommandName = "Reset";
   Controls.Add(button2);         
   Button button3 = new Button();
   button3.Text = "提交";
   button3.CommandName = "Submit";
   Controls.Add(button3);         
   Controls.Add(new LiteralControl("<br><br>"));
   label = new Label();
   label.Height = 50;
   label.Width = 500;
   label.Text = "单击一个按钮";
   Controls.Add(label);         
  }      
  protected override bool OnBubbleEvent(object source, EventArgs e)
  {  
   bool handled = false;
   if (e is CommandEventArgs)
   {
    CommandEventArgs ce = (CommandEventArgs)e;
    if (ce.CommandName == "Click")
    {
     OnClick(ce);
     handled = true;  
    } 
    else if (ce.CommandName == "Reset")
    {
     OnReset(ce);
     handled = true;  
    }
    else if (ce.CommandName == "Submit")
    {
     OnSubmit(ce);
     handled = true;  
    }            
   }
   return handled;           
  }      
  protected virtual void OnClick (EventArgs e)
  {
   if (Click != null)
   {
    Click(this,e);
   }
  }      
  protected virtual void OnReset (EventArgs e)
  {
   if (Reset != null)
   {
    Reset(this,e);
   }
  }      
  protected virtual void OnSubmit (EventArgs e)
  {
   if (Submit != null)
   {
    Submit(this,e);
   }
  } 
 }
}

  public void ClickHandler(Object sender,EventArgs e)
  {
   MyControl.Label = "您点击了 <b> 点击 </b> 按钮";
  }
  public void ResetHandler(Object sender,EventArgs e)
  {
   MyControl.Text1 = "0";
   MyControl.Text2 = "0";   
  }   
  public void SubmitHandler(Object sender,EventArgs e)
  {
   if ( Int32.Parse(MyControl.Text1) + Int32.Parse(MyControl.Text2) == MyControl.Number)
    MyControl.Label = "<h2> 您输入的匹配! </h2>";
   else
    MyControl.Label = "对不起,您输入的数字的和不等于内置数字!";
  }


限制输入内容的复合控件
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml;
using System.Collections;
namespace CompositeInput
{
 [DefaultProperty("Text"),
  ToolboxData("<{0}:ComInput runat=server></{0}:ComInput>")]
 public class ComInput : Control, INamingContainer
  //标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。这仅是一个标记接口。
 {
  private string strFileName = "bad_words.xml";
  private Label label;            
  public string FileName
  {
   get
   {
    return strFileName;
   }
   set
   {
    strFileName = value;
   }
  }            
  //以用户提交的文本内容为输入参数。如果用户提交的内容包含攻击性言辞,
  //则返回修改后的版本,
  //否则,直接返回原始的文本。

  public string CheckString(string InputString)
  {
   ArrayList alWordList = new ArrayList();
   string  xmlDocPath  = this.Page.Server.MapPath("bad_words.xml");
   XmlTextReader  xmlReader = new XmlTextReader(xmlDocPath);
   string asterisks  = "*************************";
   // 将定义攻击性言辞的xml文件内容读入到一个ArrayList
   while (xmlReader.Read())
   {
    if(xmlReader.NodeType==XmlNodeType.Text)
     alWordList.Add(xmlReader.Value);
   }
   xmlReader.Close();
   //检查用户提交的文本内容,将攻击性言辞替换为适当数量的星号
   foreach(string element in alWordList)
    InputString=InputString.Replace(element,asterisks.Substring(1, (element.Length)));
   return InputString ;
  }
public string Text
  {
   get
   {
    EnsureChildControls();
    return label.Text;
   }
   set
   {
    EnsureChildControls();
    label.Text = value;
   }
  }                   
  public event CheckEventHandler Check;            
  protected virtual void OnCheck(CheckEventArgs ce)
  {
   if (Check != null)
   {
    Check(this,ce);
   }
  }            
  protected override void CreateChildControls()
  {
   Controls.Add(new LiteralControl("<h3>请在下面输入文字内容: "));
   TextBox box1 = new TextBox();
   box1.Text = "";
   Controls.Add(box1);                  
   Controls.Add(new LiteralControl("</h3>"));                  
   Button button1 = new Button();
   button1.Text = "提交";
   Controls.Add(new LiteralControl("<br>"));
   Controls.Add(button1);
   button1.Click += new EventHandler(this.ButtonClicked);                  
   Controls.Add(new LiteralControl("<br><br>"));
   label = new Label();
   label.Height = 50;
   label.Width = 500;
   label.Text = "";
   Controls.Add(label);                  
  }            
  protected override void OnPreRender(EventArgs e)
  {
   ((TextBox)Controls[1]).Text = "";
  }            
  private void ButtonClicked(Object sender, EventArgs e)
  {
   OnCheck(new CheckEventArgs(((TextBox)Controls[1]).Text,CheckString(((TextBox)Controls[1]).Text)));
  }
 }
 public class CheckEventArgs : EventArgs
 {
  private bool match = false;
           
  public CheckEventArgs (string str1,string str2)
  {
   if (str1 == str2)
   {
    match = true;
   }
  }
  public bool Match
  {
   get
   {
    return match;
   }
  }
 }      
 public delegate void CheckEventHandler(object sender, CheckEventArgs ce);
}

  public  void CheckText(object sender,CompositeInput.CheckEventArgs e)
  {
   if(e.Match)
    ComInput1.Text = "你提交的内容已通过检查!";
   else
    ComInput1.Text = "<h2>发布内容请遵守本站规则!不得发布攻击性言辞!</h2>";
  }

 

转载地址:https://blog.csdn.net/lisong58420/article/details/2302813 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:40种网站设计常用技巧
下一篇:ASP.NET中的XML

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.191.171.12]2022年08月18日 06时01分32秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

最新文章