473,396 Members | 2,033 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,396 software developers and data experts.

Dynamic Controls Event Handling Problem

I have problems with eventhandlers when dynamically moving controls in
ASP.NET.

The controls are loaded into a placeholder on postback so that the
eventhadlers can fire. If a move evet or delete event fires the place
holder is cleared and repopulated. Unfortunatly the UniqueID carries on
increaseing from the last UniqueID of the cleared controls. This meens
if a button is clicked and page is posted when the page is rebuild the
UniqueID dosnt match and the event dosnt fire. Does anyone know how to
get around this? Some sample code is below though its a bit messy now.

private void Page_Load(object sender, System.EventArgs e)
{
if( IsPostBack )
{
RedrawComponents();
}
else
{
PopulateComponents();
}
}

private void PopulateComponents()
{
ClearChildViewState();
_pageComponentTypes = new ArrayList();
DataAccess objDA = new DataAccess( "BoundComponentsSelect" );
DataTable dtItems = objDA.GetTable( "@PageID", 1 );

foreach( DataRow drItem in dtItems.Rows )
{
if( (int)drItem["PagePartition"] == 3)
{
_pageComponentTypes.Add( new object[]{drItem["PagePartition"],
drItem["ControlPath"]} );
}
}
RedrawComponents();
int count = 0;
foreach( DataRow drItem in dtItems.Rows )
{
if( (int)drItem["PagePartition"] == 3)
{
PageComponentBase pcItem = (PageComponentBase)Panel1.Controls[count];
pcItem.BoundComponentID = (int)drItem["BoundComponentID"];
pcItem.Editable = (bool)drItem["Editable"];
pcItem.EditMode = _editMode;
pcItem.ComponentName = (string)drItem["Name"];
pcItem.Populate();
count ++;
}
}
}

private void RedrawComponents()
{
Panel1.Controls.Clear();
ClearChildViewState();

foreach( object[]objItem in _pageComponentTypes )
{
string strControlPath = (string)objItem[1];
PageComponentBase pcItem = (PageComponentBase)LoadControl(
"~/PageComponents/" + strControlPath );
Panel1.Controls.Add( pcItem );
pcItem.Deleted += new
GlenDimplexHA.PageComponents.PageComponentBase.Emp tyEvent(pcItem_Deleted);
pcItem.Moved += new
GlenDimplexHA.PageComponents.PageComponentBase.Emp tyEvent(pcItem_Moved);
}
}

#region ViewState
protected override void LoadViewState(object savedState)
{
object[]state = (object[])savedState;

base.LoadViewState (state[0]);
_editMode = (bool)state[1];
_pageComponentTypes = (ArrayList)state[2];
}

protected override object SaveViewState()
{
object[]state = new object[]
{
base.SaveViewState (),
_editMode,
_pageComponentTypes
};

Session["EditMode"] = _editMode;
return state;
}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
_editMode = !_editMode;
PopulateComponents();
}

private void pcItem_Deleted()
{
//Response.Redirect( Request.Url.PathAndQuery );
PopulateComponents();
}

private void pcItem_Moved()
{
//Response.Redirect( Request.Url.PathAndQuery );
PopulateComponents();
}
Nov 19 '05 #1
1 1347
"Paul Johnson" <P_***@Hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP14.phx.gbl...
I have problems with eventhandlers when dynamically moving controls in
ASP.NET.

The controls are loaded into a placeholder on postback so that the
eventhadlers can fire. If a move evet or delete event fires the place
holder is cleared and repopulated.


You can't move the controls and still have ViewState work. If ViewState
doesn't work, then neither will change events. You must recreate the
controls in the exact same order as the last time the page was requested.

Now, if you need to change the control hierarchy due to some event (a Click
event, perhaps), then you need to clear and recreate the control hierarchy,
which will create a new order in which the controls will have to be loaded
next time around.

John Saunders
Nov 19 '05 #2

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

Similar topics

1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
6
by: Steve Caliendo | last post by:
Hi, I'm creating 5 ImageButton controls in the panel control, and I have a unique ID specified for each one. When I click on any one of them, the Page_Load executes (Of course), but how do I...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
1
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override...
2
by: WolfyUK | last post by:
Hello, I have a standard asp:DataGrid called CasesGrid that I wish to write my own paging controls for. The aim is to get something like the following rendered to screen: << First < Previous...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
0
by: =?Utf-8?B?SWRlcm9ja3M=?= | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and I...
1
by: iderocks | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and when I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...
0
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...

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.