丢失动态创建的文本框值 [英] lost dynamically created text box values

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

问题描述

大家好,

请看下面的代码:



Hi all,
Please see below code:

        protected void btnAddField_click( Object sender, EventArgs e ) {
        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;
}





动态添加字段工作正常。但是



1.我在每个帖子上清空的文本框中输入的值

2.我无法从文本框中获取值。



protected void BtnPublish_click(object sender,EventArgs e){TextBox tb =(TextBox)placeHolderTable .FindControl(TxtNewLabel1); }



此代码无效。

请帮帮我。

提前致谢,

Manu



Dynamically adding fields are working fine. But

1. the values i entered in text boxes clearing on each post back
2. I failed to fetch values from text boxes.

protected void BtnPublish_click( object sender, EventArgs e ) { TextBox tb = (TextBox)placeHolderTable .FindControl( "TxtNewLabel1" ); }

This code not working.
Please help me.
Thanks in advance,
Manu

推荐答案

你将在回发时松开所有动态控件。



你仍然可以将这些控件的值存储在一个会话中,然后当页面刷新时,重新创建这些控件并将值放入其中。

很丑,但是应该可以工作。
You will loose all dynamic controls on postback.

You could still store the values of these controls in a session, and then when the page refreshes, recreate these controls and put the values in them.
Ugly, but should work.


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

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