动态创建文本框 [英] Dynamically create textboxes

查看:88
本文介绍了动态创建文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

对于每个按钮单击我想要动态创建文本框和相应的标签。请参阅下面的代码。



Hi all,
For each button click i want to create text boxes and there corresponding labels dynamically.Please see below code.

    <table id="tblPropertyCustom"  runat="server" enableviewstate="true"></table>



    private const string FIELDCOUNT = "fieldcount";
private int FieldCount {
    get { return (int)(ViewState[FIELDCOUNT] ?? Select_All); }
    set { ViewState[FIELDCOUNT] = value; }
}
     protected void btnAddField_click( Object sender, EventArgs e ) {

    CheckBox chkNewField = new CheckBox();
    chkNewField.ID      = "chkNewField"+ FieldCount.ToString();
    chkNewField.Checked = true;

    Label LblNewLabel   = new Label();
    LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
    LblNewLabel.Text    = "New Lable";

    TextBox TxtNewLabel = new TextBox();
    TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

    Label LblNewValue   = new Label();
    LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
    LblNewValue.Text = "New Value";

    TextBox TxtNewValue = new TextBox();
    TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

    HtmlTableRow  tRow   = new HtmlTableRow ();

    HtmlTableCell tCell1 = new HtmlTableCell("th");
    HtmlTableCell tCell2 = new HtmlTableCell("th");
    tCell2.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell3 = new HtmlTableCell("th");
    tCell3.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell4 = new HtmlTableCell();
    HtmlTableCell tCell5 = new HtmlTableCell("th");
    tCell5.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell6 = new HtmlTableCell("th");
    tCell6.Attributes.Add( "class", "medium" );

    tCell1.Controls.Add(chkNewField);
    tCell2.Controls.Add(LblNewLabel);
    tCell3.Controls.Add(TxtNewLabel);
    tCell4.Controls.Add( new LiteralControl( "" ) );
    tCell5.Controls.Add( LblNewValue );
    tCell6.Controls.Add( TxtNewValue );

    tRow.Cells.Add(tCell1);
    tRow.Cells.Add(tCell2);
    tRow.Cells.Add(tCell3);
    tRow.Cells.Add(tCell4);
    tRow.Cells.Add(tCell5);
    tRow.Cells.Add(tCell6);

    tblPropertyCustom.Rows.Add(tRow );
    FieldCount++;
}





它只显示一行标签和字段。在下一次单击时,行值将替换为下一个计数的标签和字段。请帮助我(我只能看到一行)。



提前感谢:)

Manu v Nath



Its showing only one row of labels and fields. On next click the row values will replace by next counted labels and fields.Please help me(I can see only 1 row).

Thanks in advance :)
Manu v Nath

推荐答案

Hello Manu,



你面临的问题是因为在回发期间对动态添加的控件的Object引用丢失,因为它们在代码隐藏中没有句柄。下面提到的CP上面提到的文章将帮助您正确实现这一点。



Hello Manu,

The problem your are facing is due to the fact that Object references to dynamically added controls are lost during a postback because they have no handle in the codebehind. The below mentioned articles one available right here on CP will help you implement this correctly.


  • Retaining State for Dynamically Created Controls in ASP.NET applications[^]
  • http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx[^]


它只显示一行标签和字段。在下一次单击时,行值将替换为下一个计数的标签和字段。请帮助我(我只能看到一行)。



tblPropertyCustom是一个服务器端表控件,对吗?它不保留以前的内容,这意味着视图状态未启用它或您正在某处清除它。你也可以告诉我下次你点击按钮时FieldCount的价值吗?它是否显示更新值或与上一个相同?



此外,您确定要在服务器端执行此操作吗?因为我不需要。您在这里所做的一切都可以在客户端完成,也可以使用JavaScript完成。这个链接应该有帮助 - 动态添加文本框javascript [ ^ ]。


使用
<asp:placeholder id="placeHolderTable" runat="server"></asp:placeholder>



点击事件添加按钮使用以下代码:

in your aspx page.
And on click event of the add button use the following code:

int FieldCount = 0;
if (ViewState["FieldCount"] != null)
{
    FieldCount = (int)ViewState["FieldCount"];
}

Table tbl = new Table();
if (Session["DynamicTable"] != null)
{
    tbl = (Table)Session["DynamicTable"];
}

CheckBox chkNewField = new CheckBox();
chkNewField.ID = "chkNewField" + FieldCount.ToString();
chkNewField.Checked = true;

Label LblNewLabel = new Label();
LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
LblNewLabel.Text = "New Lable";

TextBox TxtNewLabel = new TextBox();
TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

Label LblNewValue = new Label();
LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
LblNewValue.Text = "New Value";

TextBox TxtNewValue = new TextBox();
TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

TableRow tRow = new TableRow();

TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
tCell2.Attributes.Add("class", "medium");
TableCell tCell3 = new TableCell();
tCell3.Attributes.Add("class", "medium");
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
tCell5.Attributes.Add("class", "medium");
TableCell tCell6 = new TableCell();
tCell6.Attributes.Add("class", "medium");

tCell1.Controls.Add(chkNewField);
tCell2.Controls.Add(LblNewLabel);
tCell3.Controls.Add(TxtNewLabel);
tCell4.Controls.Add(new LiteralControl(""));
tCell5.Controls.Add(LblNewValue);
tCell6.Controls.Add(TxtNewValue);

tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);

tbl.Rows.Add(tRow);
placeHolderTable.Controls.Remove(tbl);
placeHolderTable.Controls.Add(tbl);
Session["DynamicTable"] = tbl;
FieldCount++;
ViewState["FieldCount"] = FieldCount;


这篇关于动态创建文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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