如何编程由textbox arrray和按钮数组呈现的按钮和文本框?

时间:2015-10-19 20:43:29

标签: c# asp.net arrays object controls

我已从数组中创建了多个标签,文本框和按钮。现在,我不确定如何编程这些对象。例如,我想编写一个按钮,它将启用一个文本框(来自数组),捕获变量,操纵变量等,就像表单中的任何其他对象一样。此外,我正在使用C#.net后端代码在ASP.net上工作。如何双击该对象以生成以下代码块" private void btnDisplay_Click(object sender,EventArgs e)"如何引用文本框以捕获输入信息?

以下代码位于实际位于表单中的按钮内,并生成标签,文本框和按钮。

  Button [] buttons = new Button[2];

       for (int i = 0; i < buttons.Length ; i++)
        {
            buttons[i] = new Button();
            buttons[i].ID = "BTN0" + i;
            if (i == 0)
            {
                buttons[i].Text = "Send Web API Request";
            }
            if (i == 1)
            {
                buttons[i].Text = "Manually Input Information";
            }
        }

        for (int i = 0; i < buttons.Length ; i++)
        {
            pnlButton.Controls.Add(buttons[i]);
            Literal lit = new Literal();
            lit.Text = "</br></br>";
            pnlButton.Controls.Add(lit);
        }

        TextBox[] textBoxes = new TextBox[n];
        Label[] labels = new Label[n];


        for (int i = 0; i < n; i++)
        {
            labels[i] = new Label();
            labels[i].ID = "LBL0" + i;
            labels[i].Text = lines3[i];
            textBoxes[i] = new TextBox();
            textBoxes[i].ID = "TXT0" + i;

        }

  for (int i = 0; i < n; i++)
        {

            pnlQuestionsLBLS.Controls.Add(labels[i]);
            Literal lit = new Literal();
            lit.Text = "</br></br>";
            pnlQuestionsLBLS.Controls.Add(lit);


            pnlQuestionsTXTS.Controls.Add(textBoxes[i]);
            Literal lit2 = new Literal();
            lit2.Text = "</br></br>";
            pnlQuestionsTXTS.Controls.Add(lit2);


        }
    }

1 个答案:

答案 0 :(得分:0)

添加您的点击功能:

buttons[i].Click += new EventHandler(this.btnDisplay_Click);

捕获字符串中的输入并禁用文本框:

    void btnDisplay_Click(Object sender, EventArgs e)
    {

        string textboxInput = textBoxes[i].text;
        textBoxes[i].enabled = false;

    }