2017-03-08 66 views
0

我有我的复选框asp.net - 算多少复选框被选中,那么存储变量

<input type="checkbox" name="checkSerial" value="serial">Serial 
    <input type="checkbox" name="checkProperty" value="property">Property 
    <input type="checkbox" name="checkAccountable" value="accountable">Accountable 
    <input type="checkbox" name="checkStatus" value="status">Status 

和IM将其发送到session可变

Session["checkSerial"] = Request.Form["checkSerial"]; 
    Session["checkProperty"] = Request.Form["checkProperty"]; 
    Session["checkAccountable"] = Request.Form["checkAccountable"]; 
    Session["checkSeries"] = Request.Form["checkSeries"]; 

但IM问的是我怎么能得到选中的复选框的数量,或者计算我已经放入的Session。因为我在iTextSharp中执行此操作,并且选中将取决于这样的列。

if (theCountVariable == 7){ 
PdfPTable table = new PdfPTable(7); 
} 

我不喜欢JS的所以是一个C#解决方案,我会非常高兴。

回答

1

如果未勾选复选框,则Request.Form["checkboxname"]将为空。

int theCountVariable = 0; 
if (Request.Form["checkSerial"] != null) theCountVariable++; 
if (Request.Form["checkProperty"] != null) theCountVariable++; 
if (Request.Form["checkAccountable"] != null) theCountVariable++; 
if (Request.Form["checkSeries"] != null) theCountVariable++;