473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("My UC.ascx");
((MyUC)Componen t1).RapId = 0;
PlaceHolderForC omponents.Contr ols.Add(Compone nt1);

The UserControl contains some textboxes wich is viewstateenable d 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 4157
> 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....hav e 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....hav e 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(wi ch 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(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{ DataGrid1.DataS ource = ws.GetAllRappor tNames();
DataGrid1.DataB ind(); }}
private void DataGrid1_EditC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{ DataGrid1.EditI temIndex = e.Item.ItemInde x;}

private void DataGrid1_Cance lCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{ DataGrid1.EditI temIndex = -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
24572
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
6998
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 changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected...
0
1004
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. An XML file defines which plugins are available and should be listed in my tab control. When the user clicks on as tab, the page should load the...
9
2438
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"); ucline.Initializecontrol(line,alternate); Placeholder1.Controls.Add(ucline); }
4
5026
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 GridView that needs to be dynamically designed, depending on what collection of fields our uses want to edit for their product data. We have 400+...
3
2332
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 page. So whenever the Admin person comes to know about the new category in the market he will be adding as different Sub-Categories for example...
4
3920
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 viewstate. I had created 4 methods to Add/Remove my special dates in viewstate. Up to here all seems perfect, but how do I used it in prerender event...
12
2648
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 test button (in a WEb User Control inside the aspx page), cookie value is displayed in Master Page but if I go to another page, no value is...
0
1656
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 page. For now, I will call them box1 and box2. Each box has a panel and will display a list of link buttons on the panel. When the page loads for...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.