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.
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.
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.
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.
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:
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
and code behind:
Where can be the problem?
Hi,
First thing to check - in your template, you need to have <form runat="server"> wrapping your macro/usercontrol
-Tom
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.
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?
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.
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.
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.
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.
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
Oh yes, of course this is the problem, I should have noticed it myself. Thank you very much, Tom.
is working on a reply...