dynamic loaded ascx delegate event not firing in parent form

C

CMELLO

I have am dynamically loading a web user control based on the click of
a tab strip
I load the default control for the first tab in the page load event
after checking page is not postback. After that the controls are
loaded/unloaded based on the SelectionChanged event for the tab strip
and again in Page load because with a dynamic load viewstate has to be
reloaded. I have a datalist in the user control and I am trying to
create the delegate so the parent can handle the click of the edit
button in the user control. Honestly I am not very good at event
delegation the code complies but I never get the command to execute on
the parent

in the child control site.ascx I set up the datalist public event
with code below

public partial class Site : System.Web.UI.UserControl
{
public delegate void myItemCommand(object source,
DataListCommandEventArgs e);
public event myItemCommand dlSiteItemCommand;

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{ }


protected void dlSite_ItemCommand(object source,
DataListCommandEventArgs e)
{
if (dlSiteItemCommand != null)
dlSiteItemCommand(source, e);
}

protected void Page_Load(object sender, EventArgs e)
{ //get the data and fill the datalist }

in the parent (Detail) I try to bind to the event in the Site.ascx
When the page loads in the parent the procedure LoadUserControl()
is called and in it after I dynamically load the correct control, I
caste the generic control to an instance of Site so I can see the
public event I created. Thats where I have got it wrong I am SOOOOO
confused. The binding does not work when I click the edit button in
the child datalist I never break into the event that is bound on the
parent Below is parent code. Please I not this is too long an
explanation but I have been trying and reading a long time.

Cindy

public partial class Detail : System.Web.UI.Page
{
private string LastLoadedControl
{
get
{
return ViewState["LastLoaded"] as string;
}
set
{
ViewState["LastLoaded"] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;

if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
if (controlPath == "Site.ascx")
{
Site ucSite = (Site)uc;
ucSite.dlSiteItemCommand += new
Site.myItemCommand(ucSite_dlSiteItemCommand);
}
PlaceHolder1.Controls.Add(uc);
}
}

void ucSite_dlSiteItemCommand(object source,
DataListCommandEventArgs e)
{
(source as DataList).EditItemIndex = e.Item.ItemIndex;
(source as DataList).DataSource = dsTunnel;
(source as DataList).DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{}

protected void Page_Load(object sender, EventArgs e)
{
if(!(Page.IsPostBack))
{
string controlPath = null;
controlPath = "Site.ascx";
LastLoadedControl = controlPath;
LoadUserControl();
}
LoadUserControl();
}
private void ts_SelectionChanged(object sender,
JQD.TabStrip.SelectionChangedEventArgs e)
{
int TabPos = e.TabPosition;
ActiveTab = TabPos;
string controlPath = string.Empty;

switch (TabPos)
{
case 0:
controlPath = "Site.ascx";
LastLoadedControl = controlPath;
LoadUserControl();
break;

case 1:
break;
}
 
E

Evan Freeman

Ok I need to see what your method LoadControl looks like. Thats the key to
understanding this other code.

But anyhow my guess is that the control isn't being created and setup in the
correct stages.

SO you createa a control:

Button aButton = new Control();

You add it to the place holder, after giving it an ID, or not depends on
what your doing:

placeHolder.Controls.Add(aButton );

Setup properties:

aButton .Click += EventHandler(aButton_Clicked); //or whatever syntax you
like, to many choices in syntax for me these days.

Now this is all wired up and ready to work.

But here is the rub. On postBack the control no longer exists, so no event,
so you need to rewire it in the Init of the page, or it will be to late.

Also if you don't create it with the same ID the postback events won't map
correctly and again it won't fire.


** I have an example of what your trying to do I think, a working one
somewhere as part of an article I was working on I'll try to post it to you
in a day or two.
--
Evan Freeman
(e-mail address removed)
http://evanfreeman.blogspot.com


I have am dynamically loading a web user control based on the click of
a tab strip
I load the default control for the first tab in the page load event
after checking page is not postback. After that the controls are
loaded/unloaded based on the SelectionChanged event for the tab strip
and again in Page load because with a dynamic load viewstate has to be
reloaded. I have a datalist in the user control and I am trying to
create the delegate so the parent can handle the click of the edit
button in the user control. Honestly I am not very good at event
delegation the code complies but I never get the command to execute on
the parent

in the child control site.ascx I set up the datalist public event
with code below

public partial class Site : System.Web.UI.UserControl
{
public delegate void myItemCommand(object source,
DataListCommandEventArgs e);
public event myItemCommand dlSiteItemCommand;

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{ }


protected void dlSite_ItemCommand(object source,
DataListCommandEventArgs e)
{
if (dlSiteItemCommand != null)
dlSiteItemCommand(source, e);
}

protected void Page_Load(object sender, EventArgs e)
{ //get the data and fill the datalist }

in the parent (Detail) I try to bind to the event in the Site.ascx
When the page loads in the parent the procedure LoadUserControl()
is called and in it after I dynamically load the correct control, I
caste the generic control to an instance of Site so I can see the
public event I created. Thats where I have got it wrong I am SOOOOO
confused. The binding does not work when I click the edit button in
the child datalist I never break into the event that is bound on the
parent Below is parent code. Please I not this is too long an
explanation but I have been trying and reading a long time.

Cindy

public partial class Detail : System.Web.UI.Page
{
private string LastLoadedControl
{
get
{
return ViewState["LastLoaded"] as string;
}
set
{
ViewState["LastLoaded"] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;

if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
if (controlPath == "Site.ascx")
{
Site ucSite = (Site)uc;
ucSite.dlSiteItemCommand += new
Site.myItemCommand(ucSite_dlSiteItemCommand);
}
PlaceHolder1.Controls.Add(uc);
}
}

void ucSite_dlSiteItemCommand(object source,
DataListCommandEventArgs e)
{
(source as DataList).EditItemIndex = e.Item.ItemIndex;
(source as DataList).DataSource = dsTunnel;
(source as DataList).DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{}

protected void Page_Load(object sender, EventArgs e)
{
if(!(Page.IsPostBack))
{
string controlPath = null;
controlPath = "Site.ascx";
LastLoadedControl = controlPath;
LoadUserControl();
}
LoadUserControl();
}
private void ts_SelectionChanged(object sender,
JQD.TabStrip.SelectionChangedEventArgs e)
{
int TabPos = e.TabPosition;
ActiveTab = TabPos;
string controlPath = string.Empty;

switch (TabPos)
{
case 0:
controlPath = "Site.ascx";
LastLoadedControl = controlPath;
LoadUserControl();
break;

case 1:
break;
}
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top