zoukankan      html  css  js  c++  java
  • 做最简单的自定义控件!

    一、在做项目时候通常会用到一些通用的控件,比如说一些表的状态,为了不至于每个界面写相同代码,底下写了一个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>      

           希望对大家有所帮助!!!           

  • 相关阅读:
    python中拆分和拼接字符串
    python中find查找是否包含特定字符并返回第一个索引
    python中删除字符串左右的空格
    python中指定字符串宽度,并指定填充字符串
    python中将字符串中的制表符转换为空格
    python中统计字符串中特定字符出现的次数,内置方法count
    python中指定分隔符分割字符串,返回三元组
    学习笔记:社会科学的特质
    启示——来自《DOOM启世录》
    多语言处理 > UNICODE > IBM的ICU类库
  • 原文地址:https://www.cnblogs.com/BeyondWJsel/p/2411670.html
Copyright © 2011-2022 走看看