473,386 Members | 1,908 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,386 software developers and data experts.

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

(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
Nov 18 '05 #1
5 4147
> 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)
Nov 18 '05 #2
> 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
Nov 18 '05 #3
> 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)
Nov 18 '05 #4
> 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
Nov 18 '05 #5
> 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
Nov 18 '05 #6

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

Similar topics

5
by: Daniel Groh | last post by:
Hi, i'd like to know more abou this event, i tryed to found in MSDN but the explanation is null for me...is there some explanation when and how should i use it ?
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
0
by: Hendrik W. Hansen | last post by:
I'm working on a WebForm, that through some tabs can load and contain a number of different "plugin" UserControls, all derived from the same "plugin" super class - a bit like the IBuySpy portal. ...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
4
by: rushikesh.joshi | last post by:
Hi All, I want to create custom control by using Web.UI.WebControls.Calendar, in which I want to set few days with different color. I had created two property to set the color and storing in...
12
by: Patxi | last post by:
This is the first time I try to use cookies, and despite of reading some tutotials, I'm have real trouble to make it work correctly. My cookie reading code is in Master Page. When I click on a...
0
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.