?
Solved

Back Slashes break search aspx.cs

Posted on 2011-10-03
2
Medium Priority
?
269 Views
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";
        base.OnPreInit(e);
    }

    protected string SearchText
    {
        get
        {
            if (ViewState["SearchText"] == null)
                return "";
            else
                return (string)ViewState["SearchText"];
        }
        set
        {
            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();
                if(SearchText!=string.Empty)
                    Search();
            }
        }
    }

    private void Search()
    {
        DataTable dtbSearchResults = Business.Search.SearchCompanyName(SearchText);
        repSearchResults.DataSource = dtbSearchResults;
        repSearchResults.DataBind();
        if (repSearchResults.Items.Count == 0)
            repSearchResults.Visible = false;
        else
            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

0
Comment
Question by:HKFuey
2 Comments
 
LVL 11

Accepted Solution

by:
f_o_o_k_y earned 1500 total points
ID: 36904135
Hi
Shouldn't you use MasterPageFile  Insted WombatMasterPage or   base.OnPreInit(e); before setting it?


Based on article: http://odetocode.com/code/450.aspx

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">
</asp:Content>
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)
    {
        base.OnPreInit(e);

        PagesSection pagesConfig =
             ConfigurationManager.GetSection("system.web/pages")
                 as PagesSection;

        MasterPageFile = pagesConfig.MasterPageFile;
    }
}




0
 

Author Closing Comment

by:HKFuey
ID: 36929159
Hi FOOKY, thanks for the detailed response
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 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