项目经历——CheckboxListbox 小结

  在项目中,进行设定考核对象的时候。如果有已经设定的考核对象,那么对已经设定的考核对象复选框进行勾选。如果没有设定,可进行勾选设定考核对象用到CheckboxListbox ,在这里用到CheckboxListbox 。简单来介绍一下。

一、属性和事件

1、AutoPostBack属性:用于设置当单击checkboxList控件时,是否自动回送到服务器。True表示回送;False(默认)表示不回送。
2、DataSource属性:用于指定填充列表控件的数据源。
3、DataTextField属性:指定DataSource中一个字段,该字段的值对应于列表项的Text属性。
4、DataValueField属性:指定DataSource中一个字段,字段的值对应于列表项的Value属性。
5、Items属性:表示复选框列表中各个选项的集合,如CheckBoxList1.Items(i)表示第i个选项,i从0开始。
每个选项都有以下3个基本属性:
Text属性:表示每个选项的文本。
Value属性:表示每个选项的选项值。
Selected属性:表示该选项是否被选中。
6、RepeatColumns属性:用于指定在CheckBoxList控件中显示选项占用几列。默认值为0,表示任意多列。
7、RepeatDirection属性:用于指定CheckBoxList控件的显示方向。Vertical时,列表项以列优先排列的形式显示;Horizontal时,列项以行优先排列的形式显示。
8、RepeatLayout属性:用于设置选项的排列方式。Table(默认)时,以表结构显示,属性值为Flow时,不以表结构显示。
9、SelectedIndex属性:用于获取或设置列表中选定项的最低序号索引值。如果列表控件中只有一个选项被选中,则该属性表示当前选定项的索引值。
10、SelectedItem属性:用于获取列表控件中索引值最小的选定项。如果列表中只有一个选项被选中,则该属性表示当前选定项。通过该属性可获得选定项的Text和Value属性值。
11:SelectedIndexchanged事件:当用户选择了列表中的任意复选框时,都将引发事件。


二、具体实例

  HTML代码:

<div title="开发区(园区)" style="padding: 10px">    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
       <ContentTemplate>
          <div>
            <asp:Button ID="cmdSelectedDevelopment" runat="server" Text="提交" ToolTip="提交选择的考核对象" OnClick="cmdSelectedDevelopment_Click" Style="height: 20px" />
            </div>
             <asp:CheckBox ID="SelectAll" runat="server" Text="全选" OnClick="selectAll('cblDevelopmentInfo')" />
             <asp:CheckBoxList ID="cblDevelopmentInfo" RepeatDirection="Horizontal" RepeatColumns="5" runat="server"></asp:CheckBoxList>
       </ContentTemplate>
    </asp:UpdatePanel>
   </div>

  JS全选

    <%--全选checkboxlist--%>
    <script type="text/javascript">
        function selectAll(searchName) {
            var aa = document.getElementsByTagName("input");
            for (var i = 0; i < aa.length; i++) {
                if (aa[i].type == 'checkbox' && aa[i].name.indexOf(searchName) > -1) {
                    aa[i].checked = event.srcElement.checked;
                }
            }
        }
    </script>


  C#代码:


//页面首次加载的时候,绑定数据
if (!IsPostBack == true)
{
 this.cblDevelopmentInfo.DataSource = development.GetDevelopmentInfo();
 this.cblDevelopmentInfo.DataTextField = "DepartmentName"; //前台看到的值,也就是CheckBoxList中显示出来的值 
 this.cblDevelopmentInfo.DataValueField = "CityID"; //这个值直接在页面上是看不到的,但在源代码中可以看到   
 this.cblDevelopmentInfo.DataBind();
}

 //对选择的Checboxlist进行相关的数据库操作
 //获取已经选择的考核对象
 DataSet dtCheckedDepartInfo = development.GetList("Checked='Yes' and Status='Active'");
 // 外循环是获取选择考核对象
 for (int i = 0; i < dtCheckedDepartInfo.Tables[0].Rows.Count; i++)
  {
  //已经选择的考核对象字段
   string strCheckedCity = dtCheckedDepartInfo.Tables[0].Rows[i]["CityID"].ToString();
   for (int j = 0; j < cblDevelopmentInfo.Items.Count; j++)
    {
     //全部的考核对象
     string strUnCheckedCity = cblDevelopmentInfo.Items[j].Value.ToString();
     if (strCheckedCity == strUnCheckedCity)
     {
      cblDevelopmentInfo.Items[j].Selected = true;
      break;
     }
    }
  }
 将选择的更新到数据库中
 #region 开发区Tab中 单击提交按钮,提交选择的考核对象到数据库中
 protected void cmdSelectedDevelopment_Click(object sender, EventArgs e)
  {
   EvaluationSystem.Model.DevelopmentBaseEntity developmententity = new DevelopmentBaseEntity(); //实例化对应的实体类
   EvaluationSystem.BLL.DevelopmentBaseBLL developmentrecord = new DevelopmentBaseBLL();//实例化对应的B层业务逻辑
   developmentrecord.UpdateNoSelected();
   for (int i = 0; i < this.cblDevelopmentInfo.Items.Count; i++)   //通过循环
   {
   if (this.cblDevelopmentInfo.Items[i].Selected)//获取控件中选定的项
   {
    developmententity.CityID = Convert.ToString(this.cblDevelopmentInfo.Items[i].Value); // 获取实体ID
    if (developmentrecord.UpdateChecked(developmententity.CityID) == true)// 如果添加成功,进行提示
    {
      ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('已成功设定考核对象')", true);
      this.cblDevelopmentInfo.Items[i].Selected = true;
      SelectAll.Checked = false;
    }
   else
   {
    ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('设定考核对象失败,请联系系统管理员')", true);
   }
  }
 }
}
#endregion


三、小结

  这里的Checkboxlist结合Asp.net中自带的UpdataPannel实现了项目中的一个小业务功能。仔细想想,无非是控件的一些基本属性,还有就是JS对html的相关操作,然后结合循环结构,从而实现要求。





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 33
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值