Problem with adding UserControls programtic in the OnPreRender event and still keep the UC viewState

Discussion in 'ASP .Net' started by Flare, Nov 21, 2003.

  1. Flare

    Flare Guest

    (ASP.NET 1.1)
    Hi

    I have a problem with my ViewStates in a userControl. The problem is that
    the ViewState is not "writte" og changed if you like after editing in my
    case a textbox.

    This is the code I use to add the User controll

    Control Component1 = null;
    Component1 = LoadControl("MyUC.ascx");
    ((MyUC)Component1).RapId = 0;
    PlaceHolderForComponents.Controls.Add(Component1);

    The UserControl contains some textboxes wich is viewstateenabled and
    autopostbacks.

    If i change the value in one of these textboxes in the UC and the above code
    is placed in Page_Load every thing works fine, but if i place it in the
    OnPreRender event all my change to my textboxes is discarded.

    I simply Cant see why this should make the change disapear and i really hope
    someone has a solution.

    Best reagards
    Anders, DK
     
    Flare, Nov 21, 2003
    #1
    1. Advertising

  2. > I simply Cant see why this should make the change disapear and i really
    hope
    > someone has a solution.

    If you are familiar with the ASP.NET control life-cycle, this makes perfect
    sense.
    The ChangedEvent/PostbackEvent steps (the steps that takes into account the
    changes) after fired BEFORE the Load step but AFTER the PreRender step.

    And if ASP.NET does find the control that raised the postback during the
    ChangedEvent/PostbackEvent steps, it ignores the changes.

    You can find a description of the ASP.NET control life cycle at
    http://www.15seconds.com/issue/020102.htm

    Hope this helps,
    Arnaud :O)
     
    Arnaud PICHERY, Nov 21, 2003
    #2
    1. Advertising

  3. Flare

    Flare Guest

    > You can find a description of the ASP.NET control life cycle at
    > http://www.15seconds.com/issue/020102.htm


    Great article. But i made me a little dipressed ;).

    I now have another simular problem. I have a datagrid in a usercontrol and
    when i click eg. the [edit] button nothing happens. And that makes sence
    since the page is already drawn!

    But does that mean i cant use eg. a datagrid in a usercontrols? That
    sounds...quite limiting....have i misunderstood something again.?

    Reagards
    Anders
     
    Flare, Nov 21, 2003
    #3
  4. > I now have another simular problem. I have a datagrid in a usercontrol and
    > when i click eg. the [edit] button nothing happens. And that makes sence
    > since the page is already drawn!

    No no... when you click on the edit button, a new request is sent to the
    server (a postback in .NET language) and another life cycle takes place.

    > But does that mean i cant use eg. a datagrid in a usercontrols? That
    > sounds...quite limiting....have i misunderstood something again.?

    You can.. and yeah it works for me wonderfully. What you just need to
    remember
    is to add the user control to the page during or before the Load step,
    same for the datagrid to the user control, same for the button to the data
    grid.

    Hope this helps,
    Arnaud :O)
     
    Arnaud PICHERY, Nov 21, 2003
    #4
  5. Flare

    Flare Guest

    > You can.. and yeah it works for me wonderfully. What you just need to
    > remember
    > is to add the user control to the page during or before the Load step,
    > same for the datagrid to the user control, same for the button to the data
    > grid.


    Where do you mean by before or during load step?

    If I add the userControls(wich contains the datagrid) in the Page_Load
    event, I have to click the buttons in the datagrid twice. (Or provoke
    another postback eg.a button pressed). In the Onload event the UC never
    appears. In OnPreRender the UC appears but thr datagrid dosesnt change
    state.

    In wich event do you mean i should add my user controls. The datagrid is
    just plain simple and databinded to a DataSet (And i works).

    Its just a pain to sit with such a simple problem for so long....Really hope
    you have some inputs, or request some codesamples.

    Reagards
    Anders
     
    Flare, Nov 21, 2003
    #5
  6. Flare

    Flare Guest

    > Its just a pain to sit with such a simple problem for so long....Really
    hope
    > you have some inputs, or request some codesamples.


    Now i made an even simpler test. Create a UserControl, drag Datagrid to it
    put his code in the UserControl.

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    { DataGrid1.DataSource = ws.GetAllRapportNames();
    DataGrid1.DataBind(); }}
    private void DataGrid1_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    { DataGrid1.EditItemIndex = e.Item.ItemIndex;}

    private void DataGrid1_CancelCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    { DataGrid1.EditItemIndex = -1;}
    }

    Now i have an emtpy form and drag the userControl to it and Run the Proejct.
    The datagrid shows perfecttly...!"#!" But i have to click twice on the Edit
    button, (and cancel).

    Now what is wrong on the user control? I have tesetet this mini test on 2
    computers using ie6.0.

    Regards
    Anders
     
    Flare, Nov 21, 2003
    #6
    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. Kenton Smeltzer
    Replies:
    3
    Views:
    3,624
    Kenton Smeltzer
    Oct 23, 2003
  2. Umut Tezduyar
    Replies:
    1
    Views:
    408
    Steven Cheng[MSFT]
    Oct 6, 2004
  3. ujjc001
    Replies:
    0
    Views:
    1,385
    ujjc001
    Jul 27, 2005
  4. Flare
    Replies:
    5
    Views:
    139
    Flare
    Nov 21, 2003
  5. Flare
    Replies:
    5
    Views:
    142
    Flare
    Nov 21, 2003
Loading...

Share This Page