Dynamic Controls Event Handling Problem

P

Paul Johnson

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.EmptyEvent(pcItem_Deleted);
pcItem.Moved += new
GlenDimplexHA.PageComponents.PageComponentBase.EmptyEvent(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();
}
 
J

John Saunders

Paul Johnson said:
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
 
Joined
Jun 12, 2007
Messages
1
Reaction score
0
Paul,

I realize that this post is over two years old, but what solution did you come up with for this problem. I am having the same problem and I am at a dead end. Thanks.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top