Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > dynamic loaded ascx delegate event not firing in parent form

Reply
Thread Tools

dynamic loaded ascx delegate event not firing in parent form

 
 
CMELLO@tams.com
Guest
Posts: n/a
 
      09-07-2008
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;
}
 
Reply With Quote
 
 
 
 
Evan Freeman
Guest
Posts: n/a
 
      09-12-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://evanfreeman.blogspot.com


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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;
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script in AJAX-loaded page inside parent page not firing pbd22 Javascript 2 09-11-2009 08:38 PM
dynamic loaded datalist ascx event delegate not firing in parent f cindy ASP .Net 0 09-09-2008 04:16 PM
Dynamic loaded control event delegate does not fire on server cindy ASP .Net Web Controls 0 09-08-2008 03:28 AM
click event is not firing in ascx file within web form sanju ASP .Net 2 06-17-2004 08:48 PM
datasource of child .ascx not visible to parent .ascx Joe ASP .Net Web Controls 1 02-16-2004 07:27 AM



Advertisments