[英]Dynamically add textbox next to checkbox list option in asp.net webform
I need to populate checkbox list from database and next to each checkbox i need to add textbox so that user can numeric value in the text box. 我需要填充数据库中的复选框列表,并在每个复选框旁边添加文本框,以便用户可以在文本框中输入数值。
I also need to save checked checkbox value in a different table 我还需要将选中的复选框值保存在其他表中
Table Project {Projectid, Name, Details, cDate}
Table ContributionForProject{id, Name, Amount, Projectid, cDate,GUID}
Table TotalContribusion{id, TotalAmount, Projectid, cDate, GUID}
I am using below code to fill the checkedlist box and showing textbox next to each option. 我正在使用下面的代码来填充清单框,并在每个选项旁边显示文本框。
in order to save value in the database i need to get the corresponding values from textbox also. 为了将值保存在数据库中,我还需要从文本框中获取相应的值。
I have two issues with this. 我有两个问题。
value
option for checkbox value
选项来标识每个复选框的projectID Below is the code which i am working with 以下是我正在使用的代码
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
attribute, not value
. Checked
属性,而不是value
。 Page_Init
event of a WebForms page, then you can access them anywhere you need. Page_Init
事件中,然后您可以在需要的任何位置访问它们。 Read about FindControl
. FindControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.