get values from dynamically added textboxes asp.net c#

后端 未结 3 1559
小鲜肉
小鲜肉 2020-12-09 06:36

as suggested in the title i have in which i can insert how many textboxes i want to add to a placeholder. i can add the textboxes just fine the problem is i cant get the val

3条回答
  •  星月不相逢
    2020-12-09 07:07

    You are actually creating textboxes with property Text set to default = ""; So you need set txt.Text property for example:

        public void txtExtra_TextChanged(object sender, EventArgs e)
        {
            for (int a = 1; a <= int.Parse(txtExtra.Text); a++)
            {
                TextBox txt = new TextBox();
                txt.ID = "txtquestion" + a;
                txt.Text = "Some text"; // Set some text here
                pholder.Controls.Add(txt);
    
            }
        }
    

    EDIT:

    After that you can store your values into the list:

    private static List values = new List();
    
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            foreach (Control ctr in pholder.Controls)
            {
                if (ctr is TextBox)
                {
                    string value = ((TextBox)ctr).Text;
                    values.Add(value); // add values here
                }
            }
        }
    

    EDIT: Here is your values: enter image description here

    EDIT: For super mega better understanding: Create one more textbox txtOutput then add button GetDataFromTextBoxesAndPutItBelow and create an event for that button `Click'. Event code:

        protected void btnGetData_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < values.Count; i++)
                txtOutput.Text += "Value from txtquestion1: " + values[i] + " ";
        }
    

    Screenshot looks: screen2

提交回复
热议问题