动态加载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; width: 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;

        }

 

 

 

你可能感兴趣的:(checkbox)