0
votes

Trying to retrieve posted data from HTML form into C# page.

However I am receiving this error:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: 'Ass2.CarPage' is not allowed here because it does not extend class 'System.Web.UI.Page'.

Source Error:

Line 1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs" Inherits="Ass2.CarPage"%>

Line 2:

Line 3:

HTML Code:

<!--Car Search Form-->
        <div id="Search_Form">
            <form name="Car Search" action="CarPage.aspx" method="post">
                <h1 align="center">Search For A Car Now: </h1>
                <h2 align="center">
                    <select name="Car">
                        <option value="Volvo">Volvo</option>
                        <option value="Ford">Ford</option>
                        <option value="Mercedes">Mercedes</option>
                        <option value="Audi">Audi</option>
                        <option value="Vauxhall">Vauxhall</option>
                    </select>
                    <h1 align="center">
                        <input type="Submit" value="Submit">
                    </h1>
            </form>
        </div>

C# Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs"  Inherits="Ass2.CarPage"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

<%     
if (Request.Form["Car"] == "Volvo") 
{
    Response.Redirect("VolvoHomepage.html");
}

if (Request.Form["Car"] == "Ford") 
{
    Response.Redirect("FordHomepage.html");
}
if (Request.Form["Car"] == "Mercedes") 
{
    Response.Redirect("MercedesHomepage.html");
}
if (Request.Form["Car"] == "Audi") 
{
    Response.Redirect("AudiHomepage.html");
}
if (Request.Form["Car"] == "Vauxhall") 
{
Response.Redirect("VauxhallHomepage.html");
}
%>  

</body>
</html>

ASPX.CS Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
1
Start with not writing C# and VB code on the same page. Remove the End If.VDWWD
@VDWWD - tried this but it still doesnt stop the error mesasgeJamie Obrien
You haven't shown us the code behind page.mason
@JamieObrien What do you mean "empty"? Is there absolutely no text at all in your .aspx.cs page?mason
Change the namespace of your code behind from WebApplication to Ass2 and the class name from WebForm1 to CarPage. Does that resolve issue?mason

1 Answers

1
votes

The error indicates that you have a class Ass2.CarPage that is not inheriting from System.Web.UI.Page. The class that it found was likely the class in the designer file, which is only a partial class definition and so doesn't have the inheritance declared there.

The actual code behind file had the wrong namespace and class, so it wasn't picked up. By changing it from WebApplication2.WebForm to Ass2.CarPage the partial classes now are referring to the same time and thus get "merged", and since your code behind inherits from the correct class, it all works.

On a side note, you should take the inline C# code out of of your ASPX page and put it in the Page_Load method of your code behind. It's silly to mix C# into the ASPX page when that's what the code behind is intended for.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["Car"] == "Volvo") 
            {
                Response.Redirect("VolvoHomepage.html");
            }

            else if (Request.Form["Car"] == "Ford") 
            {
                Response.Redirect("FordHomepage.html");
            }
            else if (Request.Form["Car"] == "Mercedes") 
            {
                Response.Redirect("MercedesHomepage.html");
            }
            else if (Request.Form["Car"] == "Audi") 
            {
                Response.Redirect("AudiHomepage.html");
            }
            else if (Request.Form["Car"] == "Vauxhall") 
            {
                Response.Redirect("VauxhallHomepage.html");
            }
        }
    }
}

Also, you should probably make sure the form values exist before accessing them. But I'll leave that as an exercise for you.