Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with adding UserControls programtic in the OnPreRender event and still keep the UC viewState

Reply
Thread Tools

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

 
 
Flare
Guest
Posts: n/a
 
      11-21-2003
(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


 
Reply With Quote
 
 
 
 
Arnaud PICHERY
Guest
Posts: n/a
 
      11-21-2003
> 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)


 
Reply With Quote
 
 
 
 
Flare
Guest
Posts: n/a
 
      11-21-2003
> 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


 
Reply With Quote
 
Arnaud PICHERY
Guest
Posts: n/a
 
      11-21-2003
> 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)


 
Reply With Quote
 
Flare
Guest
Posts: n/a
 
      11-21-2003
> 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


 
Reply With Quote
 
Flare
Guest
Posts: n/a
 
      11-21-2003
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading usercontrols, viewstate problem, slighly different from all others "viewstate uc problems" please help... ujjc001 ASP .Net 0 07-27-2005 01:52 PM
How can i handle post back event of the control that is added on the OnPreRender () method Umut Tezduyar ASP .Net 1 10-06-2004 09:10 AM
Problem with adding UserControls programtic in the OnPreRender event and still keep the UC viewState Flare ASP .Net Building Controls 5 11-21-2003 06:56 PM
Problem with adding UserControls programtic in the OnPreRender event and still keep the UC viewState Flare ASP .Net Web Controls 5 11-21-2003 06:56 PM
Events, Viewstate, OnPreRender it has to be a bug Kenton Smeltzer ASP .Net 3 10-23-2003 10:08 PM



Advertisments