2015-02-17 66 views
0

我在這裏學習ASP.NET,這就是爲什麼我顯然在這裏問一個問題。所以當我運行<% %>分隔符內的聲明時,一切正常。我嘗試在<script runat="server">中運行它,但它不起作用。我只是好奇這兩種方法之間的重大差異。我想使用腳本方法,但它只適用於<% %><script runat =「server」> vs <% %>

我的例子是這個...基本標準格式從POST方法獲取「userInput」(一個數字)。

 <form action="calcprime.aspx" method="post"> 
      Enter a number between 1-999: 
      <input type="text" name="userInput" maxlength="3" /> 
      <input type="submit" name="submit" value="Submit" /> 
     </form> 

然後,如果我需要將字符串轉換爲整數做就可以了數學我會做到這一點...

<%@ Page Language="C#" %> 

<script runat="server"> 
    int num = int.Parse(Request.Form["userInput"]); 
</script> 

<%=num%> // <-should display the number in theory..(in my head) 

不幸的是,上面的代碼錯誤,我不工作,但是替代使用只有<% %>使用相同的代碼完全相同的方法工作100%罰款。如下...

<%@ Page Language="C#" %> 

<% 
    int num = int.Parse(Request.Form["userInput"]); 
    Response.Write(num); //displays the number as it should.. 100% working 
%> 

所以我的問題是。爲什麼腳本方式不起作用?這不是一回事嗎?在ASP.NET中使用C#處理這個基本場景的正確方法是什麼?我的方法是否切合實際,還是有我應該注意的標準?這個數字會對它進行數學運算,這就是爲什麼我需要將它轉換爲整數。這只是一個有點基本的基礎性的東西,我覺得我應該知道在學習不好的實踐之前去了解它的正確方法。

+0

你從哪裏學習?你走了。除此之外,「ASP.NET C#」不是一個事物的名字。有ASP.NET,可以用C#,VB.NET或其他.NET語言編程。 – 2015-02-17 23:58:48

+0

看看這個問題的答案是否有幫助,http://stackoverflow.com/questions/957284/whats-the-deal – jac 2015-02-18 00:03:56

+1

我很欣賞這種迴應。然而,告訴我,我「走出去」並不能幫助我。我很想知道如何改進它。請發佈標準的正確方法。你還怎麼從ASP.NET中的表單獲取GET和POST數據?我來自使用PHP ..如果您能評論您的評論,我會很樂意。謝謝你,先生! – Corey 2015-02-18 00:25:26

回答

2
<script runat="server"> 
    int num = 1; 
</script> 

<%=num%> 

在我的機器上正常工作。我在我的頁面上看到了1

然而,這不起作用:

<script runat="server"> 
    int num = int.Parse(Request.Form["userInput"]); 
</script> 

<%=num%> 

我得到這個錯誤:

CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'

我懷疑你明白我的錯誤太多,但沒有包括在你的問題。 注意如果您的代碼中出現錯誤,請將其包含在您的問題中。不要以爲我們知道你有錯誤。

這工作,假設我適當地添加查詢字符串請求URL:

<script runat="server"> 
    int num = int.Parse(HttpContext.Current.Request.QueryString["randomnum"].ToString()); 
</script> 

<%=num%> 

我懷疑這將工作過,假設你已經提交的表單值的頁面。但是,你的答案並不完整,所以我不知道你是否做到了。只是表明,你需要提交一個Minimal, Verifiable, and Complete example

<script runat="server"> 
    int num = int.Parse(HttpContext.Current.Request.Form["userInput"]); 
</script> 

<%=num%> 

最終然而,你可能不應該做的內嵌代碼塊在頁面上(無論是使用腳本標記或內嵌式)。這在代碼背後處理得更好。 <% %>在某些情況下,東西沒有問題,但通常只應用它來爲頁面注入一個值,例如當您在中繼器中評估某些東西時。如果你發現自己在做很多這些內聯表達式,你可能會考慮切換到ASP.NET MVC或Web頁面。這兩種都使用Razor view engine,這更清潔。

+0

謝謝你的合法回覆。我得到同樣的錯誤是的。非常感激。 – Corey 2015-02-18 08:38:54

2

將您的.aspx文件轉換爲.cs文件(即,純粹的C#)由幕後的ASP.NET編譯器(獨立於IDE中的C#編譯器)。如果您在計算機上找到「Temporary ASP.NET Files」文件夾,則可以看到這些臨時的.cs文件。

這些.cs文件中的類有一個很大的函數,稱爲Render,它是輸出到響應流(使用Response.Write)的函數。您的.aspx文件中的所有靜態HTML都將轉換爲String實例,並直接送入Response.Write

<% %>塊被轉換爲內聯C#代碼,用於分解Render函數中的這些大規模String實例。

<script runat="server">塊被粘貼爲此運行時生成的類中的類成員。

<asp:Foo runat="server">元素被轉換爲Control實例化和呈現調用。

所以這樣的:

<%@ Page Inherits="ParentPageClass" %> 
<html> 
<head> 
    <script runat="server"> 
    String DoSomething() { 
     return "lulz"; 
    } 
    </script> 
</head> 
<body> 
    <% int x = 5; %> 
    <%= x %> 
    <div> 
     <asp:Button runat="server" /> 
    </div> 
</body> 
</html> 

被轉換成這樣:

(我已經通過去除多餘的跟蹤調用簡化這個例子中的#line文本是特殊的預處理指令,因此YSOD系統可以映射運行。錯誤返回到您的.aspx文件)。

namespace ASP { 

    [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()] 
    public class webform1_aspx : ParentPageClass, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler { 

     String DoSomething() { 
      return "lulz"; 
     } 

     [System.Diagnostics.DebuggerNonUserCodeAttribute()] 
     private global::System.Web.UI.WebControls.HyperLink @__BuildControl__control2() { 
      global::System.Web.UI.WebControls.HyperLink @__ctrl; 


      @__ctrl = new global::System.Web.UI.WebControls.HyperLink(); 

      @__ctrl.ApplyStyleSheetSkin(this); 

      @__ctrl.Text = "WebControls are evil"; 
     } 

     [System.Diagnostics.DebuggerNonUserCodeAttribute()] 
     private void @__BuildControlTree(webform1_aspx @__ctrl) { 

      this.InitializeCulture(); 

      global::System.Web.UI.WebControls.HyperLink @__ctrl1; 

      @__ctrl1 = [email protected]__BuildControl__control2(); 

      System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl)); 

      @__parser.AddParsedSubObject(@__ctrl1); 

      @__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod([email protected]__Render__control1)); 
     } 

     private void @__Render__control1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) { 

      @__w.Write("\r\n </head>\r\n <body>\r\n  "); 


      #line 11 "c:\users\dai\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\WebForm1.aspx" 
      int x = 5; 

      #line default 
      #line hidden 

      #line 12 "c:\users\dai\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\WebForm1.aspx" 
      @__w.Write(x); 


      #line default 
      #line hidden 

      @__w.Write("\r\n  <div>\r\n   "); 

      parameterContainer.Controls[0].RenderControl(@__w); 

      @__w.Write("\r\n  </div>\r\n </body>\r\n </html>"); 
     } 
    } 
}