473,763 Members | 5,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange event postback behaviour in dynamically created usercontrol

hi,
i have an aspx page which dynamically loads a user control and adds it
to a placeholder. the control is recreated and added to the
placeholder for postbacks as well.
the user control contains a datagrid and some buttons and textboxes.
the button events fire correctly and execute their code. however, the
datagrid events fire a postback, and execute Page_load in the
usercontrol, but the Item_Command code is not executed. the code to
add events to the datagrid is in InitialiseCompo nent.

i have the datagrid binding in UserControl_Pre Render, so that it is
bound between postbacks.

can anyone suggest why the postback is happening without the
ItemCommand event happening? i've verified this with VS debugging.

thanks
tim

Nov 19 '05 #1
2 3450
Hi Tim_Mac,

Welcome to ASP.NET newsgroup.
From your description, you've a UserControl which contains some button and
a DataGrid on it, the datagrid is subscribing some postback
events(ItemComm and). And in one of your asp.net page, you dynamically load
the usercontrol and add it on your page. However, you found the ItemCommand
of the datagrid(in usercontrol) always not fire, yes?

Based on my experience, such event handler not executing problem is likely
caused by the following things:
1. For dynamic loaded(created) controls, make sure we add them in the
Page_Init or Page_Load every time the page is requested(IsPos tback or
not...).

2. For control's event handler, we also need to register them in Init or
Load event. Also, for template databound controls such as DataGrid or
DataList, we should avoid rebinding them with different datasource before
processing postback event since the event may depend on the original binded
datas.

As for your condition, since some other controls (such as button)'s event
handler execute correctly. I think the problem is likely within the
UserControl itself. Would you try registering your datagrid's ItemCommand
handler in the usercontrol's ascx template instead of programmaticall y
register in codebehind? Just like
<asp:datagrid ...... OnItemCommand=" DataGrid_ItemCo mmand" ...>

If you feel it convenient, I suggest you try build a simpler usercontrol
(contains a datagrid) so as to repro the problem and send it to me. Then I
can do some researchs on my side.

In addition, if you feel necessary, I can also generate a test page on my
side and let you have a compare to see whehter there are any difference.

Please feel free to let me know if you have any questions. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 19 '05 #2
hi Steven,
thank you for your most helpful reply.
i have got it working now, and will attempt to explain how and why for
future reference.

my aspx page contaings the following in Page_Load:
Modules.SportsL adder ladder =
this.LoadContro l("Modules/SportsLadder.as cx") as Modules.SportsL adder;
this.serverForm .Controls.Add(l adder);

the user control is loaded regardless of whether a postback has taken
place or not.
the user control then contains a call to if(!IsPostBack) BindGrid() in
the Page_Load.

when i would delete an item in the datagrid, the page would post back,
and Page_Load would fire, but the datagrid_itemco mmand event would not
fire. the datagrid would disappear from the page.

i tried adding in a PreRender event for the usercontrol, and calling
BindGrid() there, and that kept the datagrid on screen after postbacks,
but the itemcommand event still didn't fire. what got it working was i
removed the if(!IsPostBack) from the Page_Load of the user control, so
then the datagrid is bound every time. i then had the problem of the
itemcommand event being called twice (what a surprise). the reason is
because it was set in the aspx page, as Steven suggested:
<asp:datagrid ...... OnItemCommand=" DataGrid_ItemCo mmand" ...>
but it was also set in InitialiseCompo nent in code. i took it out of
the code, and let the aspx handler do it.

now it works perfectly. i didn't run into any problems re-binding the
datagrid in Page_Load, my methods were still able to identify the row
that was clicked and any of the controls in the cells.

thanks for your help.
tim

Nov 19 '05 #3

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

Similar topics

3
9008
by: William LaMartin | last post by:
If I create a RadioButtonList with, say, two items, then after the page loads and I select one of the items and then click on a button whose click event contains some code to display the RadioButtonlList's selected value, there is nothing. On a postback, with the dynamically created button list visible on the page and the first item selected, if I try some code like "If Me.RadioButtonList1.Items(0).Selected = True Then.......", then I...
3
1666
by: Big D | last post by:
I have a class that inherits from System.Web.Ui.Usercontrol... all is basically is is a button which is created progammatically such as: public abstract class BaseFrontEndEditor : System.Web.UI.UserControl { protected Button btnEdit = new Button(); private void Page_Load(object sender, System.EventArgs e) {
1
2803
by: Marcel Balcarek | last post by:
I create a user control dynamically: Dim newCriteriaControl As CriteriaTextBoxUserControl = CType(Me._page.LoadControl("CriteriaTextBoxUserControl.ascx"), CriteriaTextBoxUserControl) myContainer.Add(newCriteriaControl) and the user control's load event executes on initial executon of the page. On a POST of the same page, the control is added as above, but the load event is not firing, nor is prerender.
6
3380
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
2
3837
by: Developer_Software | last post by:
Thanks in advance to anyone who can help :) I've got a placeholder control WITHIN A USER CONTROL that has its contents dynamically added and removed at runtime by a regular .aspx page. At runtime, the placeholder control adds a dropdownlist where I would like it to redirect the user to a different page dependent on the selection they make from the dropdownlist. I've tried writing the delegate and the event handler both in the
9
14459
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same time. I have to click twice on the controls for the events to be raised, the first time nothing...
5
1040
by: Martin | last post by:
I have created a subclass of a textbox. In this subclass I have a property called 'Caption'. If this property contains a value then I want to create a Label object and add it to the controls collection of the parent of the Textbox. So, basically both objects exist next to eachother on the same parent. Originally I tried the object creation inside the Property-Set, but this seems that this is too early, because the textbox itself isn't...
0
2941
by: CMELLO | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After that the controls are loaded/unloaded based on the SelectionChanged event for the tab strip and again in Page load because with a dynamic load viewstate has to be reloaded. I have a datalist in the user control and I am trying to create the...
0
1915
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After that the controls are loaded/unloaded based on the SelectionChanged event for the tab strip and again in Page load because with a dynamic load viewstate has to be reloaded. I have a datalist in the user control and I am trying to create the...
0
9387
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.