Firing Events of dynamically loaded User Control

Discussion in 'ASP .Net' started by Raed Sawalha, Oct 7, 2004.

  1. Raed Sawalha

    Raed Sawalha Guest

    I am trying to sort a DataGrid in a user control that is programatically
    loaded when a link button is clicked.
    UC1 is loaded onto Page1.aspx after clicking a link button. No problem,
    loads fine.
    How do I call the following event on UC1 when the sort collumn of the data
    grid is clicked?

    Sub DataGrid1_SortCommand(ByVal s As Object, ByVal e As
    DataGridSortCommandEventArgs)

    When I click on the sort collumn, the Page_Load event of Page1.aspx is
    called, not the above Sub. If I load this user control at design time it
    works ok, just not when it it is loaded programatically.

    Can I check for e in the Page1.aspx Page_Load event?
    If Not Page.IsPostBack then
    'Do Something
    Else
    If e = something then
    Call the Sub DataGrid1_SortCommand(...)
    End If
    End If
    Is this correct or is there a better way to be doing this?

    thanks,
    Raed Sawalha, Oct 7, 2004
    #1
    1. Advertising

  2. Raed Sawalha

    Karl Seguin Guest

    The reason the sub isn't firing is because UC1 no longer exists...you need
    to reload it on postback. Typically the way this has been done is to store
    which dynamic controls have been loaded into the viewstate then reload them
    on postback ala:

    sub page_load
    if page.ispostback then
    dim controlPath as string= cstr(ViewState("lastControl"))
    if not controlPath is nothing then
    dim c as control = Page.LoadControl(controlPath )
    Page.COntrols.Add(c)
    end if
    end if
    end sub

    sub linkbutton_click
    dim c as control = Page.LoadControl("uc1.ascx")
    Page.Controls.Add(c)
    ViewState.Add("lastControl", "uc1.ascx")
    end sub


    Alternatively, you can use Denis Bauer's dynamic placeholder which does
    this for you (though i've never used it)
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Raed Sawalha" <> wrote in message
    news:...
    > I am trying to sort a DataGrid in a user control that is programatically
    > loaded when a link button is clicked.
    > UC1 is loaded onto Page1.aspx after clicking a link button. No problem,
    > loads fine.
    > How do I call the following event on UC1 when the sort collumn of the data
    > grid is clicked?
    >
    > Sub DataGrid1_SortCommand(ByVal s As Object, ByVal e As
    > DataGridSortCommandEventArgs)
    >
    > When I click on the sort collumn, the Page_Load event of Page1.aspx is
    > called, not the above Sub. If I load this user control at design time it
    > works ok, just not when it it is loaded programatically.
    >
    > Can I check for e in the Page1.aspx Page_Load event?
    > If Not Page.IsPostBack then
    > 'Do Something
    > Else
    > If e = something then
    > Call the Sub DataGrid1_SortCommand(...)
    > End If
    > End If
    > Is this correct or is there a better way to be doing this?
    >
    > thanks,
    >
    >
    >
    Karl Seguin, Oct 7, 2004
    #2
    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. Danny Bloodworth

    Events in dynamically loaded user controls

    Danny Bloodworth, Nov 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    548
  2. Earl Teigrob
    Replies:
    1
    Views:
    7,703
    nfedin
    Feb 10, 2004
  3. Thanh Nguyen
    Replies:
    0
    Views:
    442
    Thanh Nguyen
    Apr 1, 2004
  4. =?Utf-8?B?Smlt?=
    Replies:
    2
    Views:
    607
    =?Utf-8?B?Smlt?=
    Aug 20, 2007
  5. SevDer
    Replies:
    2
    Views:
    1,298
    Walter Wang [MSFT]
    Nov 13, 2007
Loading...

Share This Page