【问题标题】:how to program a button and textbox that is being rendered by textbox arrray and button array?如何对由文本框数组和按钮数组呈现的按钮和文本框进行编程?
【发布时间】:2015-10-19 20:43:29
【问题描述】:

我从一个数组中创建了几个标签、文本框和按钮。现在,我不确定如何对这些对象进行编程。例如,我想编写一个按钮,该按钮将启用文本框(来自数组)、捕获变量、操作变量等,就像表单中的任何其他对象一样。此外,我正在使用 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);


        }
    }

【问题讨论】:

  • 看起来您正在尝试在 ASP.NET 中进行编程,就像在 WinForms 中一样。我建议做一两个关于 WebForms 的教程,因为尽管它被设计为 WinForms 开发人员的简单过渡,但由于一般 Web 开发的性质,它本质上是非常不同的。这些教程可能会让您更好地理解这一点,并回答本文中的大部分问题

标签: c# asp.net arrays object controls


【解决方案1】:

添加你的点击功能:

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

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

    void btnDisplay_Click(Object sender, EventArgs e)
    {

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

    }

【讨论】:

  • 感谢您的回复。我添加了代码,但在“textBoxes [i]”上出现了红色下划线。错误提示“当前上下文中不存在名称'textBoxes'。此外,对于数组编号的'i',同样的错误。
  • 您需要声明您的 texboxarray,就像您在代码顶部使用按钮一样 Textbox [] textBoxes = new Texbox[The number of textboxes you want] i 是您在数组中的那个文本框的数量想要禁用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多