从使用C#动态创建的文本框中检索值

时间:2013-03-28 06:45:18

标签: c# javascript asp.net c#-4.0 textbox

我在流程中编码时动态创建了少量textbox,我为每个流程提供了唯一的ID,并且我使用C#对所有文本框进行了硬编码。


现在点击按钮我试图从我使用下面代码的文本框中检索值,但是它会抛出异常OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT

请看下面的代码,我已经尝试了两件事,但仍然没有得到。
请帮帮我 谢谢

protected void btnPltGrap_onclick(object sender, EventArgs e)
{
    //spny is my stack panel and txtX0 is my of the text box id
    //Below is the 1st Try        
     TextBox tb = new TextBox(); 
     tb= (TextBox)Master.FindControl("spnY").FindControl("txtX0");
     string strx = tb.Text;
      //Below is the 2nd Try 
     string strx = (spnY.FindControl("txtX0") as TextBox).Text;
 }

由于

Am trying to use view state as per you told that i shlould recreate the controls ones again but am getting exception as Invalid Arguments. please go have a look.

protected void btnSet_onClick(object sender, EventArgs e)
 {
    Table tblMainY = new Table();
    TableRow tblRow = new TableRow();
    tblMainY.Controls.Add(tblRow);
    TableCell tblCel = new TableCell();
    TextBox txtdyn = new TextBox();
    txtdyn.Text = "1";
    txtdyn.ID = "txtY01"; 
    txtdyn.Width = 50;
    tblCel.Controls.Add(txtdyn);
    tblRow.Controls.Add(tblCel);    
    splY.Controls.Add(tblMainY);
    ViewState["temptbl"] = tblMainY
}
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
    splY.Controls.Add(ViewState["Temptbl"]);
}
Please help me out

4 个答案:

答案 0 :(得分:1)

我过去遇到过同样的问题。

我所做的是为动态添加的控件提供一个ID,并确保它在回发时也保留了该ID。

一旦回发的控件具有与之前相同的ID,Microsoft就会做出魔术,并使用回发前的值重新填充控件。

一旦读出此代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            this.NumberOfControls = 0; //very first time when page is loaded, value will be 0
        else
            this.createControls(); //if it is postback it will recreate the controls according to number of control has been created
    }

    //this is the base of this, it will hold the number of controls has been created, called properties
    protected int NumberOfControls 
    {
        get { return (int)ViewState["NumControls"]; }
        set { ViewState["NumControls"] = value; }
    }

    //it will create the controls 
    protected void createControls()
    {
        int count = this.NumberOfControls;
        for (int i = 0; i < count; i++) //loop for the total number of control.
        {
            TextBox tx = new TextBox(); //creating new control
            tx.ID = "ControlID_" + i.ToString(); //in your solution you are giving static id, don't do that, assign id number dynamically, it will help you further, if you want to manipulate the controls for some other use
            //Add the Controls to the container of your choice
            form1.Controls.Add(tx);
        }
    }

    //add new control
    protected void addSomeControl()
    {
        TextBox tx = new TextBox();
        tx.ID = "ControlID_" + NumberOfControls.ToString();
        form1.Controls.Add(tx);
        this.NumberOfControls++; //increment the number of control
    }

    protected void AddBtn_Click(object sender, EventArgs e)
    {
        addSomeControl(); 
    }

答案 1 :(得分:0)

您必须在init上重新创建控件才能获得它的价值。
这里有一些链接
Get text from dynamically created textbox in asp.net

编辑1

TextBox tb=(TextBox)ViewState["Temptbl"];
splY.Controls.Add(tb);

答案 2 :(得分:0)

bellow链接中的一些示例代码作为我的博客

它解释了如何使用panel控件在dynamicaly中使用值和验证来放置和获取textboxe。

我们去看看这个网址吧。你可以得到很好的解决方案

get and Create dynamic Textbox and dropdownlist with Validation

中获取文本框值的简单行
TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;

答案 3 :(得分:0)

<强> Default.aspx的 在aspx文件中使用占位符标记

&LT; asp:PlaceHolder ID =“PlaceHolder1”runat =“server”&gt;

<强> Default.aspx.cs //添加/创建动态文本框

            TextBox txt = new TextBox();
            txt.ID = "New_txt";
            txt.TextMode = TextBoxMode.MultiLine;
            txt.Text = dt.Rows[0]["message"].ToString();
            txt.Width = 802;
            txt.Height = 450;
            txt.ReadOnly = true;
            PlaceHolder1.Controls.Add(txt);

从文本框中恢复值

string str = txt.Text;