[英]Dynamically add textbox next to checkbox list option in asp.net webform
我需要填充數據庫中的復選框列表,並在每個復選框旁邊添加文本框,以便用戶可以在文本框中輸入數值。
我還需要將選中的復選框值保存在其他表中
Table Project {Projectid, Name, Details, cDate}
Table ContributionForProject{id, Name, Amount, Projectid, cDate,GUID}
Table TotalContribusion{id, TotalAmount, Projectid, cDate, GUID}
我正在使用下面的代碼來填充清單框,並在每個選項旁邊顯示文本框。
為了將值保存在數據庫中,我還需要從文本框中獲取相應的值。
我有兩個問題。
value
選項來標識每個復選框的projectID 以下是我正在使用的代碼
public void CreateChecklistWithOption()
{
var MyList = new List<ListItem>
{
new ListItem("Project One", "1"),
new ListItem("Project Two", "2"),
new ListItem("Project Three", "3"),
new ListItem("Project Four", "4")
};
Table myTable = new Table();
foreach (var item in MyList)
{
//Create new checkbox
CheckBox CB = new CheckBox();
CB.Text = item.Text;
CB.ID = "CB_"+item.Value;
//Create tablr row and td, then adds them accordignly
TableRow TR = new TableRow();
TableCell TD = new TableCell();
TD.Controls.Add(CB);
TR.Controls.Add(TD);
//IF <YOUR FLAG GOES HERE>-->
//if (item.Value == "2" || item.Value == "1" || item.Value == "3")
//{
//Create your input element and place it in a new Table cell (TD2)
TextBox TB = new TextBox();
TB.ID = string.Format("tb_{0}", item.Value);
TableCell TD2 = new TableCell();
TD2.Controls.Add(TB);
TR.Controls.Add(TD2);
//}
myTable.Controls.Add(TR);
}
phforCheckList.Controls.Add(myTable);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br> Ckeck Box One " );
Response.Write("<br> Ckeck Box Two ");
Response.Write("<br> Ckeck Box Three ");
}
<asp:PlaceHolder ID="fillMe" runat="server"></asp:PlaceHolder>
Checked
屬性,而不是value
。 Page_Init
事件中,然后您可以在需要的任何位置訪問它們。 了解有關FindControl
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.