Run user control from template instead of aspx file
Hi all,
I have a contact form user control created in Visual Studio that works when it is run from an aspx file.
I have integrated this into Umbraco and inserted the macro into a template however it does not work. I have already inserted the dll and usercontrol file.
It looks like your template is is masterpage (webforms), but @Umbraco.RenderMacro("ucContact") is Razor which is not supported on your masterpage. Try this:
In the above example you're using Razor and webforms. Sorry I don't understand what type of page you are on? Do you have a masterpage on which you want to render a razor macro? It doesn't work the other way around. So a usercontrol macro won't work on razor because it's not webforms.
Sorry I wasn't clear in my initial post. I have a masterpage which runs a user control but I don't want to use the masterpage/ASPX file.
I want to run the razor macro for a user control from a template/web form instead. However the macro reference does not work. It displays my contact form but it does not process it.
Sorry I'm still a little confused. You don't want the masterpage/aspx file? So you want a Razor file that renders the form? If your form is usercontrol that won't work from Razor. You need a razor form than.
That's right, I don't want the masterpage. I was just showing you what I wanted to change from.
I want to render the user control within a razor template. I have done this before with a simple hello world user control and inserted the macro into the template, I did this following the umbraco videos which demonstrates that it is possible to render user controls from a razor template. So I don't understand why you say that this is not possible?
I also have a MVC contact form which works in Visual studio but I did not know how to integrate this into my umbraco project which uses razor web forms. I dont think you can use both types.
Anyway, here is my user control I would like to use:
That usercontrol won't work. Because when you press the button it can't fire the event because it's not using webforms. So the hello world will work because it doesn't have a postback, but the button won't work. Maybe the Hybrid Framework has some examples you can use.
Run user control from template instead of aspx file
Hi all,
I have a contact form user control created in Visual Studio that works when it is run from an aspx file.
I have integrated this into Umbraco and inserted the macro into a template however it does not work. I have already inserted the dll and usercontrol file.
The aspx file looks like this:
[code]<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Email_Form.Test" %>
<%@ Register TagName="EmailForm" Src="~/Email Form.ascx" TagPrefix="cc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc:EmailForm ID="EmailForm1" runat="server" />
</div>
</form>
</body>
</html>
[/code]
The user control is Email Form.ascx, the code is fine for this. I inserted the macro reference in the usual way:
@Umbraco.RenderMacro("ucContact")
Hope you can help.
Steph
Hello,
It looks like your template is is masterpage (webforms), but @Umbraco.RenderMacro("ucContact") is Razor which is not supported on your masterpage. Try this:
Jeroen
Hi Jeroen,
It does work however it does stop everything else in the template from working, i.e. all of the razor variables are not rendered.
You are right, the above is a masterpage / aspx file, however I would like to run the user control from my template instead. Please see below:
[code]
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "Master.cshtml";
var pageTitle = string.IsNullOrWhiteSpace(CurrentPage.Title)
? CurrentPage.Name
: CurrentPage.Title;
...vars defined here...
}
<div class="container">
<form id="form1" runat="server">
<div>
<umbraco:Macro Alias="ucContact" runat="server"></umbraco:Macro>
</div>
</form>
</div>
[/code]
I have cut a lot of irrelevant stuff out of the template just to show you the reference.
Steph
In the above example you're using Razor and webforms. Sorry I don't understand what type of page you are on? Do you have a masterpage on which you want to render a razor macro? It doesn't work the other way around. So a usercontrol macro won't work on razor because it's not webforms.
Jeroen
Hi Jeroen,
Sorry I wasn't clear in my initial post. I have a masterpage which runs a user control but I don't want to use the masterpage/ASPX file.
I want to run the razor macro for a user control from a template/web form instead. However the macro reference does not work. It displays my contact form but it does not process it.
Regards,
Steph
Sorry I'm still a little confused. You don't want the masterpage/aspx file? So you want a Razor file that renders the form? If your form is usercontrol that won't work from Razor. You need a razor form than.
Maybe this blog can help: http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc
Jeroen
Hi Jeroen,
That's right, I don't want the masterpage. I was just showing you what I wanted to change from.
I want to render the user control within a razor template. I have done this before with a simple hello world user control and inserted the macro into the template, I did this following the umbraco videos which demonstrates that it is possible to render user controls from a razor template. So I don't understand why you say that this is not possible?
I also have a MVC contact form which works in Visual studio but I did not know how to integrate this into my umbraco project which uses razor web forms. I dont think you can use both types.
Anyway, here is my user control I would like to use:
[code]
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Email Form.ascx.cs" Inherits="Email_Form.EmailForm" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net" %>
<script runat="server" type="text/C#">
public void btnSend_Click(object sender, EventArgs e){
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("mail"));
msg.From = new MailAddress("mail");
msg.Subject = txtSubject.Text;
msg.Body = txtName.Text + Environment.NewLine + txtMessage.Value;
SmtpClient client = new SmtpClient();
client.Host = "smtp...";
client.Port = 587; //Your smtp server's port
NetworkCredential cred = new NetworkCredential("mail", "pass");
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);
}
public void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
txtMessage.Value = "";
}
</script>
<asp:Panel runat="server" style="margin-bottom: 32px">
<asp:Label runat="server">Name:</asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter the information requested." ControlToValidate="txtName"></asp:RequiredFieldValidator>
<br />
<asp:Label runat="server">Email:</asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter the information requested." ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server" ErrorMessage="Please enter a valid email address." ControlToValidate="txtEmail" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<br />
<asp:Label runat="server">Subject:</asp:Label>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter the information requested." ControlToValidate="txtSubject"></asp:RequiredFieldValidator>
<br />
<asp:Label runat="server">Message:</asp:Label>
<textarea runat="server" id="txtMessage" rows="7" cols="24"></textarea>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter a message." ControlToValidate="txtMessage"></asp:RequiredFieldValidator>
<br />
<asp:Button runat="server" ID="btnSend" OnClick="btnSend_Click" Text="Send" />
<asp:Button runat="server" ID="btnReset" CausesValidation="false" OnClick="btnReset_Click" Text="Reset" />
</asp:Panel>
[/code]
So all I want to do is call this user control using a macro into my template.
Hello,
That usercontrol won't work. Because when you press the button it can't fire the event because it's not using webforms. So the hello world will work because it doesn't have a postback, but the button won't work. Maybe the Hybrid Framework has some examples you can use.
Jeroen
Hi Jeroen,
Okay thanks for explaining that, I can stop trying to do the impossible.
My options are to easily use the masterpage or try to integrate my MVC project from Visual Studio into umbraco.
Do you know of a good guide that will describe if and how I can use the controller and model files in my umbraco structure?
Steph
Hello,
Try looking at this video: https://www.youtube.com/watch?v=Enni9r0whCE
Jeroen
Hi Jeroen,
That's brilliant. Thanks a lot for your help.
Regards,
Steph
Glad I could help. Could you please mark a post as the solution if it helped you?
Jeroen
is working on a reply...