Back Slashes break search aspx.cs

Posted on 2011-10-03
Last Modified: 2012-05-12
If a user searches our web site for a product with more than one slash e.g. 123/456/1
They get this error: -
Server Error in '/' Application.

Content controls are allowed only in content page that references a master page.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Content controls are allowed only in content page that references a master page.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Content controls are allowed only in content page that references a master page.]
   System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8672243
   System.Web.UI.Page.get_Master() +51
   System.Web.UI.Page.ApplyMasterPage() +15
   System.Web.UI.Page.PerformPreInit() +45
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +282

I have inserted the code that I think does the search, please be patient as I'm not familiar with this stuff.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class SearchResults : Wombat.Business.Page
    protected override void OnPreInit(EventArgs e)
        WombatMasterPage = "content.master";

    protected string SearchText
            if (ViewState["SearchText"] == null)
                return "";
                return (string)ViewState["SearchText"];
            ViewState["SearchText"] = value;

    protected void Page_Load(object sender, EventArgs e)
        ((HtmlHead)this.Master.FindControl("oHead")).Title = "CompanyName - Search";
        if (!IsPostBack)
            if (Request.QueryString["SearchText"] != null)
                SearchText = Request.QueryString["SearchText"].ToString();

    private void Search()
        DataTable dtbSearchResults = Business.Search.SearchCompanyName(SearchText);
        repSearchResults.DataSource = dtbSearchResults;
        if (repSearchResults.Items.Count == 0)
            repSearchResults.Visible = false;
            repSearchResults.Visible = true;
        lbResults.Text = "You searched for <span style=\"color:red;\">" + SearchText + "</span> and it returned " + repSearchResults.Items.Count.ToString() + " results";

Open in new window

Question by:HKFuey
    LVL 11

    Accepted Solution

    Shouldn't you use MasterPageFile  Insted WombatMasterPage or   base.OnPreInit(e); before setting it?

    Based on article:

    If we attempt to view this content page in design view, Visual Studio will produce the error message shown earlier. If we really want to use the designer with our content page, we can leave the MasterPageFile attribute empty, like in the following code:

    <%@ Page Language="VB" MasterPageFile=""  %>
    <asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" Runat="Server">
    We can’t just drop the MasterPageFile attribute from the @ Page directive, because the designer will raise a different error (“Content controls are allowed only in content page that references a master page”). The empty attribute appears to trick the designer into allowing us into design mode.

    At runtime, however, the page will throw an exception because it doesn’t have a master file. We can avoid the exception by programmatically setting the MasterPageFile property at runtime. We know we will need to set the master page before or during the PreInit event. The following code reads the masterPageFile attribute from the <pages> section of web.config. By putting the code into a base class, we can cover all the content pages in an application.

    using System;
    using System.Web.UI;
    using System.Web.Configuration;
    using System.Configuration;

    public class BaseContentPage : Page
        protected override void OnPreInit(EventArgs e)

            PagesSection pagesConfig =
                     as PagesSection;

            MasterPageFile = pagesConfig.MasterPageFile;


    Author Closing Comment

    Hi FOOKY, thanks for the detailed response

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now