一、在做项目时候通常会用到一些通用的控件,比如说一些表的状态,为了不至于每个界面写相同代码,底下写了一个Lable和Dropdowlist。详细如下:
1、//显示Lable状态控件
1 public class ChatRoomMemberStateLabel:Label
2 {
3 private int _ChatRoomState;
4 /// <summary>
5 /// 交流室成员状态显示
6 /// </summary>
7 public int ChatRoomState
8 {
9 get { return _ChatRoomState; }
10 set
11 {
12 _ChatRoomState = value;
13 if (_ChatRoomState == (int)CommonState.Invalid)
14 {
15 this.Text = "禁用";
16 }
17 else
18 {
19 this.Text = "启用";
20 }
21 }
22 }
23 }
2、对应插入下拉框代码
/// <summary>
/// 封装试题题型下拉框
/// </summary>
public class ComDraftTypeDrop:DropDownList
{
private bool _TrySelectedValueSelected; //代表是否有默认值
private string _TryAllContainsValue; //是否包含《全部》选项
protected override void OnInit(EventArgs e)
{
DraftQuestionTypeLogic cStrDraftQuestionTypeLogic = new DraftQuestionTypeLogic();
DraftQuestionTypeList cStrDraftQuestionTypeList = new DraftQuestionTypeList();
cStrDraftQuestionTypeList = cStrDraftQuestionTypeLogic.GetListAll();
foreach (DraftQuestionType cDraftQuestionType in cStrDraftQuestionTypeList)
{
this.Items.Add(new ListItem(cDraftQuestionType.QuestionTypeName, string.Format("{0}:{1}", cDraftQuestionType.DraftQuestionTypeId.ToString(), cDraftQuestionType.ComplexTypeFlag.ToString())));
}
if (!string.IsNullOrEmpty(_TryAllContainsValue))
{
Items.Insert(0, new ListItem("全部", "-1"));
}
base.OnInit(e);
}
public string TrySelectedValue
{
get { return SelectedValue; }
set
{
foreach (ListItem li in this.Items)
{
if("-1".Equals(li.Value))
{
continue;
}
string[] aStrArray = li.Value.Split(':');
if (aStrArray[0] == value)
{
SelectedValue = value;
_TrySelectedValueSelected = true;
}
}
if (_TrySelectedValueSelected == false)
{
SelectedIndex = 0;
}
}
}
/// <summary>
/// 是否包含《全部》选项
/// </summary>
public string TryContainsAllValue
{
get { return _TryAllContainsValue; }
set
{
if (!string.IsNullOrEmpty(value))
{
_TryAllContainsValue = value;
}
}
}
}
3、页面使用,导入控件所在的命名空间,注册方式有两种,一种是页面注册,另一种是web.config注册
<%@ Register Assembly="Open.ItemBank.Controls" Namespace="Open.ItemBank.Controls" TagPrefix="cc1" %>//页面
<pages styleSheetTheme="Default" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"> //web.config注册,推荐用这一种
<controls>
<add tagPrefix="itembank" namespace="Open.ItemBank.Controls" assembly="Open.ItemBank.Controls"/>
</controls>
</pages>
使用时候如:
<itembank:ChatRoomMemberStateLabel ID="CommonStateLabel" runat="server" ChatRoomState=“0”></itembank:ChatRoomMemberStateLabel>
<cc1:ChatRoomMemberStateLabel ID="CommonStateLabel" runat="server" ChatRoomState=“0”></cc1:ChatRoomMemberStateLabel>
希望对大家有所帮助!!!