473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

A problem of dynamically creating a control



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.

Mar 22 '07 #1
3 1631
On Mar 22, 10:48 am, s9213...@gmail.com wrote:
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

Mar 22 '07 #2
On 3¤ë22¤é, ¤U¤È2®É01¤À, "Masudur" <munn...@gmail.comwrote:
On Mar 22, 10:48 am, s9213...@gmail.com wrote:


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

- Åã¥Ü³Q¤Þ¥Î¤å¦r -
Thanks a lot. Your reply save me much survey time

Mar 22 '07 #3
On 3¤ë22¤é, ¤U¤È2®É01¤À, "Masudur" <munn...@gmail.comwrote:
On Mar 22, 10:48 am, s9213...@gmail.com wrote:


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

Mar 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Duncan Welch | last post by:
Hi, I've got a fairly simple date control, that I'm creating dynamically in my page. The reason for creating it dynamically is that I want the ID to vary, depending on it's situation. The...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status)...
3
by: Daniel Friend | last post by:
I have an application and use custom user controls as plugins. Is there any way to communicate back and forth from control to app. NOTE: The control is not refrenced in the app, it will act as a...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
5
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
8
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. ...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.