Dynamic Controls Event Handling Problem

Discussion in 'ASP .Net' started by Paul Johnson, Jan 8, 2005.

  1. Paul Johnson

    Paul Johnson Guest

    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();
    }
     
    Paul Johnson, Jan 8, 2005
    #1
    1. Advertising

  2. "Paul Johnson" <> wrote in message
    news:...
    >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
     
    John Saunders, Jan 8, 2005
    #2
    1. Advertising

  3. Paul Johnson

    gdray

    Joined:
    Jun 12, 2007
    Messages:
    1
    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.
     
    gdray, Jun 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Caliendo

    Event handling of dynamic controls

    Steve Caliendo, Apr 15, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    608
    Kevin Spencer
    Apr 19, 2004
  2. WolfyUK
    Replies:
    2
    Views:
    499
    WolfyUK
    Oct 9, 2006
  3. Majeti Lakshmi Sreenivasulu

    Dynamic User Controls Event Handling Problem

    Majeti Lakshmi Sreenivasulu, Nov 11, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    146
    Majeti Lakshmi Sreenivasulu
    Nov 11, 2003
  4. Majeti

    Dynamic User Controls Event Handling

    Majeti, Nov 11, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    137
    Teemu Keiski
    Nov 13, 2003
  5. Patrick

    event handling for dynamic controls

    Patrick, Jan 16, 2004, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    139
    Patrick
    Jan 19, 2004
Loading...

Share This Page