Blog posts

  • Regenerate aspx.designer.cs file

    October 26, 2010 | Tags: ASP.NET, Troubleshooting
    Seems like the connection between your asp and asp.designer.cs file has stopped working? Here's a two step guide how to regenerate it.

    Within the Visual Studio:

    1) Remove your aspx.designer.cs file

    2) Right click on your aspx file and select "Convert to Web Application"

    This should add the aspx.designer.cs back and up to date.

    If you get an error saying:
    "Generation of designer file failed: The method or operation is not implemented."

    Try close Visual Studio and then reopen your project and do step number two again.

  • EPiServer: How to upload a file to the pagefiles folder

    October 22, 2010 | Tags: EPiServer, C#, Snippet
    Here's a function to save an uploaded file into an EPiServer page virtual pagefile folder. You might want to check that your UploadFile-control has a file before calling this function (using the HasFile-property on the control).

    /// <summary>
    /// Save an uploaded file into the virtual pagefiles folder for an EPiServer page
    /// </summary>
    /// <param name="fileUpload">The FileUpload-controller that is used to upload the file</param>
    /// <param name="page">The page for which you want to save the file to</param>
    /// <returns>Path to the uploaded file</returns>
    private string SaveFile(FileUpload fileUpload, PageData page) {
    //Get the page folder ID
    int folderId = (int)page.Property["PageFolderID"].Value;

    //Get the root directory for page folders
    string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;

    VersioningDirectory pageRootDirectory = (VersioningDirectory)GenericHostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath);

    //Combine the root path with folder name and get directory
    string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));

    UnifiedDirectory pageDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;

    //Bypass security and Create page folder if needed
    if (pageDirectory == null) {
    pageRootDirectory.BypassAccessCheck = true;
    pageDirectory = pageRootDirectory.CreateSubdirectory(folderId.ToString(), page);

    // Get the filename from the upload control
    string fileName = fileUpload.FileName;

    // Create a file handle
    UnifiedFile file = pageDirectory.CreateFile(fileName);

    // Save the content
    using (Stream stream = file.Open(FileMode.Create, FileAccess.Write)) {
    stream.Write(fileUpload.FileBytes, 0, fileUpload.FileBytes.Length);

    // Return the path to the uploaded file
    return VirtualPathUtilityEx.Combine(pageDirectory.VirtualPath, fileName); ;

    Thanks goes to Fredrik Haglund for the great information about how to create the pagefiles folder from code.

  • DropDownList empty after postback

    October 22, 2010 | Tags: ASP.NET, C#, Troubleshooting
    Getting an empty DropDownList after postback and although you selected a value, DropDownList.SelectedIndex is -1.

    This is most likely the case of the viewstate being turned off. If you manually populate the list, there's a workaround without using the viewstate. This is accomplished by understanding the event order.

    Simplified it works as follows:
    • Init - page is initiliazed
    • ProcessPostData - handles the data posted to the page
    • Load - page is loaded and ready to access

    If your DropDownList doesn't contain any values when ProcessPostData is reached it will ignore the posted values while recreating your DropDownList-control.

    The trick is to always populate your DropDownList at Page_Init like:
    protected void Page_Init(object sender, EventArgs e) {

    Now you will have be able to read the selected in your Page_Load or event handler!

  • EPiServer: Null values for saved but unpublished page

    October 21, 2010 | Tags: EPiServer, Troubleshooting
    If you save a new page in EPiServer (ran into this issue using CMS 5, don't know which other versions it affect) without publishing it and then get the new page, you might expect that you will get the property values as they where set when saved. A fair assumption, but instead you'll get null values.

    Consider the following:
    // Create a new page of page type id 7 under page id 100 
    PageData page = DataFactory.Instance.GetDefaultPageData(new PageReference(100), 9);

    // Set values
    page.PageName = "My new page";
    page.Property["MyDateProperty"].Value = new DateTime(2010, 10, 23);

    // Save the new page
    DataFactory.Instance.Save(page, SaveAction.Save, AccessLevel.NoAccess);

    // Get the newly created page (we assumes its page id)
    PageData page2 = DataFactory.Instance.GetPage(new PageReference(101));

    // Get the property
    object test = page.Property["MyDateProperty"].Value;

    The variable test will be null. The solution is to use another constructor for creating the PageReference.

    This code will work:
    // Get the newly created page
    PageData page2 = DataFactory.Instance.GetPage(new PageReference(101, true));

    // Get the property
    object test = page.Property["MyDateProperty"].Value;

    This is because just a PageReference(pageId) won't retrieve the latest working page (if you check the WorkingPageId is 0). By using PageReference(pageId, bool anyVersion) you ask for the latest working page version (the version that was saved most recently). This will get you a version of the page with all the property values just like when you saved it.

  • Google Search Engine Optimization Starter Guide gets updated

    October 06, 2010 | Tags: SEO, Google
    Google has released an updated version of their Search Engine Optimization (SEO) Starter Guide. It's a great overview for learning what you can do to increase visibility and ranking on searchengines, like Google. Don't expect miracles, but it's a good read :)

    Here's a link to the Google Search Engine Optimization Starter Guide in PDF form.