我有一個頁面,我創建2個複選框動態。獲取按鈕上的動態複選框值點擊
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
Tc.Attributes["style"] = "line-height: 30px; text-align: left";
Tc.Attributes["width"] = "50%";
Tc.Style.Add("padding-left", "5px");
//Checkboxes on left along with labels
CheckBox checkBoxCtrl = new CheckBox();
checkBoxCtrl.ID = "checkBoxCtrl" + i;
Tc.Controls.Add(checkBoxCtrl);
tr.Cells.Add(Tc);
}
一旦它們在頁面加載事件創建我有一個要求,以檢查是否複選框被選中或不Ok_button click事件。
protected void Update2_Click(object sender, EventArgs e)
{
if(checkBoxCtrl.checked)
//here i wont be able to get the value
// i get the error the name checkBoxCtrl does not exist..
{
response.write("true");
}
}
但我該如何做這種情況下的檢查。
感謝
答:
這是需要做獲得的複選框什麼樣的價值觀
protected void Update1_Click(object sender, EventArgs e)
{
for(int i = 0; i < ControlPropList.Count; i++)
{
CheckBox chkTest = (CheckBox)xxx.FindControl("checkBoxCtrl" + i);
{
if (chkTest.Checked)
{
Global.logger.Info("Checkbox True = " + chkTest.ID);
}
else
{
Global.logger.Info("Checkbox False = " + chkTest.ID);
}
}
}
}
您需要將該複選框保存在該用戶的Session中,並在每個頁面加載時添加複選框,並確保您保留對它們的代碼隱藏引用。它也可以用JavaScript來完成,所以它在客戶端上執行,如果這就是你想要的。 – MrFox