Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to get dynamically created textbox value in asp.net

This is code which I have created in this code text box are created but when I will input the value in the text and retrieve the value from dynamically created text box it give error
Object reference not set to an instance of an object


Below is my code...

protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Generate initial textboxes 
            GenerateButton_Click(sender, e);
        }
    }
    protected void GenerateButton_Click(object sender, EventArgs e)
    {
        //Generate textboxes 
        int z = int.Parse(HowManyToGenerateTextBox.Text);

        Panel1.Controls.Clear();

        for (int i = 0; i < z; i++)
        {
            TextBox s = new TextBox();
            s.ID = "tb" + i.ToString();
            Session[s.ID] = s;
            Panel1.Controls.Add(s);
        }
    }
    protected void CalculateButton_Click(object sender, EventArgs e)
    {
        //Calculate sum 
        int sum = 0;

        for (int i = 0; i < Session.Count; i++)
        {
            TextBox txt = (TextBox)Panel1.FindControl("tb" + i.ToString());
            ResultLabel.Text = txt.Text;

           
            if (ResultLabel.Text != null)
                sum += int.Parse(ResultLabel.Text);
        }

        ResultLabel.Text = sum.ToString();
    }
Posted
Comments
Praveen Kumar Upadhyay 18-Dec-14 6:02am    
By seeing your code I can say that if there will be a postback, you will loose all your dynamic created controls.
basurajkumbhar 18-Dec-14 6:09am    
When I click on the Generate Button then TextBox are created and when i click on the Calculate Button then
TextBox txt = (TextBox)Panel1.FindControl("tb" + i.ToString());
Here in the txt the vale is null,
ResultLabel.Text = txt.Text;
at this line line i am getting the error

Object reference not set to an instance of an object

What i want to do in my code to get the text value.

try this
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        GenerateButton_Click(sender, e);
    }
    public TextBox[] s;
    protected void GenerateButton_Click(object sender, EventArgs e)
    {
        int z = Convert.ToInt32(TextBox1.Text);
        Panel1.Controls.Clear();
        Array.Resize(ref s, z);
        for (int i = 0; i < z; i++)
        {
            s[i] = new TextBox();
            s[i].ID = "tb" + i.ToString();
            s[i].Text = "tb" + i.ToString();
            Session[s[i].ID] = s;
            Panel1.Controls.Add(s[i]);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
                Response.Write(s[0].Text);
    }
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 18-Dec-14 6:34am    
Nice..
basurajkumbhar 18-Dec-14 7:23am    
It gives error when load the page
Object Reference not set
Praveen Kumar Upadhyay 18-Dec-14 7:45am    
Replace TextBox1 to HowManyToGenerateTextBox
Sid_Joshi 18-Dec-14 7:26am    
click taskbar menu and right click on ASP.Net Deployment Server and select Stop
and run project
basurajkumbhar 18-Dec-14 7:53am    
Thank's a lot all of us for giving me reply it's working

Thank's a for specially 88388132 and Praveen Kumar
try this,

TextBox txt = (TextBox)Panel1.FindControl("tb" + i);

or

may be this link could help you:


http://stackoverflow.com/questions/12560074/handle-dynamic-control-in-asp-net-findcontrol[^]
 
Share this answer
 
Comments
basurajkumbhar 18-Dec-14 7:54am    
Thank's fro giving this help full link
Deepak....
deepankarbhatnagar 18-Dec-14 7:57am    
your welcome
In your code, it is written that GenerateTextBox when there is no post back. When you will click on Calculate button so your complete page will be postback and your dynamic controls will be lost, So you need to retain the dynamic controls.

Check out below link, It will definitely solve your problem.

Retaining State for Dynamically Created Controls in ASP.NET applications[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900