3

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

4 回答 4

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(); 
    }
于 2013-03-28T06:52:03.847 回答
0

您必须重新创建控件init才能获得它的价值。
这是一些链接
从asp.net中动态创建的文本框中获取文本

编辑 1

TextBox tb=(TextBox)ViewState["Temptbl"];
splY.Controls.Add(tb);
于 2013-03-28T06:50:15.203 回答
0

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

它解释了如何使用panelcontrol 动态地放置和获取带有值和验证的文本框。

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

使用验证获取并创建动态文本框和下拉列表

用于获取文本框值的简单行

TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
于 2013-03-28T07:05:20.483 回答
0

Default.aspx 在 aspx 文件中取占位符标记

<asp:PlaceHolder ID="PlaceHolder1" runat="server">

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);

从文本框中检索值

字符串str = txt.Text;

于 2013-03-28T08:00:58.177 回答