我是一个菜鸟,我一直在试图弄清楚我们如何asp:TextBox
在使用 c# 在 ASP.NET 中创建标签时为标签分配一个 ID。
例子:
我需要创建一个可能有多个文本框的线程。
当用户单击按钮时,必须生成一个带有ID
say的文本框txt01
。在第二次被点击时,ID
生成的文本框必须是txt02
等等..取决于点击次数。
提前致谢。
在您的 aspx 页面中获取和placeholder
:例如:<asp:PlaceHolder runat="server" ID="pholder" />
在后面的代码中:
TextBox txtMyText = new TextBox();
tb1.ID = YourDynamicId;
pholder.Controls.Add(txtMyText);
您可以在 ViewState 中保存当前 id 并获取相同的 id 并将递增的 id 分配给您的动态文本框。
我想这就是你要找的 -
你会注意到我已经使用了Page_Init
,因为如果你创建/添加控件Page_Load
thenFindControl
将返回 null in PostBack
。而且,您在动态添加的控件中输入的任何数据都不会在回发期间保留。
但是在加载或加载数据Page_Init
之前调用。因此,您不能使用或任何其他控件来保持控件计数。所以我习惯于保持计数。ViewState
PostBack
ViewState
Session
试试看,让我知道你的想法。
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" />
<asp:Button ID="btnRead" runat="server" Text="Read" OnClick="btnRead_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
protected int NumberOfControls
{
get { return Convert.ToInt32(Session["noCon"]); }
set { Session["noCon"] = value.ToString(); }
}
private void Page_Init(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//Initiate the counter of dynamically added controls
this.NumberOfControls = 0;
else
//Controls must be repeatedly be created on postback
this.createControls();
}
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void btnCreate_Click(object sender, EventArgs e)
{
TextBox tbx = new TextBox();
tbx.ID = "txtData"+NumberOfControls;
NumberOfControls++;
PlaceHolder1.Controls.Add(tbx);
}
protected void btnRead_Click(object sender, EventArgs e)
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = (TextBox)PlaceHolder1.FindControl("txtData" + i.ToString());
//Add the Controls to the container of your choice
Label1.Text += tx.Text + ",";
}
}
private void createControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "txtData" + i.ToString();
//Add the Controls to the container of your choice
PlaceHolder1.Controls.Add(tx);
}
}
将 id 存储在ViewState
类似这样的东西中 首先初始化一个与此类似的计数变量。在你class
写这个
protected int replyCount //declare the variable
{
get { return (int)ViewState["replyCount"]; }
set { ViewState["replyCount"] = value; }
}
如果不是回发,在您的页面 Load 中写入以初始化replyCount;
protected void Page_Load(object sender, EventArgs e)
{
if(!page.IsPostBack)
{
replyCount = 0; //initialise the variable
}
}
然后在创建动态文本框时
protected void Button_Click(Object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.id = "tb" + replycount; //use the variable
replycount++; // and after using increment it.
form.controls.add(tb); // assuming your form name is "form"
}
这就是你应该做的。
记住某个变量中的最后一个 ID,例如int lastID
,然后在创建新 TextBox 时在按钮的 onClick 方法中分配其ID="txt"+lastID;
.
您必须在页面回发期间保留 lastID,您可以将其存储在ViewState
.
尝试这个:
int i = 1;
if (ViewState["i"] == null)
{
ViewState["i"] = i;
}
else
i = (int)ViewState["i"];
PlaceHolder1.Controls.Clear();
for (int j = 1; j <= i; j++)
{
TextBox TextBox = new TextBox();
TextBox.ID = "TextBox" + j.ToString();
PlaceHolder1.Controls.Add(TextBox);
}
ViewState["i"] = i + 1;
将此添加到您的 .aspx 页面上
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
希望这有帮助。