0

What I want is for the user to type say 3 into a textbox which will then create 3 buttons on the aspx page, is this possible on the webpage or is it just for windows apps? So depending on what the user puts in the textbox, i want to display that number i.e. if they type 4 in textbox then i want 4 buttons to appear My code is below:

Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
4
  • 1
    well your code is creating buttons not textboxes, but yes it is possible. Just create textboxes and add them to the page. Commented Apr 17, 2014 at 12:27
  • Oh sorry yeah doh! Im not sure how to add them to the page though Commented Apr 17, 2014 at 12:28
  • this.Controls.Add(btnSave); but that's not all; if you attach events :D ...you guessed right, more pain is coming ;) (depending on where you are adding that control to the page) Commented Apr 17, 2014 at 12:31
  • Do not forget to give different ID on each button. Commented Apr 17, 2014 at 12:31

3 Answers 3

2

Add these to your ASPX page

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Panel ID="buttonPanel" runat="server"></asp:Panel>

and then in your Button click event:

protected void Button1_Click(object sender, EventArgs e)
{
    int number = int.Parse(TextBox1.Text);
    for(int i =  0; i< number ;i++)
    {
        Button btn = new Button();
        btn.ID = "btnSave" + i;
        btn.Text = "Save " + i;
        buttonPanel.Controls.Add(btn);
    }
}

You can use int.TryParse to check for invalid inputs in the TextBox.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a detailed example on

Adding Controls to an ASP.NET form Dynamically

What they do in that example is to create a PlaceHolder control called DynamicControlsHolder where some controls are added when the user clicks on the AddControlButton

    <form id="form1" runat="server">
    <div>
    <asp:Button ID="AddControlButton" runat="server" Text="Add Control" onclick="AddControlButton_Click" />
    <br />
    <asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>
    <br />
    <br />
    <asp:Button ID="Submit" runat="server" Text="Submit Form" onclick="Submit_Click" />
    <br />
    ........
    </div>
    </form> 

And then in the code behind they react to the AddControlButton being clicked

 protected void AddControlButton_Click(object sender, EventArgs e)
{
    TextBox txt = new TextBox();
    DynamicControlsHolder.Controls.Add(txt);
    DynamicControlsHolder.Controls.Add(new LiteralControl("<br>"));
}

Comments

0

Try this way :

for(int i=1; i<= ButtonNos; i++)
{
 Button NewButton =new Button();
 NewButton.ID = "Newbutton"+i;
 NewButton.Width = 540;
 NewButton.Height = 60;
 NewButton.Text = "Button "+ i;

 this.Controls.Add(NewButton);
}

1 Comment

I think you wanted to write NewButton.Text = "Button "+ i;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.