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();
}
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();
}