A problem of dynamically creating a control

S

s9213037

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.
 
M

Masudur

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.

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
 
S

s9213037

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.

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.

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...
Masudurhttp://www.kaz.com.bdhttp://munnacs.blogspot.com- ÁôÂóQ¤Þ¥Î¤å¦r -

- Åã¥Ü³Q¤Þ¥Î¤å¦r -

Thanks a lot. Your reply save me much survey time
 
S

s9213037

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.

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.

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...
Masudurhttp://www.kaz.com.bdhttp://munnacs.blogspot.com- ÁôÂóQ¤Þ¥Î¤å¦r -

- Åã¥Ü³Q¤Þ¥Î¤å¦r -

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top