I am an Umbraco noob and have been playing around with making a custom user control / macro but have hit a bit of a brick wall!
Any help appreciated. The control consists of 3 text boxes and a register button.. it all loads up fine on the page but nothing happens when you click the register button.
I did it in VS 2010, placed the dll in the .bin and the .ascx files in usercontrols.
my .ascx contains:
<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="RMRegister.ascx.cs" Inherits="RMRegister" %>
using System; using System.Web.Security; using System.Web.UI; using umbraco; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.member;
public partial class RMRegister : UserControl { // Macro Parameters public int SuccessfulLoginPage { get; set; }
// Member Type & Group private const string MembersType = "SiteMembers"; private const string MembersGroup = "SiteMembers";
// Error format private const string ErrorFormat = "<p class=\"formerror\">{0}</p>";
protected void RegisterPlayer(object sender, EventArgs e) { // Do some server side checks just to be on the safe side if (string.IsNullOrWhiteSpace(tbEmail.Text) | string.IsNullOrWhiteSpace(tbPassword.Text) | string.IsNullOrWhiteSpace(tbUsername.Text)) { litError.Text = string.Format(ErrorFormat, "Please complete all fields"); return; }
// Check the user isn't already registered if (Member.GetMemberFromEmail(tbEmail.Text) == null && Member.GetMemberFromLoginName(tbUsername.Text) == null) { // Set the member type and group var mt = MemberType.GetByAlias(MembersType); var addToMemberGroup = MemberGroup.GetByName(MembersGroup);
//create the member, and set the password and email var m = Member.MakeNew(tbUsername.Text, mt, new User(0)); m.Password = tbPassword.Text; m.Email = tbEmail.Text;
// Add the member to the group m.AddGroup(addToMemberGroup.Id);
//Save member m.Save();
//Generate member Xml Cache m.XmlGenerate(new System.Xml.XmlDocument());
// NOTE: This Is Optional // Login the user FormsAuthentication.SetAuthCookie(tbUsername.Text, false);
// Redirect to successful page (Usually their profile or member page) Response.Redirect(library.NiceUrl(SuccessfulLoginPage)); } else { // Error, member already exists with email or username used litError.Text = string.Format(ErrorFormat, "User already exists"); } } }
Custom User control / Macro woes
I am an Umbraco noob and have been playing around with making a custom user control / macro but have hit a bit of a brick wall!
Any help appreciated.
The control consists of 3 text boxes and a register button.. it all loads up fine on the page but nothing happens when you click the register button.
I did it in VS 2010, placed the dll in the .bin and the .ascx files in usercontrols.
my .ascx contains:
<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="RMRegister.ascx.cs" Inherits="RMRegister" %>
<div id="registersubscriberform">
<p>All items marked with a * are mandatory</p>
<asp:Literal ID="litError" runat="server" />
<div class="formholder">
<div class="formrow">
<div class="formleft"><label for="<%= tbUsername.ClientID %>">Username*</label></div>
<div class="formright">
<asp:TextBox ToolTip="Enter username" ID="tbUsername" runat="server"
ClientIDMode="Static" />
</div>
</div>
<div class="formrow">
<div class="formleft"><label for="<%= tbEmail.ClientID %>">Email*</label></div>
<div class="formright">
<asp:TextBox ToolTip="Enter email" CssClass="required email" ID="tbEmail"
runat="server" ClientIDMode="Static" />
</div>
</div>
<div class="formrow">
<div class="formleft"><label for="<%= tbPassword.ClientID %>">Password*</label></div>
<div class="formright">
<asp:TextBox ToolTip="Enter a password" CssClass="required" ID="tbPassword" TextMode="Password" runat="server" ClientIDMode="Static" />
</div>
</div>
<div class="formrow">
<div class="formleft"></div>
<div class="formright">
<asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="RegisterPlayer" />
</div>
</div>
</div>
</div>
and the .ascx.cs contains:
using System;
using System.Web.Security;
using System.Web.UI;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
public partial class RMRegister : UserControl
{
// Macro Parameters
public int SuccessfulLoginPage { get; set; }
// Member Type & Group
private const string MembersType = "SiteMembers";
private const string MembersGroup = "SiteMembers";
// Error format
private const string ErrorFormat = "<p class=\"formerror\">{0}</p>";
protected void RegisterPlayer(object sender, EventArgs e)
{
// Do some server side checks just to be on the safe side
if (string.IsNullOrWhiteSpace(tbEmail.Text) | string.IsNullOrWhiteSpace(tbPassword.Text) | string.IsNullOrWhiteSpace(tbUsername.Text))
{
litError.Text = string.Format(ErrorFormat, "Please complete all fields");
return;
}
// Check the user isn't already registered
if (Member.GetMemberFromEmail(tbEmail.Text) == null && Member.GetMemberFromLoginName(tbUsername.Text) == null)
{
// Set the member type and group
var mt = MemberType.GetByAlias(MembersType);
var addToMemberGroup = MemberGroup.GetByName(MembersGroup);
//create the member, and set the password and email
var m = Member.MakeNew(tbUsername.Text, mt, new User(0));
m.Password = tbPassword.Text;
m.Email = tbEmail.Text;
// Add the member to the group
m.AddGroup(addToMemberGroup.Id);
//Save member
m.Save();
//Generate member Xml Cache
m.XmlGenerate(new System.Xml.XmlDocument());
// NOTE: This Is Optional
// Login the user
FormsAuthentication.SetAuthCookie(tbUsername.Text, false);
// Redirect to successful page (Usually their profile or member page)
Response.Redirect(library.NiceUrl(SuccessfulLoginPage));
}
else
{
// Error, member already exists with email or username used
litError.Text = string.Format(ErrorFormat, "User already exists");
}
}
}
Hi Matt and welcome to our :)
Is content of the masterpage template where I suppose you drop in your form macro above sorrounded by a <from runat="server"></form> tag?
If not that's most likely the reason why nothing happens.
Looking forward to hearing more from you.
/Jan
Thanks for such a quick response... I must admit I am a noob so will prolly make a few school boy errors as I get used to it..
I am a convert from DNN and so far love it.... especially for in my case amazing speed compared to other CMS's I used..
Anyway thanks I will check this !
Hmm.. checked this and could not see a 'form runat=server' tag anywhere in my master template.
I added the tags around my macro in the HTML editor under content but still no joy !!
Or should I not do it this way ??? Thanks
Hi Matt
Happy to hear you love using Umbraco :)
Hmm, could you perhaps post the code of your master-template?
Cheers,
Jan
Here ya go.. its the master template from The Standard MVC starter kit
The page standard template which in turn uses this master template..
Thanks for your patience!
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
var homepage = Model.Content.AncestorOrSelf(1);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>@Model.Content.GetPropertyValue("title")</title>
<meta name="keywords" content="@Model.Content.GetPropertyValue("keywords")" />
<meta name="description" content="@Model.Content.GetPropertyValue("description")" />
<meta http-equiv="Content-Language" content="en" />
<meta name="language" content="en" />
<meta name="robots" content="all, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="MSSmartTagsPreventParsing" content="TRUE" />
<link rel="stylesheet" href="/css/base-min.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="/css/default.css" type="text/css" media="screen, projection" />
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
</head>
<body>
<div id="header">
<div class="page-width">
@if (homepage.HasProperty("headerNavigation"))
{
<ul class="navigation fc">
@foreach (var item in Umbraco.TypedContent(homepage.GetPropertyValue("headerNavigation").ToString().Split(',')))
{
<li><a href="@item.NiceUrl()">@item.Name</a></li>
}
</ul>
}
<a href="/"><img src="/images/logo.png" alt="Koiak Basic Site" /></a>
@if(Umbraco.MemberIsLoggedOn())
{
<p class="fr" id="loggedIn">
Welcome @Context.User.Identity.Name, <a href="/login.aspx?signout=true">logout</a>
</p>
}
<form action="/search" id="searchForm" method="get">
<fieldset>
<label for="search" class="remove">Search Site</label>
<input type="text" value="@Request.QueryString["search"]" class="input" name="search" id="search"/>
<input type="submit" value="Search" class="submit"/>
</fieldset>
</form>
</div>
<div class="navigation">
@if (homepage.HasProperty("primaryNavigation"))
{
<ul class="navigation fc">
@foreach (var item in Umbraco.TypedContent(homepage.GetPropertyValue<string>("primaryNavigation").Split(',')))
{
var selected = Array.IndexOf(Model.Content.Path.Split(','), item.Id.ToString()) >= 0 && item.Level > 1 ? " class=\"selected\"" : "";
<[email protected](selected)><a href="@item.NiceUrl()">@item.Name</a></li>
}
</ul>
}
</div>
</div>
<div id="body" class="page-width">
@RenderBody()
</div>
<div id="footer" class="fc">
<div class="page-width">
@Html.Partial("Affiliations",homepage)
<p>@homepage.GetPropertyValue("address")</p>
<p>@Html.Raw(homepage.GetPropertyValue<string>("copyright"))</p>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.timers.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="/scripts/default.js"></script>
</body>
</html>
Hi Matt - you're welcome, this IS a friendly community you know :)
And shame on me for not figuring this on out before...you're running MVC mode but you're making a user control for Webforms mode...
I think you should check out the Creative Website Starter package by Warren Buckley instead. It's based on Webforms and not MVC.
You can change from MVC to Webforms in the /config/umbracoSettings.config in <templatemode> (easier to just search the file for MVC).
However since the package you're currently using is made for MVC only this is not going to help you - just thought I'd mention the option.
/Jan
Makes sense... I will switch to the webforms version.. thank you for your help!
Already received far more help on here than I ever did on DNN forums e.t.c..
Regards,
Matt
Hi Matt
You're very welcome. Don't hesitate to ask further questions if there is anything you're in doubt about.
Keep in mind others might benefit from it as well :)
/Jan
is working on a reply...