• 动态加载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;
            }

     

  • 相关阅读:
    BAT 大厂最流行的测试技术体系与测试职业发展晋级指南
    通知 | 2021 第一期《测试开发实战宝典》赠书活动顺利结束!
    你还缺个好工作?但7月毕业季即将来临怎么办
    一图看懂云栖大会「云原生」重磅发布
    阿里云重磅发布业务中台产品 BizWorks,中台发展进入下一个阶段
    云栖·追踪云原生|Serverless入围阿里云十大核心技术
    先行一步,7大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
    New homework
    German
    suggestion
  • 原文地址:https://www.cnblogs.com/zhutao1015/p/3272932.html
Copyright © 2020-2023  润新知