[英]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.