ASP.NET-动态创建控件 [英] ASP.NET - Creating controls dynamically
本文介绍了ASP.NET-动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,
当我单击链接按钮时,我想在一个占位符内生成一个动态控件(3个文本框和2个下拉列表).我实现了它,但是我特别想要的是,当我单击链接按钮时,它应该连续创建那些控件.我的代码只生成了一次.
我在下面粘贴了我的代码.
Hi Friends,
I want to generate a dynamic controls(3 textboxes and 2 dropdownlist) inside a place holder when i click a link button. I acheived it but what I specifically wanted is when ever I click a link button it should created those controls in a row. My code generated only one time.
I have pasted my code below.
protected void AddSkills_Click(object sender, EventArgs e)
{
TextBox txtSkill = new TextBox();
TextBox txtVersion = new TextBox();
TextBox txtLastUsed = new TextBox();
DropDownList ddlExpYears = new DropDownList();
DropDownList ddlExpMonths = new DropDownList();
SkillPlaceHolder.Controls.Add(txtSkill);
SkillPlaceHolder.Controls.Add(txtVersion);
SkillPlaceHolder.Controls.Add(txtLastUsed);
SkillPlaceHolder.Controls.Add(ddlExpYears);
SkillPlaceHolder.Controls.Add(ddlExpMonths);
}
请告诉我每次单击链接时如何更改此代码并连续生成那些控件.希望你理解我的问题.
亲切的问候
Azeem
Kindly tell me how can i change this code and generate those controls in a row each time I click on a link. Hope you understand my question.
Kind regards
Azeem
推荐答案
选中此选项,对我来说效果很好
check this one , it work fine with me
int countTimes = 0;
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (ViewState["countTimes"] == null)
{
countTimes = 1;
}
else
{
countTimes = Convert.ToInt32(ViewState["countTimes"]);
}
for (int i = 0; i < countTimes; i++)
{
TextBox txtSkill = new TextBox();
txtSkill.ID = "txtSkill"+i;
TextBox txtVersion = new TextBox();
txtVersion.ID = "txtVersion" + i;
TextBox txtLastUsed = new TextBox();
txtLastUsed.ID = "txtLastUsed"+i;
DropDownList ddlExpYears = new DropDownList();
ddlExpYears.ID = "ddlExpYears" + i;
DropDownList ddlExpMonths = new DropDownList();
ddlExpMonths.ID = "ddlExpMonths"+i;
Form.Controls.Add(txtSkill);
Form.Controls.Add(txtVersion);
Form.Controls.Add(txtLastUsed);
Form.Controls.Add(ddlExpYears);
Form.Controls.Add(ddlExpMonths);
Form.Controls.Add(txtSkill);
}
countTimes = countTimes + 1;
ViewState.Add("countTimes", countTimes);
}
Hello Mohamed,
我想您不明白我的问题,在表单加载后单击链接按钮时,我上面发布的代码仅一次添加了控件,但是我希望每次单击链接按钮时都创建一个控件一次.您的代码无法解决我的问题.
亲切的问候
Azeem
Hello Mohamed,
I think you didn''t understand my question, the code I posted above adding the controls only one time when we click on a link button after form load, but I want this to be created each time I click on a link button not only once. Your code doesn''t solve my issue.
Kind regards
Azeem
您错过了一些有用的细节.当您说连续"时,是指GridView行或HTML表格行,还是其他.
对于GridView,您可以将一个项目添加到它绑定到的数据源并重新绑定它,这将自动创建控件.与Repeater或DataList相同的原因.
使用中继器,您可以向页脚添加一组控件,然后在必要时使用JavaScript克隆它们.
You have missed some useful details. When you say "in a row" do you mean in a GridView row or HTML table row, or something else.
In the case of a GridView you could just add an item to the datasource it is bound to and rebind it, this will automatically create the controls. Same why with a Repeater or DataList.
Using a Repeater you could add a set of controls to the footer then clone them using JavaScript when necessary.
这篇关于ASP.NET-动态创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文