2017-03-08 62 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++;