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

我曾经在下拉列表中选择索引改变的方法动态地创建标签和文本框,以及如何获取提交方法中的文本框值....


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


    }

    }


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.

链接地址: http://www.djcxy.com/p/7612.html

上一篇: DNN 7中的自定义错误处理?

下一篇: 文本框和标签数组如何在c#中提交方法中获取值