• CheckBoxList的操作查询是否被选中设置或者得到


    在项目中我们可能会经常遇到一收集多选信息的情况,比如做注册的时候要收集个人爱好,那时候大家第一个想到的肯定是CheckBoxList。那我们怎么来获取到CheckBoxList的值并且存入数据库呢??

    如果我们还需要编辑用户的个人信息而其中爱好也是可以改动的,此时同样大家也会想用CheckBoxList去显示用户的各人信息,那我们又要如何将库里的值用CheckBoxList表示出来呢?? 编辑分析问题
    遇到这种情况大家肯定想到for,foreach去遍历,没错这样完没有问题,不管是获取CheckBoxList的值还是设置CheckBoxList的值我们都可以用遍历去实现。而我这里将大家常用的方法总结了一下,做了两个方法。这样用起来能更方便灵活。

    举个例子:我们要收集某某公司员工的信息其中一项是爱好。而且要求员工信息可以改动

    我们选用了CheckBoxList来实现爱好的收集和显示

    方法:

    ......

    1.收集时,将CheckBoxList里选中的项转换成字符串,并用“,”隔开
    这里只要调用方法GetChecked(CheckBoxList checkList, string separator)
    就可以获取到想要的数据。然后存入数据库。

    2.显示时,先从库里获取爱好的数据(刚刚用“,”隔开的字符串),
     然后调用方法SetChecked(CheckBoxList checkList,string selval,string separator)
    就可以将库里的数据用CheckBoxList的形式表现出来

    ......
    方法的使用: 
    //这里获取CheckBoxList中的选中项并用","隔开
    string str=GetChecked(this.checkList1, ",");
    ......

    //这里是将str这个字符串的值又设回CheckBoxList
    SetChecked(this.checkList1,str,",");
    
            /// <summary>
            /// 初始化CheckBoxList中哪些是选中了的         /// </summary>
            /// <param name="checkList">CheckBoxList</param>
            /// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>
            /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
            public static string SetChecked(CheckBoxList checkList,string selval,string separator)
            {
                selval = separator + selval + separator;        //例如:"0,1,1,2,1"->",0,1,1,2,1,"
                for(int i=0; i<checkList.Items.Count; i++)
                {
                    checkList.Items[i].Selected = false;
                    string val = separator + checkList.Items[i].Value + separator;
                    if(selval.IndexOf(val)!=-1)
                    {
                        checkList.Items[i].Selected = true;
                        selval = selval.Replace(val,separator);        //然后从原来的值串中删除已经选中了的
                        if(selval == separator)        //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
                        {        
                            selval += separator;        //添加一个分隔符
                        }
                    }
                }
                selval = selval.Substring(1,selval.Length-2);        //除去前后加的分割符号
                return selval;
            }
    
            /// <summary>
            /// 得到CheckBoxList中选中了的值
            /// </summary>
            /// <param name="checkList">CheckBoxList</param>
            /// <param name="separator">分割符号</param>
            /// <returns></returns>
            public static string GetChecked(CheckBoxList checkList, string separator)
            {
                string selval = "";
                for(int i=0;i<checkList.Items.Count;i++)
                {
                    if(checkList.Items[i].Selected)
                    {
                        selval += checkList.Items[i].Value + separator;
                    }
                }
                return selval;
            }

    全选的是  添加个button  这个单击事件写:

      for (int b = 0; b < CheckBoxList1.Items.Count; b++)
            {
                    this.CheckBoxList1.Items[b].Selected = true;
            }

    引用自  http://www.cnblogs.com/shawker/archive/2009/03/17/1414795.html

    做出来的题

    空间拖成这样

    前台代码

     <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Number" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:TemplateField HeaderText="选择">
                        <HeaderStyle HorizontalAlign="Center" Height="25px" Width="45px" />
                        <ItemTemplate>
                            <asp:CheckBox ID="ckb" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:BoundField DataField="Number" HeaderText="Number" InsertVisible="False" ReadOnly="True" SortExpression="Number" />
                    <asp:BoundField DataField="NumName" HeaderText="NumName" SortExpression="NumName" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoConnectionString %>" SelectCommand="SELECT * FROM [DataTb]"></asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        </form>

    后台代码

     protected void Button1_Click(object sender, EventArgs e)
            {
                string PKname = "";
                foreach (GridViewRow GR in this.GridView1.Rows)
                {
                    CheckBox CB = (CheckBox)GR.FindControl("ckb");
                    if (CB.Checked)
                    {
                        PKname += this.GridView1.DataKeys[GR.RowIndex].Value.ToString() + ",";
                    }
                }
                Response.Write(PKname);
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow GR in this.GridView1.Rows)
                {
                    CheckBox CB = (CheckBox)GR.FindControl("ckb");
                    CB.Checked = true;
                }
            }

     后台获取全选的方法.

         protected void Page_Load(object sender, EventArgs e)
            {
                CheckBox cbAll = (CheckBox)GridView1.HeaderRow.FindControl("sss");
                if (cbAll.Checked)
                {
                    NewMethod();
                }
                else
                {
                    NoNewMethod();
    
                }
            }

    那两个方法是

      private void NewMethod()
            {
                foreach (GridViewRow GR in this.GridView1.Rows)
                {
                    CheckBox CB = (CheckBox)GR.FindControl("ckb");
                    CB.Checked = true;
                }
            }
            private void NoNewMethod()
            {
                foreach (GridViewRow GR in this.GridView1.Rows)
                {
                    CheckBox CB = (CheckBox)GR.FindControl("ckb");
                    CB.Checked = false;
                }
            }

    前台js获取全选 和选中变色的方法

    <script>
            function SelectAll(tempControl) {
                //将除头模板中的其它所有的CheckBox取反 
                var theBox = tempControl;
                xState = theBox.checked;
                elem = theBox.form.elements;
                for (i = 0; i < elem.length; i++)
                    if (elem[i].type == 'checkbox' && elem[i].id != theBox.id) {
                        if (elem[i].checked != xState)
                            elem[i].click();
                    }
    
            }
            function changecolor(cbo, o) {
                var theBox = cbo;
                var tr = document.getElementById(o);
                if (theBox.checked) {
                    tr.style.backgroundColor = "Red";
                }
                else {
                    tr.style.backgroundColor = "#CCCCCC";
    
                }
            }
            function ttt() {
                alert("aaa");
            }
        </script>

    使用js的方法

     <HeaderTemplate>
                            <asp:CheckBox ID="sss" runat="server" Text="全选" onclick="javascript:SelectAll(this);" />
                        </HeaderTemplate>

     repeater编辑check是否被选中的方法

      string ids = "";
                for (int i = 0; i < Repeater1.Items.Count; i++)
                {
                    //找到相应的Checkbox控件  
                    CheckBox cb = (CheckBox)Repeater1.Items[i].FindControl("chk_word1");
                    //获取id并连接成字符串,用,分隔  
                    if (cb != null)
                    {
                        string selectedId = cb.ToolTip;
                        if (cb.Checked)
                        {
                            ids += selectedId;
                        }
                    }
                }


    前台中的chk_word1是

    <asp:CheckBox ID="chk_word1" ToolTip='<%#Eval("id")%>' runat="server" />

    另外上面的用foreach循环的方法 更简单

        foreach (RepeaterItem rt in this.Repeater1.Items)
                {
                    CheckBox cb = (CheckBox)rt.FindControl("chk_word1");
                    //获取id并连接成字符串,用,分隔  
                    if (cb != null)
                    {
                        string selectedId = cb.ToolTip;
                        //判断是否被选中
                        if (cb.Checked)
                        {
                            //可以获取循环别的空间                      
                  HiddenField hid1 = (HiddenField)rt.FindControl("CPhone1"); HiddenField hid2 = (HiddenField)rt.FindControl("CPhone2"); ids += hid1.Value; ids += hid2.Value; ids += selectedId; } } }
  • 相关阅读:
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......
    模拟Executor策略的实现
    设计3D标签
    创建被图像填充的组件
    netty基础09_利用EmbeddedChannel做单元测试
    netty基础08_引导类
    netty基础07_Netty提供的消息处理器和编码解码器
    netty基础06_编码器和解码器
    netty基础05_管道和消息处理器
    netty基础04_数据缓冲区
  • 原文地址:https://www.cnblogs.com/crazyair/p/3669753.html
Copyright © 2020-2023  润新知