Making Polls with Xforms in a usercontrol

XForm is a somewhat of a lazy way out. You know you should do the forms yourself but XForm is simple for the editors and also saves you a lot of job. Anyway, if you want to use XForm to make a poll, you’ll need to make a few adjustments.

Create your new control and add an XForm property to the controls pagetype

Here you can take the template code from EPiServer to get the basic XForm functions.

Create your XForm in EPiServer and assign it to your page. Make sure to let anonymous post and also multiple times (Because all your users will be anonymous and you want anonymous to be able to vote more than once)

Create a cookie so people only vote once (they can delete their cookies and vote over and over so if you want a more secure way, go ahead and do it =D) and also so you can display the results insetad of the poll whenever they visit the page again.

The cookie could look something like this:
[sourcecode lang=”c#”]
private void SetCookie()
var cookie = new HttpCookie(”Poll-” + page.PageLink.ID);
cookie.Values.Add(”voted”, ”true”);
cookie.Expires = DateTime.Now.AddYears(24);
and then you can check for the cookie and give the user different views depending if they voted or not. And that code could look something like this
[sourcecode lang=”c#”]
var cookie = Request.Cookies.Get(”Poll-” + page.PageLink.ID);
if (cookie== null)
If you’re using your XForm directly on a page, then you probably don’t need this step and you’re pretty much set. But if you want to use this as a usercontrol and add the poll with webparts or anything alike you’ll need to do this step.

The thing with EPiServers Statistics is that it will always fetch ”this.PageLink” so if you’re using this as a webpart/control it will try to get the currentpages xform property which will be null. Even if you load the XForm correctly and fetch it from your usercontrol path, the statistics won’t do it. In order to make Statistics work you’ll have to do this:
[sourcecode lang=”c#”]
Statistics.PageLink = page.PageLink;
Statistics.PageGuid = Form.PageGuid;
Otherwise it will take the currentpage pagelink and pageguid. So basicly, you need to set up the Statistics just as you need to set up XForms.

Good luck!


Denna webbplats använder Akismet för att minska skräppost. Lär dig hur din kommentardata bearbetas.