是否可以在aspx页面中以编程方式创建多个按钮?

时间:2022-12-15 22:40:02

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:

我想要的是让用户在一个文本框中键入说3,然后在aspx页面上创建3个按钮,这可能在网页上还是仅用于Windows应用程序?因此,根据用户放入文本框的内容,我想显示该数字,即如果他们在文本框中输入4,那么我想要4个按钮出现我的代码如下:

Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";

3 个解决方案

#1


2  

Add these to your ASPX page

将这些添加到您的ASPX页面

<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.

您可以使用int.TryParse检查TextBox中的无效输入。

#2


1  

Here is a detailed example on

这是一个详细的例子

Adding Controls to an ASP.NET form Dynamically

动态地将控件添加到ASP.NET表单

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

他们在这个例子中做的是创建一个名为DynamicControlsHolder的PlaceHolder控件,当用户点击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

然后在后面的代码中,他们对点击的AddControlButton作出反应

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

#3


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


2  

Add these to your ASPX page

将这些添加到您的ASPX页面

<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.

您可以使用int.TryParse检查TextBox中的无效输入。

#2


1  

Here is a detailed example on

这是一个详细的例子

Adding Controls to an ASP.NET form Dynamically

动态地将控件添加到ASP.NET表单

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

他们在这个例子中做的是创建一个名为DynamicControlsHolder的PlaceHolder控件,当用户点击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

然后在后面的代码中,他们对点击的AddControlButton作出反应

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

#3


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