Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Zakhar 171 posts 397 karma points
    Jun 27, 2012 @ 17:45
    Zakhar
    0

    Button click is not working in usercontrol Umbraco

    I started to build a website using Umbraco and I noticed that button click events (and probably other events) are not working.

    I created simplest usercontrol with one button, added it to a page, When I debug it the Page_Load is called (breakpoint being hit), but not button click.

    The code is very standard, but here it is:

    .aspx file

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestControl.ascx.cs" Inherits="usercontrols_TestControl" %>

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    and code behind:

    protected void Button1_Click(object sender, EventArgs e) {
       
    Label1.Text = "Button clicked!";
    }

    Where can be the problem?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 27, 2012 @ 17:58
    Tom Fulton
    0

    Hi,

    First thing to check - in your template, you need to have <form runat="server"> wrapping your macro/usercontrol

    -Tom

  • Zakhar 171 posts 397 karma points
    Jun 27, 2012 @ 18:08
    Zakhar
    0

    Hi Tom, thanks, yes I do have this tag in my Main.master, I have it inside my body tag so all content goes in it, something like this:

    ...

    <body>

    <form runat="server">

    ...

    </form>

    </body>

    In fact I can't even add a the usercontrol to a page without it as I get this exception straight away:

    Control 'ContentPlaceHolderDefault_Content_TestControl_2_Button1' of type 'Button' must be placed inside a form tag with runat=server.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 27, 2012 @ 18:13
    Tom Fulton
    0

    Hmm, odd, it should be working...

    How are you calling the usercontrol from the template?  Is it wrapped in a macro or are you calling directly?  Can you paste the code?

  • Zakhar 171 posts 397 karma points
    Jun 27, 2012 @ 18:23
    Zakhar
    0

    Yes, I use macro as usual,

    <umbraco:Macro Alias="TestMacro" runat="server" />

    In fact it was working earlier today, I was playing with membership provider and members, it suddenly stopped working. I can't figure out what I changed that could break it.

  • Zakhar 171 posts 397 karma points
    Jun 27, 2012 @ 20:20
    Zakhar
    0

    Ok, I solved part of the problem. I was adding <form runat="server"> to my parent template and adding macro to child template. After I moved <form runat="server"> to the child template and wrapped it around my macro it started to work.

    Why it happens? Is it possible to add it once to master (parent) template. I'm sure I saw it working in another project.

  • skiltz 501 posts 701 karma points
    Jun 27, 2012 @ 22:02
    skiltz
    0

    Yes definetly possible.  I  always put my form tag on the master.  You probably need to post your usercrontol and master pages so we can see.  I presume you have copied across your compiled DLL.

  • Zakhar 171 posts 397 karma points
    Jun 28, 2012 @ 19:50
    Zakhar
    0

    It's became even more interesting, I can add <form runat="server"> to Main template but it has to be some specific place in the template.


    So here is my Main template (I removed html to make it shorter):


    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
    <!doctype html>
    <html dir="ltr" lang="en" class="no-js">
    <head>
      ...
      <asp:contentplaceholder id="Head" runat="server"></asp:contentplaceholder>
    </head>
    <body>
    <!-- begin markup -->
      <!--doesn't work if I place it here above Header macro-->
       <umbraco:Macro Alias="Header" runat="server" />
       <form runat="server">  <!--everything work if I place it here-->
      <asp:contentplaceholder id="Content" runat="server"></asp:contentplaceholder>
    ...
          <umbraco:Macro Alias="MainNavigation" runat="server" />
    ...
       <asp:contentplaceholder id="PageBottom" runat="server"></asp:contentplaceholder>
    </form>
    </body>
    </html>
    </asp:Content>


    and here is my child template where I place macro with button:


    <%@ Master Language="C#" MasterPageFile="~/masterpages/Main.master" AutoEventWireup="true" %>
    <asp:Content ContentPlaceHolderID="Content" runat="server">
       <umbraco:Macro Alias="TestMacro" runat="server" />
    </asp:Content>


    It looks like my Header macro prevents button click event from working, If i place <form runat="server"> above it it doesn't work, if below everything starts to work. Weird because it is mainly html which has one simle razor condition, here it is:


    <header id="header" @if(@Model.NodeTypeAlias != "Homepage") { <text>class="sub"</text> }>
      <div class="content">
        <h1 id="logo"><a href="/">Site name</a></h1>

        <form action="" method="" id="search">
          <p><input type="text" placeholder="search" /><input type="submit" value="submit" /></p>
        </form>

        <strong id="lang">
          <img src="/img/flag.jpg" alt="English - UK" />
          <em>English</em>
          <a href="#">Change</a>
        </strong>
      </div>
    </header><!-- e: header -->


    Any ideas?


    p.s. Sorry for the lack of formatting, don't know how to do it on this forum.


  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 28, 2012 @ 20:39
    Tom Fulton
    0

    Hi,

    I think it's because your Header macro has it's own <form> tag in it, and it's not valid to have nested <form> tags.

    So I think you'll need to move the <form runat="server"> after the header macro, or remove the <form> element from your header macro.

    -Tom

  • Zakhar 171 posts 397 karma points
    Jun 29, 2012 @ 10:27
    Zakhar
    0

    Oh yes, of course this is the problem, I should have noticed it myself. Thank you very much, Tom.

Please Sign in or register to post replies

Write your reply to:

Draft