简体   繁体   English

文本框和标签数组如何在C#中的Submit方法中获取值

[英]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 .... 我曾经在下拉列表中选择索引更改方法以及如何在Submit方法中获取这些文本框值的过程中动态创建标签和文本框。

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 使用request.form

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

UPDATE 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. 关于您的控件在“提交”按钮单击时是否可见的问题之外,这是另一个问题,因为您正在下拉列表中的selectedindexchanged事件中创建文本框。 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. 在PageLoad(!IsPostBack块之外)和semDropDownList_SelectedIndexChanged事件中调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM