简体   繁体   English

在ASP.NET Web表单中的复选框列表选项旁边动态添加文本框

[英]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. 我有两个问题。

  1. How can i identify projectID for each checbox as i can't pass value option for checkbox 我无法通过复选框的value选项来标识每个复选框的projectID
  2. How can i get the values for each checked checklistbox & related textbox so that i can loop through & save. 我如何获取每个选中的复选框和相关文本框的值,以便我可以遍历和保存。

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>

  1. Use the Checked attribute, not value . 使用Checked属性,而不是value
  2. Dynamic controls usually are added in the Page_Init event of a WebForms page, then you can access them anywhere you need. 动态控件通常添加在WebForms页面的Page_Init事件中,然后您可以在需要的任何位置访问它们。 Read about FindControl . 了解有关FindControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM