zoukankan      html  css  js  c++  java
  • 动态加载CheckBoxList

    项目开发过程中,时常用到多选框动态加载问题,比如说:

    系统设计往来单位(客商)管理功能一般都用到单位性质(施工单位、设备供货商、勘察单位、设计单位等等)是需要一个用户自定义的字段,需要用到多选框动态加载,总结用到的方法有以下两种:

    方法一:利用服务端控件

    在aspx页面添加
    <asp:CheckBoxList ID="cblist" runat="server" RepeatDirection="Horizontal" ></asp:CheckBoxList>
    在aspx.cs文件添加
    绑定初始化数据:
            cblist.DataSource = ds;
            cblist.DataTextField = "name";
            cblist.DataValueField = "id";
            cblist.DataBind();
    保存时
       string str="";
            foreach (ListItem li in cblsit.Items)
            {
                if(li.Selected)
                {
                    str += li.Value + ",";
                }
            }
    
            str = str.Remove(str.Length - 1);
    
    
    
    显示时
      string ifstr = "SHJD0001,SHJD0002,SHJD0003";
                foreach (ListItem li in cblsit.Items)
                {
                    if (ifstr.IndexOf(li.Value)!=-1)
                    {
                        li.Selected=true;//str += li.Value + ",";
                    }
                }

    方法二:利用html拼接

    在aspx页面添加
    <span id="zjlypropertielists" runat="server" style="text-align: left;  100%"></span>
    在aspx.cs文件添加
    绑定初始化数据:
    zjlypropertielists.InnerHtml = CheckboxList.ZjlyCheckboxlist("ZJLY");
    
      /// <summary>
            /// 加载资金来源列表
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            public string ZjlyCheckboxlist(string p)
            {
                string spanhistory = "";
                string strSQL = "";
                DataSet ds = new DataSet();
                //spanhistory += "<label> 资金来源:</label>";
                strSQL += "select h.id as code,h.name as name from  xt_zdk  h where h.codingClass='ZJLY'";
                ds = general.Select(strSQL);
                int count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    spanhistory += "<label><input type="checkbox"  value="" + ds.Tables[0].Rows[i]["code"].ToString() + ""  name="Zjlycb"  runat="server" />" + ds.Tables[0].Rows[i]["name"].ToString() + "</label>";
                }
                return spanhistory;
            }
    
    保存时
    
      string ZJLYcbvalue = HttpContext.Current.Request.Params["Zjlycb"];
    
    显示的时候:
    zjlypropertielists.InnerHtml = CheckboxList.ZjlyCheckboxlist(xmpsrw.zjly, "ZJLY");
    public string ZjlyCheckboxlist(string p, string p_2)
            {
                string spanhistory = "";
                string strSQL = "";
                DataSet ds = new DataSet();
                //spanhistory += "<label> 资金来源:</label>";
                strSQL += "select h.id as code,h.name as name from  xt_zdk  h where h.codingClass='ZJLY'";
                ds = general.Select(strSQL);
                int count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    if (p.IndexOf(ds.Tables[0].Rows[i]["code"].ToString())==-1)
                    {
                    
                    spanhistory += "<label><input type="checkbox"  value="" + ds.Tables[0].Rows[i]["code"].ToString() + ""  name="Zjlycb"  runat="server" />" + ds.Tables[0].Rows[i]["name"].ToString() + "</label>";
                    }
                    else
                    {
    
                        spanhistory += "<label><input type="checkbox"  value="" + ds.Tables[0].Rows[i]["code"].ToString() + ""  name="Zjlycb"  checked="checked"  runat="server" />" + ds.Tables[0].Rows[i]["name"].ToString() + "</label>";
                    
                    }
                }
                return spanhistory;
            }

     

  • 相关阅读:
    关于 Dev中的GridControl 中 GridView 的 PopulateColumns() 方法
    操作系统 页面置换算法LRU和FIFO
    C#中有哪些类型的数组
    博弈论:取石子问题
    java中 sleep 与 wait 的区别
    java 中 ArrayList LinkedList Vector 三者的异同点
    C# Mysql You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ????
    我在使用vs进行C#编程中常用的几个快捷键
    javascript 数据类型基础
    html5 <script>
  • 原文地址:https://www.cnblogs.com/zhutao1015/p/3272932.html
Copyright © 2011-2022 走看看