Calling Button_click event in dynamically loaded UserControl from the Main Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • axxon
    New Member
    • Jul 2007
    • 6

    Calling Button_click event in dynamically loaded UserControl from the Main Page

    Hi guys! This is my first post here
    I have this really nagging problem which is driving me crazy for 3 days already.Thought I would come here and post the question in case someone can help!

    I have a created a WebUserControl called AddEditPanel. In the usercontrol I have several text fields and one update button, which, when called is supposed to update some record in the database .

    And then I call that usercontrol dynamically from the mother page. The control loads just fine. ONLY PROBLEM IS THAT I CAN NOT FIND ANY WAY TO CATCH THE BUTTON-CLICK EVENT IN THE MOTHER PAGE. NO MATTER WAHT I DO, THE PAGE JUST DOES A POST BACK AND THATS ALL.

    I have searched the net and this forum and other forums as well. But none of them quite addresses the problem that I am facing.

    Pleasee help me out.
    Thanks to all in advance
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Welcome to the scripts developer network. What version of ASP are you using? What else can you tell us about your system set up that might be applicable (what is your development platform etc?)

    Jared

    Comment

    • axxon
      New Member
      • Jul 2007
      • 6

      #3
      Hi I am using ASP.net 2.0, and the latest ajax extention and toolkit from http://ajax.asp.net

      How ever I have already solved the problem..yeyyy! anyway..i wil post the solution here:

      ascx file:

      <%@ Control Language="C#" AutoEventWireup ="true" CodeFile="WebUs erControl.ascx. cs" Inherits="WebUs erControl" %>
      <asp:Button ID="Button1" runat="server" OnClick="Button 1_Click" Text="Button" />

      ascx.cx file:

      using System;
      using System.Data;
      using System.Configur ation;
      using System.Collecti ons;
      using System.Web;
      using System.Web.Secu rity;
      using System.Web.UI;
      using System.Web.UI.W ebControls;
      using System.Web.UI.W ebControls.WebP arts;
      using System.Web.UI.H tmlControls;

      public partial class WebUserControl : System.Web.UI.U serControl
      {


      private void Page_Load(objec t sender, System.EventArg s e)
      {
      Response.Write( "WebUserControl 1 :: Page_Load <BR>");
      }
      public event EventHandler BubbleClick;
      protected void OnBubbleClick(E ventArgs e)
      {
      if (BubbleClick != null)
      {
      BubbleClick(thi s, e);
      }
      }
      public void Button1_Click(o bject sender, System.EventArg s e)
      {
      Response.Write( "WebUserControl 1 :: Begin Button1_Click <BR>");
      // OnBubbleClick(e );
      Response.Write( "WebUserControl 1 :: End Button1_Click <BR>");
      }







      }


      .aspx file which loads the control
      <%@ Page Language="C#" AutoEventWireup ="true" CodeFile="WebFo rm1.aspx.cs" Inherits="WebFo rm1" %>

      <%@ Register Src="WebUserCon trol.ascx" TagName="WebUse rControl" TagPrefix="uc1" %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
      <title>Untitl ed Page</title>
      </head>
      <body>
      <form id="form1" runat="server">
      <div>
      &nbsp;<asp:Plac eHolder ID="PlaceHolder 1" runat="server"> </asp:PlaceHolder >

      </div>
      </form>
      </body>
      </html>

      aspx.cs file:

      using System;
      using System.Data;
      using System.Configur ation;
      using System.Collecti ons;
      using System.Web;
      using System.Web.Secu rity;
      using System.Web.UI;
      using System.Web.UI.W ebControls;
      using System.Web.UI.W ebControls.WebP arts;
      using System.Web.UI.H tmlControls;

      public partial class WebForm1 : System.Web.UI.P age
      {



      private void Page_Load(objec t sender, System.EventArg s e)
      {
      Response.Write( "WebForm1 :: Page_Load <BR>");
      WebUserControl bubble = (WebUserControl )LoadControl("W ebUserControl.a scx");
      PlaceHolder1.Co ntrols.Add(bubb le);
      bubble.BubbleCl ick += new EventHandler(We bForm1_BubbleCl ick);
      }



      private void WebForm1_Bubble Click(object sender, EventArgs e)
      {
      Response.Write( "WebForm1 :: WebForm1_Bubble Click from " +
      sender.GetType( ).ToString() + "<BR>");
      }
      }

      Comment

      • rsdev
        New Member
        • Jul 2007
        • 149

        #4
        Does anyone know why I would get this error with the code from previous post?

        'System.Web.UI. Control' does not contain a definition for 'BubbleClick'

        I am dynamically loading my user control in the OnPreInit event, as suggested somewhere else.

        Comment

        Working...