我开始 .net 并开始我将动态创建多个文本框。
我写了这个:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Page sans titre</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="OnclickButton" />
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
</form>
</body>
</html>
和这个
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnclickButton(object sender, EventArgs ea)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
Button btnSomeButton = sender as Button;
btnSomeButton.Text = "I was clicked!" + randomNumber;
TextBox txt = new TextBox();
txt.ID = "txt_" + randomNumber;
form1.Controls.Add(txt);
}
}
}
我不明白为什么当我在 Button1 上单击 2 次时,只出现 1 个文本框。
为什么会有这种行为?做我想做的事的好方法是什么?先感谢您