繁体   English   中英

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

[英]Retrieve the values from textbox which is dynamically created using C#

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

现在点击按钮我试图从文本框中检索我已使用下面代码的值,但它抛出异常作为OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECTOBJECT 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

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

我所做的是为动态添加的控件提供一个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(); 
    }

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

编辑1

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

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

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

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

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

获取文本框值的简单行

TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;

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

从文本框中返回值

string str = txt.Text;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM