Firing Events of dynamically loaded User Control

R

Raed Sawalha

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,
 
K

Karl Seguin

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
 

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

Top