c#根据后台数据,自动生成checkbox

时间:2022-10-01 14:30:14

前端在aspx中,添加生成checkbox的容器div:

<div id="container" runat="server"></div>

后台cs代码如下:

 1 private DbHelper m_oDbHelper = null;//dll,数据访问类
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 if (!IsPostBack)
5 {
6
7 int i = 0;
8 try
9 {
10 if (this.m_oDbHelper == null)
11 {
12 this.m_oDbHelper = new DbHelper();
13 }
14 this.m_oDbHelper.OpenDbResource("");//打开数据库
15
16 DbCommand oCmd = m_oDbHelper.CreateDbCommand();
17 oCmd.CommandType = CommandType.Text;//指定sql类型是存储过程还是文本
18 oCmd.CommandText = string.Format("select AreaId,AreaName from TB_PropArea where AreaId = ParentAreaId order by AreaId,AreaName");//从数据库中选择出checkbox的id和checkbox显示的名称
19 oCmd.Parameters.Clear();
20
21 IDataReader reader = oCmd.ExecuteReader();
22
23 while (reader.Read())
24 {
25 CheckBox cbk = new CheckBox();
26 string sza = reader.GetValue(1).ToString();
27
28 cbk.ID = reader.GetValue(0).ToString();
29 cbk.Text = i + 1 + ". " + sza;
30 this.container.Controls.Add(cbk);
31 LogCentral.CurrentLogger.LogInfo("cbk" + (i + 1) + " " + cbk.ID + cbk.Text);//log文件,可以忽略
32
33 i++;
34 }
35
36 }
37 catch(Exception ex)
38 {
39 LogCentral.CurrentLogger.LogInfo(ex.ToString ());
40 }
41 finally
42 {
43 m_oDbHelper.CloseDbResource();
44 }
45 }
46 }

生成后画面如下:c#根据后台数据,自动生成checkbox

html显示如下:

<div id="container"><input id="Area000015" type="checkbox" name=Area000015" /><label for="Area000015">1. 色差值</label>
<input id="Area000016" type="checkbox" name=Area000016" /><label for="Area000016">2. 平方米克重</label>
<input id="Area000017" type="checkbox" name=Area000017" /><label for="Area000017">3. 厚度</label>
<input id="Area000018" type="checkbox" name=Area000018" /><label for="Area000018">4. 拉力强度</label>
<input id="Area000019" type="checkbox" name=Area000019" /><label for="Area000019">5. 伸长率</label>
<input id="Area000020" type="checkbox" name=Area000020" /><label for="Area000020">6. 静负荷伸长率</label>
<input id="Area000021" type="checkbox" name=Area000021" /><label for="Area000021">7. 残余伸长率</label>
<input id="Area000022" type="checkbox" name=Area000022" /><label for="Area000022">8. 撕裂强度</label>
<input id="Area000023" type="checkbox" name=Area000023" /><label for="Area000023">9. 剥离强度</label>
<input id="Area000024" type="checkbox" name=Area000024" /><label for="Area000024">10. 层间剥离强度</label>
<input id="Area000025" type="checkbox" name=Area000025" /><label for="Area000025">11. 缝口拉断强度</label>
<input id="Area000026" type="checkbox" name=Area000026" /><label for="Area000026">12. 气味试验(80℃*2hrs)</label>
<input id="Area000027" type="checkbox" name=Area000027" /><label for="Area000027">13. 阻燃性</label><input id="Area000028" type="checkbox" name=Area000028" /><label for="Area000028">14. 磨擦色落</label><input id="Area000029" type="checkbox" name=Area000029" /><label for="Area000029">15. 耐热性</label><input id="Area000030" type="checkbox" name=Area000030" /><label for="Area000030">16. 可冷凝成份</label><input id="Area000031" type="checkbox" name=Area000031" /><label for="Area000031">17. TVOC总碳量散发</label><input id="Area000032" type="checkbox" name=Area000032" /><label for="Area000032">18. 耐光照牢度</label><input id="Area000033" type="checkbox" name=Area000033" /><label for="Area000033">19. A4纸擦拭</label><input id="Area000034" type="checkbox" name=Area000034" /><label for="Area000034">20. 蒸汽熨烫</label><input id="Area000036" type="checkbox" name=Area000036" /><label for="Area000036">21. 测试</label><input id="Area000061" type="checkbox" name=Area000061" /><label for="Area000061">22. CC</label><input id="Area000068" type="checkbox" name=Area000068" /><label for="Area000068">23. DD</label><input id="Area000069" type="checkbox" name=Area000069" /><label for="Area000069">24. QQ</label><input id="Area000070" type="checkbox" name=Area000070" /><label for="Area000070">25. EE</label></div>