2017-03-03 4 views
0

投稿されたデータをHTMLフォームからC#ページに取り出そうとしています。フォームデータをC#で取得したときのパーサーエラー

私は、このエラーが発生しますが:

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コード:

<!--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#コード:

<%@ 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コード:

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) 
     { 

     } 
    } 
} 
+0

同じページに 'C#'と 'VB'コードを書かないことから始めます。 'End If'を削除してください。 – VDWWD

+0

@VDWWD - これを試しましたが、エラーメッセージを停止しません –

+0

あなたは私たちにコードビハインドページを表示していません。 – mason

答えて

1

エラーは、System.Web.UI.Pageから継承していないクラスAss2.CarPageがあることを示しています。見つかったクラスはデザイナーファイルのクラスである可能性があります。これは部分的なクラス定義なので、そこに継承が宣言されていません。

実際のコードビハインドファイルの名前空間とクラスが間違っていたため、選択されていませんでした。 WebApplication2.WebFormからAss2.CarPageに変更することで、部分クラスは同じ時刻を参照しているため、「マージ」され、コードは正しいクラスから継承されるため、すべて動作します。

サイドノートでは、ASPXページからインラインC#コードを取り出し、コードのPage_Loadメソッドに配置する必要があります。コードビハインドが意図されているときに、C#をASPXページに混在させるのは愚かです。

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"); 
      } 
     } 
    } 
} 

また、アクセスする前にフォームの値が存在することを確認する必要があります。しかし、私はそれをあなたのための運動として残します。

関連する問題