Web控件开发系列(五) 控件事件

控件在WEB开发时经常要用到,虽然有部分已经存在工具箱里,但有时总需要根据自己的要求,开发一些合适自己的控件。接上一节,已经说过了控件的属性,下面来说一下,控件里面究竟怎样定义事件呢? 首先,大家都知道,控件里面的子控件可能会有需要事件,也就是子控件在事件中处理业务流程,还有就是控件本身就需要事件,像button那样,有常用的click事件,那么我们自定义的控件是否有需要这方面的事件存在呢?如果需要应该怎样添加呢?下面介绍一下,控件里面子控件的事件,例如我们在web页面上需要一个简单计数器给用户进行+ - * /这四个操作,下面介绍一下实现的代码,在ServerControl里面添加一个Caler的类。

以下为引用的内容:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Caler runat=server></{0}:Caler>")]
public class Caler : CompositeControl
{
private TextBox txt1; //计算的第一个数
private TextBox txt2; //计算的第二个数
private TextBox txt3; //计算的结果
private Button btn1; //加
private Button btn2; //减
private Button btn3; //乘
private Button btn4; //除

//关于控件文本框只能输入数字的判断大家自己加进,这里只实现用户前提是输入数字的情况下,
//也不包括长度,因为我在textbox中只设了10的长度,多出部分会去掉,实际开发中还需要加进好
//多的判断,这些由大家去添加吧..这里只说明怎样添加控件的事件而已

private Unit ButtonWidth = Unit.Pixel(30);

protected override void CreateChildControls()
{
this.Controls.Clear();

txt1 = new TextBox();
txt1.ID = "txt1";
txt1.MaxLength = 10;
this.Controls.Add(txt1);

txt2 = new TextBox();
txt2.ID = "txt2";
txt2.MaxLength = 10;
this.Controls.Add(txt2);

txt3 = new TextBox();
txt3.ID = "txt3";
txt3.MaxLength = 10;
this.Controls.Add(txt3);

btn1 = new Button();
btn1.ID = "btn1";
btn1.Text = "+";
btn1.Click += new EventHandler(btn1_Click); //添加事件
this.Controls.Add(btn1);

btn2 = new Button();
btn2.ID = "btn2";
btn2.Text = "-";
btn2.Click += new EventHandler(btn2_Click);
this.Controls.Add(btn2);

btn3 = new Button();
btn3.ID = "btn3";
btn3.Text = "*";
btn3.Click += new EventHandler(btn3_Click);
this.Controls.Add(btn3);

btn4 = new Button();
btn4.ID = "btn4";
btn4.Text = "/";
btn4.Click += new EventHandler(btn4_Click);
this.Controls.Add(btn4);
this.ChildControlsCreated = true;
}

protected override void RecreateChildControls()
{
if (this.ChildControlsCreated == false)
{
base.RecreateChildControls();
}
}

protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
writer.RenderBeginTag(HtmlTextWriterTag.Table);

writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this.txt1.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();

writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this.txt2.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();

writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this.txt3.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();

writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this.btn1.RenderControl(writer);
this.btn2.RenderControl(writer);
this.btn3.RenderControl(writer);
this.btn4.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();

writer.RenderEndTag();
}

private void btn4_Click(object sender, EventArgs e)
{
if (this.txt1.Text.Trim().Length > 0 && this.txt2.Text.Trim().Length > 0)
{
txt3.Text = (Convert.ToDouble(txt1.Text) / Convert.ToDouble(txt2.Text)).ToString();
}
else
{
txt3.Text = "";
}
}

private void btn3_Click(object sender, EventArgs e)
{
if (this.txt1.Text.Trim().Length > 0 && this.txt2.Text.Trim().Length > 0)
{
txt3.Text = (Convert.ToDouble(txt1.Text) * Convert.ToDouble(txt2.Text)).ToString();
}
else
{
txt3.Text = "";
}
}

private void btn2_Click(object sender, EventArgs e)
{
if (this.txt1.Text.Trim().Length > 0 && this.txt2.Text.Trim().Length > 0)
{
txt3.Text = (Convert.ToDouble(txt1.Text) - Convert.ToDouble(txt2.Text)).ToString();
}
else
{
txt3.Text = "";
}
}

private void btn1_Click(object sender, EventArgs e)
{
if (this.txt1.Text.Trim().Length > 0 && this.txt2.Text.Trim().Length > 0)
{
txt3.Text = (Convert.ToDouble(txt1.Text) + Convert.ToDouble(txt2.Text)).ToString();
}
else
{
txt3.Text = "";
}
}
}
}

转载请注明:代码家园 » Web控件开发系列(五) 控件事件

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)