如何在asp.net和c#中动态创建文本框。 [英] How to dynamically create textboxes in asp.net and c#.
问题描述
我已经在c#中动态创建了文本框。
TextBox MyTextBox = new TextBox();
MyTextBox.ID =tb ++ ViewState [num];
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(MyTextBox);
你已经在你提到的代码中添加了控件:
按钮button1 = new 按钮();
button1.Text = 动态按钮;
button1.Left = 10 ; button1.Top = 10 ; // 按钮的位置
此跨度> .Controls.Add(按钮1); // 这是您添加控件的方法
请参考;
代码:在运行时添加控件(Visual C#) [ ^ ]
动态地向ASP.NET表单添加控件 [ ^ ]
ASP.NET动态控件 [ ^ ]
<块引用> 我们如何在ASP.NET中动态创建TextBox并获取TextBox的文本: -
使用System;
使用System.Configuration;
使用System.Web.UI.WebControls;
使用System.Data.SqlClient;
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
int addedBooks = 2; //设置所需的动态文本框数量
ViewState [addedBooks] = addedBooks;
CreateDynamicTextBox();
}
else
{
//如果回发动态生成的控件是不可见的
//所以我们可以在每个帖子中绑定
CreateDynamicTextBox() ;
}
}
//创建动态文本框的方法
protected void CreateDynamicTextBox()
{
int addedBooks = Convert.ToInt16(ViewState [addedBooks ]);
for(int i = 0;我< addedBooks; i ++)
{
Literal lt1 = new Literal();
lt1.Text =书名:+(i + 1) +;
pnl1.Controls.Add(lt1); //pnl1是Panel的id
TextBox MyTextBox = new TextBox();
//分配文本框ID名称
MyTextBox.ID =tb+ i;
MyTextBox.Width = 100;
MyTextBox.Height = 15;
MyTextBox.Text = String.Empty;
pnl1.Controls.Add(MyTextBox);
Literal lt = new Literal();
lt.Text =
;
pnl1 .Controls.Add(lt);
}
}
//查找动态文本框的文本按钮单击
protected void btnSubmit_Click(object sender,EventArgs e)
{
int NoOfDynamicTextBoxes = Convert.T oInt32(ViewState [addedBooks]);
String [] books = new String [NoOfDynamicTextBoxes];
for(int i = 0;我< NoOfDynamicTextBoxes; i ++)
{
String txtId =tb+ i; //生成动态创建的TextBox的id
TextBox txtUserName = pnl1.FindControl (txtId)as TextBox;
if(txtUserName!= null)
{
books [i] = txtUserName.Text;
}
}
}
}
用于创建文本框的代码片段控制n次,
//声明变量的限制为
int n = 5;
for(int i = 0; i< n; i ++)
{
TextBox MyTextBox = new TextBox();
//分配文本框ID名称
MyTextBox.ID =tb++ ViewState [num] + i;
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(MyTextBox);
}
我希望这可以帮到你。
关于
RK
i have created textboxes dynamically in c#.
TextBox MyTextBox=new TextBox();
MyTextBox.ID = "tb" +""+ ViewState["num"];
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(MyTextBox);
You have already added control in code you mentioned:
Button button1=new Button(); button1.Text="dynamic button"; button1.Left=10; button1.Top=10; //the button's location this.Controls.Add(button1); //this is how you can add control
Please refer;
Code: Adding Controls at Run Time (Visual C#)[^]
Adding Controls to an ASP.NET form Dynamically[^]
ASP.NET Dynamic Controls[^]
How we Dynamically Create TextBoxes in ASP.NET and get the Text of that TextBoxes :-
using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int addedBooks = 2;// Set Required Number of Dynamic TextBoxes
ViewState["addedBooks"] = addedBooks;
CreateDynamicTextBox();
}
else
{
//if postback dynamic generated controls are invisible
//so we can bind in each post back
CreateDynamicTextBox();
}
}
//Method for Creating Dynamic TextBoxes
protected void CreateDynamicTextBox()
{
int addedBooks =Convert.ToInt16(ViewState["addedBooks"]);
for (int i = 0; i < addedBooks; i++)
{
Literal lt1 = new Literal();
lt1.Text = "Book Name : " + (i + 1) + " ";
pnl1.Controls.Add(lt1);// "pnl1" is a id of Panel
TextBox MyTextBox = new TextBox();
//Assigning the textbox ID name
MyTextBox.ID = "tb" + i;
MyTextBox.Width = 100;
MyTextBox.Height = 15;
MyTextBox.Text = String.Empty;
pnl1.Controls.Add(MyTextBox);
Literal lt = new Literal();
lt.Text = "
";
pnl1.Controls.Add(lt);
}
}
//Find Text of Dynamic TextBoxes on Button Click
protected void btnSubmit_Click(object sender, EventArgs e)
{
int NoOfDynamicTextBoxes = Convert.ToInt32(ViewState["addedBooks"]);
String[] books = new String[NoOfDynamicTextBoxes];
for (int i = 0; i < NoOfDynamicTextBoxes; i++)
{
String txtId = "tb" + i;// Produce id of Dynamically created TextBoxes
TextBox txtUserName = pnl1.FindControl(txtId) as TextBox;
if (txtUserName != null)
{
books[i] = txtUserName.Text;
}
}
}
}
Code snippet to create a textbox control for n number of times,
//Declaring the variable with limit int n=5; for (int i=0;i<n;i++) { TextBox MyTextBox=new TextBox(); //Assigning the textbox ID name MyTextBox.ID = "tb" +""+ ViewState["num"] + i; MyTextBox.Width = 540; MyTextBox.Height = 60; MyTextBox.TextMode = TextBoxMode.MultiLine; this.Controls.Add(MyTextBox); }
I hope this could help you.
With regards
R.K.
这篇关于如何在asp.net和c#中动态创建文本框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!