1
votes

i have used to create labels and textboxes dynamically in drop down list selected index changed method and how to get those textbox values in submit method ....


    public partial class StudentMarklistEntry : System.Web.UI.Page
    {
      private Label[] sublabels = new Label[7];
      private TextBox[] subtextbox = new TextBox[7];

     protected void semDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sem = int.Parse(semDropDownList.SelectedItem.Text);
            string dept = DeptDropDownList.SelectedItem.Text;
                    if (sem != null)
            {
                SqlDataReader subject = Mlist.GetSubjects(d_id,sem);
                int i = 0;
                while (subject.Read())
                {
                    sublabels[i] = new Label();
                    subtextbox[i] = new TextBox();
                    sublabels[i].Text = sub;
                    sublabels[i].ID = (subject["SUB_ID"]).ToString();
                    markz[i] = Convert.ToString(subject["SUB_ID"]);
                    subtextbox[i].ID = "subtextbox"+i.ToString();
                    labelPlaceHolder.Controls.Add(sublabels[i]);
                    labelPlaceHolder.Controls.Add(new LiteralControl(""));
                   Textboxholder.Controls.Add(subtextbox[i]);
                   Textboxholder.Controls.Add(new LiteralControl(""));
        i++;

                }

                subject.Close();
            }

    protected void SaveButton_Click(object sender, EventArgs e)
        {


    }

    }

1
You have to (re)create all controls which were created dynamically on every postback in Page_Load at the latest and with the same ID's as before.Tim Schmelter
Here's an example that can help you with resolve your issue: codeproject.com/Articles/3684/…Bayeni

1 Answers

2
votes

You can access the control values in two ways

Looping through placeholders controls

IList<string> selectedValues= new List<string>();
foreach (Control control in placeHolderText.Controls)
{
    if (control is TextBox)
    {
        var textBox = control as TextBox;
        selectedValues.Add(textBox.Text);
    }
}

Using request.form

var keys = Request.Form.AllKeys.Where(formKey => formKey.Contains("subtextbox"));
foreach (var formKey in keys)
{
    selectedValues.Add(Request.Form[formKey]);
}

UPDATE

Further to your question regarding the controls' visibility on submit button click, this is the problem becuase you are creating the textboxes in the dropdownlist selectedindexchanged event. In your button click event the placeholder will be empty as the controls are not created at all. As a workaround, you can try the following approach.

Create a function as below

private void CreateDynamicControls()
{
    int sem = int.Parse(semDropDownList.SelectedItem.Text);
    string dept = DeptDropDownList.SelectedItem.Text;
    if (sem != null)
    {
        SqlDataReader subject = Mlist.GetSubjects(d_id, sem);
        int i = 0;
        while (subject.Read())
        {
            sublabels[i] = new Label();
            subtextbox[i] = new TextBox();
            sublabels[i].Text = sub;
            sublabels[i].ID = (subject["SUB_ID"]).ToString();
            markz[i] = Convert.ToString(subject["SUB_ID"]);
            subtextbox[i].ID = "subtextbox" + i.ToString();
            labelPlaceHolder.Controls.Add(sublabels[i]);
            labelPlaceHolder.Controls.Add(new LiteralControl(""));
            Textboxholder.Controls.Add(subtextbox[i]);
            Textboxholder.Controls.Add(new LiteralControl(""));
            i++;

        }

        subject.Close();

    }
}

Call the function both in PageLoad(out side the !IsPostBack block) and semDropDownList_SelectedIndexChanged event.