Groups | Blog | Home
all groups > asp.net > march 2007 >

asp.net : A problem of dynamically creating a control



s9213037 NO[at]SPAM gmail.com
3/21/2007 9:48:48 PM


Use case scenario:

I have a panel with ID "Upload_Panel", where there is a FileUpload
control with ID "FileUpload1" and a button with ID
"More_Upload_Files", both of which are added at design time.

When users click the "More_Upload_Files" button, a new FileUpload with
ID "FileUpload2" control will be added into "Upload_Panel"
dynamically.

Probelm:

The first added FileUpload (FileUpload2) work fine. However, when
users further add more FileUploads, the subsequent generated
FileUploads will replace the FileUpload2, rather than added to the
"Upload_Panel". In other words, FileUpload3 will replace FileUpload2,
FileUpload4 will replace FileUpload3 and so forth. The source code is
listed as follows.

******************************************************************************************

protected void More_Upload_Files_Button_Click(object sender,
EventArgs e)
{
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUpload" +
ViewState["noOfFileUploads"].ToString();
fileUpload.Width = Unit.Pixel(800);
this.Upload_Panel.Controls.Add(new LiteralControl("<br>"));
this.Upload_Panel.Controls.Add(fileUpload);

ViewState["noOfFileUploads"] =
(int)ViewState["noOfFileUploads"] + 1;
}

******************************************************************************************

Is there any state-related issue I ignore so that
Upload_Panel.Controls connlection is not persistent.



Any reply would be great appreciated.

Regards.
Masudur
3/21/2007 11:01:18 PM
[quoted text, click to view]

Hi...

Dynamically loaded controls do not persists in page... after post
back...
you need to repopulate the controls again....

Try adding the controls in page init...
each time the page is posted to server...

and don't for get to clear the control place holder....
Page_Init()
{
this.Upload_Panel.Controls.Clear();
int NoofUploadControls = (int)ViewState["noOfFileUploads"];
for(int i=0; i<NoofUploadControls ; i++)
{
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUpload" + i.ToString();
fileUpload.Width = Unit.Pixel(800);
this.Upload_Panel.Controls.Add(new LiteralControl("<br>"));
this.Upload_Panel.Controls.Add(fileUpload);
}
}

Now one more thing...
Since the control of upload_Panel get Clear...

You got to keep the command button and initial file upload control out
side the panel....

Thanks...
Masudur
http://www.kaz.com.bd
http://munnacs.blogspot.com
s9213037 NO[at]SPAM gmail.com
3/22/2007 6:10:47 AM
On 3=A4=EB22=A4=E9, =A4U=A4=C82=AE=C901=A4=C0, "Masudur" <munn...@gmail.com=
[quoted text, click to view]

Thanks a lot. Your reply save me much survey time
s9213037 NO[at]SPAM gmail.com
3/22/2007 6:17:11 AM
On 3=A4=EB22=A4=E9, =A4U=A4=C82=AE=C901=A4=C0, "Masudur" <munn...@gmail.com=
[quoted text, click to view]

There is one more question with respect to your suggested answer.

According to MSDN, view-state information cannot be accessed within
this "Init event"; it is not populated yet. Is this correct?
AddThis Social Bookmark Button