1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Event in the ascx file

Event in the ascx file

4 posts, 0 answered
  1. Developer
    Developer avatar
    10 posts
    Registered:
    02 Aug 2007
    16 Aug 2007
    Link to this post
    Hi everyone:

    I used the ascx file as  template when I develop the pluggable module.
    and there are some events in the ascx file ,
    eg:   <asp:Button ID="btnSearch" Runat="server" OnClick="btnSearch_Click" Text="Find Products" CssClass="button" />

    How  can I  make  this button work in the pluggable module?

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    16 Aug 2007
    Link to this post
    Hi Developer,

    You should wire the events in CreateChildControls method of the composite control using the corresponding template. For example:

    btnSearch.Click += new EventHandler(btnSearch_Click);


    The event handlers (btnSearch_Click) should be implemented in the same server control also. I am recommending you a very useful article, demonstrating the main steps for creating composite controls: http://msdn2.microsoft.com/en-us/library/3257x3ea.aspx

    Hope this helps.

    Sincerely yours,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Developer
    Developer avatar
    10 posts
    Registered:
    02 Aug 2007
    20 Aug 2007
    Link to this post
    Hi Pepi,
    Thank you for your help.
    I also had some problem when I created this control.
    The ascx template file content is :
    <div id="shoppingCart">
       
                            <asp:button  runat="server" ID="btnFilterVouchers" CssClass="button" OnClick="btnFilterVouchers_Click"  text="View Vouchers"/>
                     
                        <asp:GridView ID="grdVouchers" AllowSorting="true"
    OnRowCommand="grdVouchers_RowCommand"
     OnSorting="grdVouchers_Sorting" 

    AllowPaging="true" PageSize="20" OnPageIndexChanging="grdVouchers_PageIndexChanging" runat="server" AutoGenerateColumns="false" CssClass="ProjectGrid" CellPadding="5">                   
                        </asp:GridView>
                    </td>
                </tr>
        </table>
    </div>                                     

    The createChildControls method is :
       protected override void CreateChildControls()
            {
                this.container = new VoucherContainer(this);

                if (this.template == null)
                {
                    if (this.Page != null)
                    {
                        string path = String.IsNullOrEmpty( TemplatePath) ? Manager.Provider.VouchersTemplate : TemplatePath;
                        if (File.Exists(this.Page.MapPath(path)))
                            this.template = this.Page.LoadTemplate(path);
                        else
                            this.template = new DefaultVoucherTemplate();
                    }
                    this.template.InstantiateIn(container);
                }

                this.container.BtnFilterVouchers.Click += new EventHandler(btnFilterVouchers_Click);

                this.container.GrdVouchers.Sorting += new GridViewSortEventHandler(grdVouchers_Sorting);
                this.container.GrdVouchers.RowDataBound += new GridViewRowEventHandler(grdVouchers_RowDataBound);
                this.container.GrdVouchers.RowCommand += new GridViewCommandEventHandler(grdVouchers_RowCommand);
                this.container.GrdVouchers.PageIndexChanging += new GridViewPageEventHandler(grdVouchers_PageIndexChanging);

                this.Controls.Add(container);
            }


    but when I tried to run the website there was an error displayed:
    Compiler Error Message: BC30456: 'btnFilterVouchers_Click' is not a
    member of 'ASP.sitefinity_admin_controltemplates_vouchers_ascx'.

    How can I fix this??
    Cheers

  4. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    20 Aug 2007
    Link to this post

    Hello Developer,

    The problem is that you are defining event handlers in an ascx file, which is not compiled as part of the control. It would be OK if it was a user control, but it is not. You are using the ascx file only to get the needed markup. The error you get is because the compiler is trying to locate a partial class in a codebehind file, containing the handlers and it doesn't find one.
    The solution, as Pepi pointed out, is to bind the handlers in the C# code for your composite control, and not in the ascx.
    So instead of doing it like this:

    <asp:button  runat="server" ID="btnFilterVouchers" CssClass="button" OnClick="btnFilterVouchers_Click"  text="View Vouchers"/>

    do it in the CreateChildControls method of you C# file:

    btnFilterVouchers.Click += new EventHandler(btnFilerVouchers_Click);

    Then, you just write the btnFilerVouchers_Click() method and everything should work when you remove the OnClick attribute from the markup. Also, do not use design time support in Visual Studio, because it assumes you are creating a user control.

    You can take a look at this blog post: http://blogs.sitefinity.com/Ivan/Post/07-05-21/building_a_module_for_sitefinity_part_6_anatomy_of_a_web_control.aspx. There is a project available for download. In the project, the ControlPanel.cs control is implemented and commented, so take a look at how the event handlers are done there.

    Best wishes,

    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Register for webinar
4 posts, 0 answered