2017-02-28 67 views
0

從C#開始,ASP.net從PHP嘗試轉換我的代碼。嘗試檢索C#.aspx網頁表單中的表單響應

我目前正在嘗試下面的代碼來檢索表單中發佈的數據。

<%@ 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" then 
header('Location:VolvoHomepage.html');End If 
If Request.Form["Car"] == "Ford" then 
header('Location:FordHomepage.html');End If 
If Request.Form["Car"] == "Mercedes" then 
header('Location:MercedesHomepage.html');End If 
If Request.Form["Car"] == "Audi" then 
header('Location:AudiHomepage.html');End If 
If Request.Form["Car"] == "Vauxhall" then 
header('Location:VauxhallHomepage.html');End If 
%> 

</body> 
</html> 

但我一直收到「/'應用程序中的服務器錯誤。」

任何人都可以幫忙嗎?

+0

你需要給出更多關於錯誤的更多細節。應該有一個完整的堆棧跟蹤。它說什麼?話雖如此,我想這個問題是因爲'header('Location:MercedesHomepage.html');'是PHP語法,而不是.NET。你會想'Response.Redirect(「MercedesHomepage.html」)'我應該想。 – ADyson

回答

1

此行是說,你是在C#語言編碼:

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

但低於你正在使用Visual Basic和PHP語言的混合:

<% If Request.Form["Car"] == "Volvo" then 
header('Location:VolvoHomepage.html');End If 
%> 

在C#代碼以上將是:

<% if (Request.Form["Car"] == "Volvo") { 
    // do your thing 
} 
%> 

但是在Web窗體框架,您應該在aspx文件中聲明「用戶控件」,並將「邏輯」編碼爲aspx.cs文件。你ASPX代碼可能看起來像:

<myUserControls:VolvoHomepage runat="server" id="_ucVolvo" visible="false" /> 
<myUserControls:FordHomepage runat="server" id="_ucFord" visible="false" /> 
... 

您將在相應的用戶控件複製/粘貼HTML的每個代碼文件。

現在(例如)的CarPage.aspx.cs Page_Load方法可以定義如果用戶控件是可見或不可見:

protected void Page_Load(object sender, eventargs e) { 
    if (Request.Form["Car"] == "Volvo") _ucVolvo.Visible = true; 
    else if (Request.Form["Car"] == "Ford") _ucFord.Visible = true; 
} 

是不是更清楚了嗎?

+0

更清楚謝謝 –