3

I have created few textbox dynamically while coding in the flow I have provided unique id for each and I have hard coded some values to all the text boxes using C#.

Now on click of button am trying to retrieve the values from the textbox for which I have used the below code, but its throwing an exception as OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.

Please look at the below code, I have tried both the things but still am not getting.
Please help me out.
Thanks

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

Thanks

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
शेखर
  • 17,412
  • 13
  • 61
  • 117
Rakesh
  • 87
  • 1
  • 3
  • 9
  • first make sure that it is not a postback issue, before retrieving the value of textbox make sure that during postback you have created that and they exists. – Mogli Mar 28 '13 at 06:49

4 Answers4

1

I've had the same problem in the past.

What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback.

Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.

Read out this code once

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(); 
    }
Mogli
  • 1,972
  • 11
  • 34
  • 67
  • Thanks. am trying to use View state. but its throwing exception as invalid Arguments. Please refer the below code. – Rakesh Mar 28 '13 at 11:37
  • Because of text limit am splitting into two pats. please go through it. – Rakesh Mar 28 '13 at 11:55
  • How can i show my code to you, some some 15 lines is there but this text limit restrictions i cannot able to post. Can u please tell as am very new to this.. so. thanks – Rakesh Mar 28 '13 at 11:58
  • see there is a link namely "edit" just below your question which you posted, click on that and it will allow you to edit your question, just add that code just below your original question. – Mogli Mar 28 '13 at 12:01
0

You must recreate your controls on init to get it's value.
Here are some links
Get text from dynamically created textbox in asp.net

Edit 1

TextBox tb=(TextBox)ViewState["Temptbl"];
splY.Controls.Add(tb);
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • Thak you. In order to Recreate only i putting into view state and in another button click i just using that viewstate and binding it into my panel ones aging like splY.Controls.Add(ViewState["Temptbl"]); but here its telling i cannot do that. Its giving the exception as invalid arguments. – Rakesh Mar 28 '13 at 12:29
  • @Rakesh you need to cast it back to `textbox` and then add it. It should work. See my edit. – शेखर Mar 28 '13 at 12:43
  • thanks but its not working throwing the exception like Type 'System.Web.UI.WebControls.Table' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable. – Rakesh Mar 28 '13 at 13:20
  • what you have kept in the `view-stat` – शेखर Mar 28 '13 at 14:57
  • inside of a dynamically created table am creating a dynamic textbox. So am keeping the table itself into the viewstate. ViewState["temptbl"] = tblMainY – Rakesh Mar 29 '13 at 04:10
  • If you want to keep anything in the view-state then it must be serialize and `Table` is not serialize. So instead of keeping table into `viewstate` keep the `textbox` inside viewstate. And retrive textbox from view state and create a table and add it in the container. – शेखर Mar 29 '13 at 04:32
  • Thanks, but the thing is am creating 15 text box using a loop, i dont think so that its good to assign each text box into a ViewState. please correct me if am wrong. – Rakesh Mar 29 '13 at 08:52
  • No just create a list of textbox and keep into a single viewstate. – शेखर Mar 29 '13 at 08:56
  • hope this link help you http://mshelp.be/storing-a-list-of-objects-in-viewstate-155.htm – शेखर Mar 29 '13 at 08:57
0

some sample code in bellow link as my blog

Its explain for how to put and get textboxe's with values and validations in dynamicaly using panel control .

Let's go this url . and you can get good solutions

get and Create dynamic Textbox and dropdownlist with Validation

simple line for get textbox values in

TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

Default.aspx take placeholder tag in aspx file

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

Default.aspx.cs // adding/creating dynamic text box

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

Retrive value from text box

string str = txt.Text;