Click here to Skip to main content
15,904,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am adding a textbox to a panel(panel1) dynamically on click of a button add

And want to retrieve its value on lcick of a button (btnretrive)

but an exception is thrown saying panel1 doesnot contain any controls.

help me on this
Posted

Without your code, it is difficult to comment accurately on what is wrong with it! Instead, I would use this code to add the textbox to my panel:
TextBox tb = new TextBox();
tb.Width = 100;
tb.Height = 50;
tb.Location = new Point(10,10);
tb.Text = "My New TextBox";
myPanel.Controls.Add(tb);
And I would use this code to access it:
foreach(Control c in myPanel.Controls)
   {
   TextBox tb = c as TextBox;
   if (tb != null)
      {
      Control.WriteLine(tb.Text);
      }
   }
What are you doing that is different?
 
Share this answer
 
Comments
m@dhu 5-Apr-11 2:51am    
From OP:OriginalGriff,

IN the second code that you have given myPanel.Controls give null to me or myPanel.Controls.Count =0
Olivier Levrey 5-Apr-11 3:39am    
If myPanel.Controls is null, it means you forgot myPanel.Controls.Add(...).
Have a 5 OriginalGriff.
OriginalGriff 5-Apr-11 4:04am    
Then we need to see the relevant code fragments. Where are you declaring your Panel? In the Designer, or in code? If in code, we need to see that. We need to see the two button click handlers code exactly as you have them in your program - copy and paste them here so we can comment.
sekharkaza 5-Apr-11 5:14am    
Code is very simple.Point is on a button click am adding textbox control to panel(panel.controls.add(txtbox1))

On any other postback(i.e., click of btnretrieve, or any other click) am loosing those controls from the panel(because they were not added in pageload but addd later in a button click ).


Now my question is how to retain those controls in a postback.
OriginalGriff 5-Apr-11 5:29am    
Oh dear.
You do realize that doesn't work?
When you create dynamic controls in a webpage, rather than a winform, they only exist in the codebehind for the life of the page load. When you get the postback, the page is reloaded as far as the codebehind is concerned and they never existed - your panel reports empty, because it is empty.
Help is at hand, but you will have to do a fair amount of reading @ MSDN: http://msdn.microsoft.com/en-us/library/ms972976
This link [^]discusses looping through controls and may be a help to you.
 
Share this answer
 
try this Link
Link [^]
 
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