Why the event from my button is not working?

Hi Experts!
I have  a Web User Control(WebUserControl.ascx) that contained a Panel(Panel1) and inside this panel there is a button(Button1) that have a click event.
Also, I have a Page in which I am trying to create  the control " collapsible Panel"  programatically(this control is Ajax).
I am trying to set the panel(Panel1) from my Web User Control inside the Collapsible panel, which I have done it.
The Problem: The  click event from the button(Button1)  inside the WebuserControl is not triggering at all.
Thinking about the Problem: I think that the problem is in this line:
                                              cpe1.TargetControlID = myControl.StepPanel.UniqueID;

Somehow the right ID has not being called or something like this, I tried the .ClientID and appear this error:
Exception Details: System.InvalidOperationException: The TargetControlID of 'CollapsiblePanelExtender' is not valid. A control with ID 'MyControl1_ContentPanelStep' could not be found

Thanks for your help!! :)


This is the web user control .ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Exampletry.ascx.cs" Inherits="controls_Exampletry" %>
<asp:Panel ID="ContentPanelStep" runat="server" CssClass="collapsePanel" BackColor="#EAE3CF">
<table>
 <tr>
   <td>
    <asp:Button ID="Button1" runat="server" Text="Testing" OnClick="Button1_Click" />   
   </td>
 </tr>
</table>
</asp:Panel>
 
This is the web user control ascx.cs:
public partial class controls_Exampletry : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string buttonID = this.UniqueID.Replace(this.ID, Button1.UniqueID);
        //Button1.ID = this.UniqueID + Button1.UniqueID;
        //Button1.ID = "TestButtonID1";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
              //this is the part is not triggering
    }
 
    public Panel StepPanel
    {
        get { return ContentPanelStep; }  //this is my panel and inside this panel there is a button
    }
}
 
 
This is my code in my page:
    protected void Page_Load(object sender, EventArgs e)
    {
        
        controls_Exampletry myControl = (controls_Exampletry)Page.LoadControl("controls/Exampletry.ascx");
        myControl.ID = "MyControl1";
 
        controls_NewHireWorkflowStepControl myHeaderControl = (controls_NewHireWorkflowStepControl)Page.LoadControl("controls/NewHireWorkflowStepControl.ascx");
        myHeaderControl.ID = "MyHeaderControl1";
        //panel1.Controls.Add(myControl);  
 
 
        HtmlTableRow stepRow = new HtmlTableRow();
        HtmlTableCell stepCell = new HtmlTableCell();
        
 
        AjaxControlToolkit.CollapsiblePanelExtender cpe1 = new AjaxControlToolkit.CollapsiblePanelExtender();
        cpe1 = new AjaxControlToolkit.CollapsiblePanelExtender();
        cpe1.ID = "CollapsiblePanelExtender";
        cpe1.TargetControlID = myControl.StepPanel.UniqueID;
        cpe1.BehaviorID = "CPEBehavior";        
        cpe1.Collapsed = true;
        cpe1.ExpandControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        cpe1.CollapseControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        cpe1.AutoCollapse = false;
        cpe1.AutoExpand = false;
        cpe1.ScrollContents = false;
        cpe1.ExpandDirection = AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical;
 
        stepCell.Controls.Add(myHeaderControl);
        stepCell.Controls.Add(myControl);
        stepCell.Controls.Add(cpe1);
        stepRow.Cells.Add(stepCell);
        StepsTable.Rows.Add(stepRow);
 
 
    }

Open in new window

LVL 4
eddyperuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShazbotOKCommented:
Ok if your PAGE is attempting to capture the event from the User Control then you need to create event delegates in the user control then attach the event handler to the events from the PAGE code.
On a side-note I noticed your using the AJAX toolkit, be sure you have a defined scriptmanager in the markup AND update panels/triggers configured.

//in the ASCX code
 
public event CommandEventHandler MyButtonClicked;
//in the HTML Markup set the OnClick to the below method inside the ASCX
public void MyButton_Click(object sender, EventArgs e){MyButtonClicked(sender,e);}
 
//in the ASPX.cs Page_Load code
MyUserControl.MyButtonClicked += new CommandEventHandler(MyButtonClick);
 
//new method
void MyButtonClick(object sender, EventArgs e)
{
  //do your stuff here
}

Open in new window

0
eddyperuAuthor Commented:
There is an error with the object MybutonCliecked(sender, e)
I am ataching my code....
..Thanks for your help

ps: Why Do I need "update panels/triggers configured"? I've  the script manager in my html code but not the update panel, Do I need it and where suppose I need to set it up.Thanks
My web usercontro.ascx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class controls_Exampletry : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public event CommandEventHandler MyButtonClicked;
    public void MyButton_Click(object sender, EventArgs e) { MyButtonClicked(sender, e); }
 
 
    public Panel StepPanel
    {
        get { return ContentPanelStep; } 
    }
 
    void MyButtonClick(object sender, EventArgs e)
    {
        //do your stuff here
    }
}
 
My .ascx from my web user control:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Exampletry.ascx.cs" Inherits="controls_Exampletry" %>
<asp:Panel ID="ContentPanelStep" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Testing" OnClick="MyButton_Click" />   
</asp:Panel>
 
My ASPX.cs Page:
public partial class Insidemasterpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        HtmlTableRow stepRow = new HtmlTableRow();
        HtmlTableCell stepCell = new HtmlTableCell();
 
        controls_Exampletry myControl = (controls_Exampletry)Page.LoadControl("controls/Exampletry.ascx");
 
        myControl.MyButtonClicked += new CommandEventHandler(MyButtonClick);
 
 
 
        myControl.ID = "MyControl1";
 
        //controls_NewHireWorkflowStepControl myHeaderControl = (controls_NewHireWorkflowStepControl)Page.LoadControl("controls/NewHireWorkflowStepControl.ascx");
       // myHeaderControl.ID = "MyHeaderControl1";
        //panel1.Controls.Add(myControl);  
 
 
 
 
 
        AjaxControlToolkit.CollapsiblePanelExtender cpe1 = new AjaxControlToolkit.CollapsiblePanelExtender(); cpe1.ID = "CollapsiblePanelExtender";        
        cpe1.TargetControlID = myControl.StepPanel.ClientID;
        cpe1.SuppressPostBack = false;
 
        // cpe1.ExpandControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        //cpe1.CollapseControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        //cpe1.BehaviorID = "CPEBehavior";        
        // cpe1.Collapsed = true;
        //cpe1.AutoCollapse = false;
        //cpe1.AutoExpand = false;
        //cpe1.ScrollContents = false;
        //cpe1.ExpandDirection = AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical;
 
 
       // stepCell.Controls.Add(myHeaderControl);
        stepCell.Controls.Add(myControl);
        stepCell.Controls.Add(cpe1);
        stepRow.Cells.Add(stepCell);
        StepsTable.Rows.Add(stepRow);
}
}

Open in new window

0
eddyperuAuthor Commented:
Well, first I don't need the event handler because I ma loading a complet WEb USer control inside a control, but Thanks anyway
Second I found the answer it have to do with the order how I was loadin and I forgot to load it in the control: here it is:
        controls_Exampletry myControl = (controls_Exampletry)Page.LoadControl("controls/Exampletry.ascx");
        myControl.ID = "MyControl1";
        controls_NewHireWorkflowStepControl myHeaderControl = (controls_NewHireWorkflowStepControl)Page.LoadControl("controls/NewHireWorkflowStepControl.ascx");
        myHeaderControl.ID = "MyHeaderControl1";


        Page.Controls.Add(myControl);
        AjaxControlToolkit.CollapsiblePanelExtender cpe1 = new AjaxControlToolkit.CollapsiblePanelExtender();
        cpe1.ID = "myCollapsiblePanelExtender";
        // cpe1.TargetControlID = Page.FindControl(myControl.ID).StepPanel.ID;
        cpe1.TargetControlID = myControl.StepPanel.UniqueID;
        cpe1.ExpandControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        cpe1.CollapseControlID = myHeaderControl.StepHeaderPanel.UniqueID;
        cpe1.BehaviorID = "CPEBehavior";
        cpe1.Collapsed = true;
        cpe1.AutoCollapse = false;
        cpe1.AutoExpand = false;
        cpe1.ScrollContents = false;
        cpe1.ExpandDirection = AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical;



        Page.Controls.Add(cpe1);

        HtmlTableRow stepRow = new HtmlTableRow();
        HtmlTableCell stepCell = new HtmlTableCell();
        stepCell.Controls.Add(myHeaderControl);
        stepCell.Controls.Add(myControl);
        stepCell.Controls.Add(cpe1);
        stepRow.Cells.Add(stepCell);
        StepsTable.Rows.Add(stepRow);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.