Rocky
Rocky

Reputation: 27

Array of textbox and labels how to get value in submit method in c#

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)
        {


    }

    }

Upvotes: 2

Views: 2446

Answers (1)

Kiran Hegde
Kiran Hegde

Reputation: 3681

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.

Upvotes: 2

Related Questions