zoukankan      html  css  js  c++  java
  • 一、自定义控件之CheckBoxList

    因需求不同

    最近公司项目要求开发自定义控件,看了些前辈的代码……现在结合自己的项目。

                       自定义CheckBoxList 主要功能: 全选功能、获取任一checkbox的Text或者Value值

    后台CS:

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.ComponentModel;

    namespace FumaCRM_BS.WebControls
    {
    #region CheckBoxList
    /// <summary>
    /// 多项选择复选框组
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData(
    "<{0}:FBSCheckBoxList runat=server></{0}:FBSCheckBoxList>")]
    public class FBSCheckBoxList : System.Web.UI.WebControls.CheckBoxList
    {
    private const char SEPARATOR = '|';//分隔符

    #region Field
    private bool selectAll;//全选
    #endregion
    [Category(
    "Appearance"), Browsable(true), Description("是否全选"), DefaultValue(false)]
    public bool SelectAll
    {
    set
    {
    selectAll
    = value;
    }
    get
    {
    return selectAll;
    }
    }
    /// <summary>
    /// 选中项的Text值
    /// </summary>
    [Category("Appearance"), Browsable(true), Description(" 选中项的Text值"), DefaultValue("")]
    public string NewSelectedText
    {
    set
    {
    //newSelectText = value;
    if (value != null && value != string.Empty && value != "")
    {
    this.SelectedIndex = -1;
    //重新选择
    string[] textList = value.Split(new char[] { SEPARATOR });
    for (int i = 0; i < textList.Length; i++)
    {
    for (int j = 0; j < this.Items.Count; j++)
    {
    if (this.Items[j].Text == textList[i])
    {
    this.Items[j].Selected = true;
    }
    }
    }
    }
    }
    get
    {
    //return newSelectText;
    string textList = string.Empty;
    for (int i = 0; i < this.Items.Count; i++)
    {
    if (this.Items[i].Selected == true)
    {
    textList
    += this.Items[i].Text + SEPARATOR.ToString();
    }
    }
    if (textList != string.Empty)
    {
    return textList.Substring(0, textList.Length - 1);
    }
    else
    {
    return textList;
    }
    }
    }

    /// <summary>
    /// 属性:选中项的Value值
    /// </summary>
    [Category("Appearance"), Browsable(true), Description(" 选中项的Value值"), DefaultValue("")]
    public string NewSelectedValue
    {
    set
    {
    if (value != null && value != string.Empty && value != "")
    {
    this.SelectedIndex = -1;
    //重新选择
    string[] valueList = value.Split(new char[] { SEPARATOR });
    for (int i = 0; i < valueList.Length; i++)
    {
    for (int j = 0; j < this.Items.Count; j++)
    {
    if (this.Items[j].Value == valueList[i])
    {
    this.Items[j].Selected = true;
    }
    }
    }
    }
    }
    get
    {
    string valueList = string.Empty;
    for (int i = 0; i < this.Items.Count; i++)
    {
    if (this.Items[i].Selected == true)
    {
    valueList
    += this.Items[i].Value + SEPARATOR.ToString();
    }
    }
    if (valueList != string.Empty)
    {
    return valueList.Substring(0, valueList.Length - 1);
    }
    else
    {
    return valueList;
    }
    }
    }

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);
    if (selectAll)
    {
    for (int i = 0; i < this.Items.Count; i++)
    {
    Items[i].Selected
    = true;
    }
    }
    else
    {
    for (int i = 0; i < this.Items.Count; i++)
    {
    if (Items[i].Selected)
    Items[i].Selected
    = true;
    else
    Items[i].Selected
    = false;
    }
    }
    }
    protected override void Render(HtmlTextWriter writer)
    {
    base.Render(writer);
    }
    }
    #endregion
    }

    前台aspx 同行在工具中 右键 选择项  引入刚才编译好的dll

    直接拖拽到aspx中即可 代码如下

    View Code
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" runat="server">
    <cc1:FBSValidateBox ID="FBSValidateBox1" Max="3" Min="1" runat="server"></cc1:FBSValidateBox>
    </asp:Panel>
    <Upload:FBSUpload runat="server" />
    </div>
    <div style="margin: 10px 0;">
    <a href="#" class="easyui-linkbutton" onclick="loadData()">LoadData</a> <a href="#"
    class
    ="easyui-linkbutton" onclick="setValue()">SetValue</a> <a href="#" class="easyui-linkbutton"
    onclick
    ="getValue()">GetValue</a> <a href="#" class="easyui-linkbutton" onclick="disable()">
    Disable
    </a> <a href="#" class="easyui-linkbutton" onclick="enable()">Enable</a>
    </div>
    <select id="cc" class="easyui-combobox" name="state" style=" 200px;" required="true">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="HI">Hawaii</option>
    <option value="ID">Idaho</option>
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
    <option value="IA">Iowa</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="ME">Maine</option>
    <option value="MD">Maryland</option>
    <option value="MA">Massachusetts</option>
    <option value="MI">Michigan</option>
    <option value="MN">Minnesota</option>
    <option value="MS">Mississippi</option>
    <option value="MO">Missouri</option>
    <option value="MT">Montana</option>
    <option value="NE">Nebraska</option>
    <option value="NV">Nevada</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NM">New Mexico</option>
    <option value="NY">New York</option>
    <option value="NC">North Carolina</option>
    <option value="ND">North Dakota</option>
    <option value="OH" selected>Ohio</option>
    <option value="OK">Oklahoma</option>
    <option value="OR">Oregon</option>
    <option value="PA">Pennsylvania</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="SD">South Dakota</option>
    <option value="TN">Tennessee</option>
    <option value="TX">Texas</option>
    <option value="UT">Utah</option>
    <option value="VT">Vermont</option>
    <option value="VA">Virginia</option>
    <option value="WA">Washington</option>
    <option value="WV">West Virginia</option>
    <option value="WI">Wisconsin</option>
    <option value="WY">Wyoming</option>
    </select>
    <cc1:FBSCheckBoxList ID="FBSCheckBoxList1" runat="server">
    <asp:ListItem Value="1">选项A</asp:ListItem>
    <asp:ListItem Value="2">选项B</asp:ListItem>
    <asp:ListItem Value="3">选项C</asp:ListItem>
    <asp:ListItem Value="4">选项D</asp:ListItem>
    <asp:ListItem Value="5">选项E</asp:ListItem>
    <asp:ListItem Value="6">选项F</asp:ListItem>
    <asp:ListItem Value="7">选项G</asp:ListItem>
    <asp:ListItem Value="8">选项H</asp:ListItem>
    </cc1:FBSCheckBoxList>
    <asp:Button ID="Button11" runat="server" Text="设置选择项Text" OnClick="Button11_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" Text="获取选择项Text" OnClick="Button2_Click" />
    <br />
    <asp:Button ID="Button3" runat="server" Text="设置选择项Value" OnClick="Button3_Click" />
    <br />
    <asp:Button ID="Button4" runat="server" Text="获取选择项Value" OnClick="Button4_Click" /><br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="获取选择项Test+Value" OnClick="Button5_Click" /><br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>

    页面如下

    可以跟不同的需求 实现全选,获取任意checkbox值及value 也可以通过设置默认text和value

    之前做的项目一直是用到服务器端控件和html控件,自己自定义开发控件 刚接触  所以 有什么不妥的 请大家指教 谢谢

    如果 您有这块自定义代码   可否 email给我一份 感谢 pepe_anhwei@126.com  


    作者:PEPE
    出处:http://pepe.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    根据现有文件生成图形化的目录树
    一个最简的短信验证码倒计时例子
    将指定目录下的所有资源整体迁移到另一个目录下
    通过 url 获取相应的 location 信息
    node-glob的*匹配
    mysql将查询出来的一列数据拼装成一个字符串
    Call to undefined function mysql_connect()错误原因
    JavaScript转unix时间戳
    .net3.0 中跨线程访问控件
    WPF的线程模型
  • 原文地址:https://www.cnblogs.com/PEPE/p/pepe_anhwei.html
Copyright © 2011-2022 走看看