首页 > 开发 > 综合 > 正文

用C#写一个Web自定义控件

2024-07-21 02:18:06
字体:
来源:转载
供稿:网友
原来写的用户控件,但是维护太麻烦,后来就写了一个自定义控件,比较好维护,使用也方便多了。
该控件是一个时间控件,通过下拉列表框选择时间的年、月、日、时、分、秒。初始化时要提供可选时间的
起始年份。

源代码如下:

using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.componentmodel.design;

namespace usertime
{
/// <summary>
/// 用户自定义时间控件,取到年、月、日、时、分、秒
/// </summary>
[defaultproperty("text"),
toolboxdata("<{0}:clsusertime runat=server></{0}:clsusertime>")]
//inamingcontainer为子控件提供了一个新的命名范围,保证子控件的id唯一性
public class clsusertime : system.web.ui.webcontrols.webcontrol, inamingcontainer
{
private system.web.ui.webcontrols.dropdownlist ddlyear;
private dropdownlist ddlmonth;
private dropdownlist ddlday;
private dropdownlist ddlhour;
private dropdownlist ddlminite;
private dropdownlist ddlsecond;

private static int beginyear = 0; //起始年号,这里必须使用static以防二次调用后变量被清零
private static bool isinit = false; //是否已经初始化过,这里必须使用static以防二次调用后变量被清零

public int year //取控件时间:年
{
get
{
return ddlyear.selectedindex + beginyear;
}
}

public int month //取控件时间:月
{
get
{
return ddlmonth.selectedindex + 1;
}
}

public int day //取控件时间:日
{
get
{
return ddlday.selectedindex + 1;
}
}

public int hour //取控件时间:时
{
get
{
return ddlhour.selectedindex;
}
}

public int minite //取控件时间:分
{
get
{
return ddlminite.selectedindex;
}
}

public int second //取控件时间:秒
{
get
{
return ddlsecond.selectedindex;
}
}

//用起始年号初始化控件,如果已经初始化过控件,直接返回
public void initcontrols(int startyear, int endyear)
{
if (isinit)
{
return;
}
ensurechildcontrols();
if (startyear > endyear)
{
int tmp = startyear;
startyear = endyear;
endyear = startyear;
}
beginyear = startyear;
for (int i = startyear; i <= endyear; ++i)
{
ddlyear.items.add(i.tostring());
}
for (int i = 1; i <=12; ++i)
{
ddlmonth.items.add(i.tostring());
}
for (int i = 1; i <= 31; ++i)
{
ddlday.items.add(i.tostring());
}
for (int i = 0; i < 24; ++i)
{
ddlhour.items.add(i.tostring());
}
for (int i = 0; i < 60; ++i)
{
ddlminite.items.add(i.tostring());
ddlsecond.items.add(i.tostring());
}
isinit = true;
}
//重载controls属性,取得controlcollection对象,该对象表示ui层次结构中的服务器控件的子控件
public override controlcollection controls
{
get
{
//确定服务器是否包含子控件
ensurechildcontrols();
return base.controls;
}
}

//动态创建子控件
protected override void createchildcontrols()
{
//创建年、月、日、时、分、秒下拉列表框
this.controls.clear();
ddlyear = new dropdownlist();
ddlyear.id = "ddlyear";
ddlyear.width = 54;

ddlmonth = new dropdownlist();
ddlmonth.id = "ddlmonth";
ddlmonth.width = 40;

ddlday = new dropdownlist();
ddlday.id = "ddlday";
ddlday.width = 40;

ddlhour = new dropdownlist();
ddlhour.id = "ddlhour";
ddlhour.width = 40;

ddlminite = new dropdownlist();
ddlminite.id = "ddlminite";
ddlminite.width = 40;

ddlsecond = new dropdownlist();
ddlsecond.id = "ddlsecond";
ddlsecond.width = 40;

this.controls.add(ddlyear);
this.controls.add(ddlmonth);
this.controls.add(ddlday);
this.controls.add(ddlhour);
this.controls.add(ddlminite);
this.controls.add(ddlsecond);

base.createchildcontrols ();
}

/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 html 编写器 </param>
protected override void render(htmltextwriter output)
{
//将要呈现的html属性添加到指定的system.web.htmltextwriter中
addattributestorender(output);
output.addattribute(system.web.ui.htmltextwriterattribute.cellpadding, "0", false);

//制定一个表,把子控件排在一行上
output.renderbegintag(htmltextwritertag.table); //表
output.renderbegintag(htmltextwritertag.tr); //行

//年下拉列表框
output.renderbegintag(htmltextwritertag.td); //列
ddlyear.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("年");
output.renderendtag();

//月下拉列表框
output.renderbegintag(htmltextwritertag.td);
ddlmonth.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("月");
output.renderendtag();

//日下拉列表框
output.renderbegintag(htmltextwritertag.td);
ddlday.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("日");
output.renderendtag();

//时下拉列表框
output.renderbegintag(htmltextwritertag.td);
ddlhour.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("时");
output.renderendtag();

//分下拉列表框
output.renderbegintag(htmltextwritertag.td);
ddlminite.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("分");
output.renderendtag();

//秒下拉列表框
output.renderbegintag(htmltextwritertag.td);
ddlsecond.rendercontrol(output);
output.renderendtag();

output.renderbegintag(htmltextwritertag.td);
output.write("秒");
output.renderendtag();

output.renderendtag();
output.renderendtag();
}

public datetime gettime() //取得选定的时间
{
datetime time = new datetime(this.year, this.month, this.day, this.hour, this.minite, this.second, 0);
return time;
}

//清除控件里所有下拉列表框的值
public void clearcontrols()
{
ddlyear.items.clear();
ddlmonth.items.clear();
ddlday.items.clear();
ddlhour.items.clear();
ddlminite.items.clear();
ddlsecond.items.clear();
isinit = false;
}

//撤消对所有下拉列表框的选项
public void clearselection()
{
ddlyear.clearselection();
ddlmonth.clearselection();
ddlday.clearselection();
ddlhour.clearselection();
ddlminite.clearselection();
ddlsecond.clearselection();
}
}
}



最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表