简体   繁体   中英

how to program a button and textbox that is being rendered by textbox arrray and button array?

I have created several labels,textboxes, and buttons from an array. Now, I am not sure how to program those objects. For example, I want to program a button that will enable a text-box (from the array), capture variables, manipulate the variables, etc just like any other object that is in the form. Furthermore, I am working on ASP.net with C#.net back-end code. how do I double click on the object to generate the following block of code "private void btnDisplay_Click(object sender, EventArgs e)" and how do I make reference to a textbox as well to capture the input information?

The following code is within a button that is actually in the form and generates labels, textbox, and buttons.

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


        }
    }

Add your clickfunction:

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

Captures the input in a string and disables your textbox:

    void btnDisplay_Click(Object sender, EventArgs e)
    {

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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