文本框和标签数组如何在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)
        {


    }

    }


您可以通过两种方式访问​​控制值

通过占位符控制循环

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);
    }
}

使用request.form

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

UPDATE

关于控件在提交按钮单击时的可见性问题,还有一个问题,因为您正在下拉列表selectedindexchanged事件中创建文本框。 在按钮单击事件中,占位符将为空,因为控件根本不会创建。 作为解决方法,您可以尝试以下方法。

如下创建一个函数

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();

    }
}

在PageLoad(在!IsPostBack块的外侧)和semDropDownList_SelectedIndexChanged事件中调用函数。

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

上一篇: Array of textbox and labels how to get value in submit method in c#

下一篇: how can compress data