如何点击(按钮)后,动态创建文本框? asp.net [英] how to create textbox dynamically after click (button)? asp.net

查看:95
本文介绍了如何点击(按钮)后,动态创建文本框? asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何创建一个文本框(或其他控件)动态,点击按钮后。我已经写了,如果我把它添加到的Page_Load 方法,它工作的功能,但它不工作作为关系到一个特定的按钮(点击后)。

另外,我试图了解如何计算点击次数。

的.aspx 标记:

 < ASP:按钮的ID =addexProductBTN=服务器文本=增加的onclick =addexProductBTN_Click/>
< ASP:占位符ID =DynamicControlsHolder=服务器>< / ASP:占位符>

.aspx.cs code:

 诠释计数= 0;
保护无效addnewProductBTN_Click(对象发件人,EventArgs的发送)
{
    算上++;
    addControlTB();
}保护无效addControlTB()
{
    表tbldynamic =新表();
    TableCell的TC =新的TableCell();
    TR的TableRow =新的TableRow();
    文本框NewProdTB =新的TextBox();
    NewProdTB.ID =DynamicTextBox;
    NewProdTB.Text =;
    tc.Controls.Add(NewProdTB);
    tr.Cells.Add(TC);
    tbldynamic.Rows.Add(TR);
    DynamicControlsHolder.Controls.Add(tbldynamic);
}


解决方案

的第一件事就是与视图状态做:

 文本框NewProdTB =新的TextBox();
NewProdTB.EnableViewState = FALSE;

试着玩如何行为的变化,当设置为false和true;

这儿,这是关于ViewState的行为的文章。

你的问题的伯爵的一部分:如果您需要一种能承受住重新加载页面的一个全局变量,你应该尝试使用的会话对象

I tried to figure out how to create a textbox (or other controls) dynamically, after click on button. I have written a function that does work if I add it to the Page_Load method, but it does not work as related to a specific button (after click).

Also, I tried to understand how to count my clicks.

.aspx markup:

<asp:Button ID="addexProductBTN" runat="server" Text="add" onclick="addexProductBTN_Click"/>
<asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>

.aspx.cs code:

int count = 0;
protected void addnewProductBTN_Click(object sender, EventArgs e)
{
    count++;
    addControlTB();
}

protected void addControlTB()
{
    Table tbldynamic = new Table();
    TableCell tc = new TableCell();
    TableRow tr = new TableRow();
    TextBox NewProdTB = new TextBox();
    NewProdTB.ID = "DynamicTextBox";
    NewProdTB.Text = "";
    tc.Controls.Add(NewProdTB);
    tr.Cells.Add(tc);
    tbldynamic.Rows.Add(tr);
    DynamicControlsHolder.Controls.Add(tbldynamic);
}

解决方案

the first thing has to do with viewstate:

TextBox NewProdTB = new TextBox();
NewProdTB.EnableViewState = false;

Try to play around how the behaviour changes, when set to false and true;

THIS here is an article about the viewstate behaviour.

The "count" part of your question: if you need a global variable that survives reload of the page, you should try to use session object.

这篇关于如何点击(按钮)后,动态创建文本框? asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆