A problem of dynamically creating a control

Discussion in 'ASP .Net' started by s9213037@gmail.com, Mar 22, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Masudur Guest

    On Mar 22, 10:48 am, 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
     
    Masudur, Mar 22, 2007
    #2
    1. Advertising

  3. Guest

    On 3¤ë22¤é, ¤U¤È2®É01¤À, "Masudur" <> wrote:
    > On Mar 22, 10:48 am, 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, 2007
    #3
  4. Guest

    On 3¤ë22¤é, ¤U¤È2®É01¤À, "Masudur" <> wrote:
    > On Mar 22, 10:48 am, 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, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gibs

    creating control dynamically

    Gibs, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    333
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,808
    Matt Berther
    Mar 5, 2005
  3. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,376
    Scott Allen
    Mar 4, 2005
  4. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,113
    Ravi Ambros Wallau
    Jun 1, 2005
  5. Ravi Ambros Wallau
    Replies:
    0
    Views:
    324
    Ravi Ambros Wallau
    Jun 1, 2005
Loading...

Share This Page