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

F

Flare

(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
 
A

Arnaud PICHERY

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)
 
F

Flare

You can find a description of the ASP.NET control life cycle at

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
 
A

Arnaud PICHERY

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)
 
F

Flare

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
 
F

Flare

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
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top