单击按钮后如何获得复选框值

时间:2016-03-10 12:35:37

标签: c# asp.net

复选框是动态创建的。

protected void Page_Load(object sender, EventArgs e)
{
    XDocument doc = XDocument.Load(@"C:\Users\Faraz\Documents\Visual Studio 2015\Projects\Assignment_3_i130316\Assignment_3_i130316\bin\Products.xml");
    var goals = doc.Element("FTSRecord").Elements("Approval_PickDrop");
    var array = goals.Select(x => x.Value).ToArray();

    int rowCnt = 0;
    int rowCtr;
    foreach (var b in array)
    {
        rowCnt++;
    }

    for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
    {
        TableRow tRow = new TableRow();
        Table1.Rows.Add(tRow);
        for (int cellCtr = 1; cellCtr <= 2; cellCtr++)
        {
            // Create a new cell and add it to the row.
            TableCell tCell = new TableCell();
            tRow.Cells.Add(tCell);
            if (cellCtr == 1)
            {
                tCell.Text = array[rowCtr - 1];
                tRow.Cells.Add(tCell);
            }
            else
            {
              CheckBox c = new CheckBox();
              c.ID = "ID" + cellCtr;
              tCell.Controls.Add(c);
            }
        }                
    }
}

2 个答案:

答案 0 :(得分:0)

将Request.Form与复选框的名称一起使用

https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx

答案 1 :(得分:0)

更改复选框单元格ID :: c.ID =&#34; ID _&#34; + rowCtr;

单击按钮后,您将获得Table1并迭代每一行并获取每行的复选框单元格值,其ID为 c.ID =&#34; ID _&#34; + rowCtr; 使用&#39; _&#39;拆分ID得到它的数组索引位置,复选框检查状态告诉它的值。