Click here to Skip to main content
15,941,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding textbox and fileupload control runtime by this way-
for (int i = 0; i < no; i++)
{
FileUpload fileup = new FileUpload();
TextBox txt = new TextBox();
fileup.ID = i.ToString();
pnlupload.Controls.Add(txt);
pnlupload.Controls.Add(fileup);
}
Then I want to save values of Text box and File upload. But, It's not working.Help Me.
Posted
Comments
Andy Lanng 18-Mar-13 7:48am    
You have kindly posted the code for creating the controls but could you please post the code you have tried to retrieve these controls. Thanks

1 solution

Please improve your code like this

for (int i = 0; i < 4; i++)
{
FileUpload fileup = new FileUpload();
TextBox txt = new TextBox();
txt.ID = "txt" + i.ToString();//Specify ID for your control
fileup.ID = "FileU"+i.ToString();//Specify ID for your control
Panel1.Controls.Add(txt);
Panel1.Controls.Add(fileup);
}

and Then try code given below for accessing the values of your controls

TextBox tb = (TextBox)Panel1.FindControl("txt1");
string tx = tb.Text;
FileUpload fb = (FileUpload)Panel1.FindControl("FileU1");
string nam= fb.FileName;
 
Share this answer
 
v2
Comments
pal89 18-Mar-13 8:31am    
object of textbox tb, object of fileUpload fb, are getting NULL values. so throws exception.
Braj_12 18-Mar-13 8:36am    
where are you accessing these controls values ..I have accessed on the click of button event.
Braj_12 19-Mar-13 6:07am    
I am adding these dynamic controls on form load in panel1 and on click of button i can find all the controls.
CHill60 19-Mar-13 6:07am    
It's because the ID is not persisted between post backs - the solution will only work if the creation of the controls and the reading of the content are in the same function in the code-behind - an unlikely position.
Have a look at this article to fix the problem http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

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