ASP.NET MVC項目裡建立一個aspx視圖

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   檔案   io   

先從控制器裡添加視圖

視圖引擎選"ASPX(C#)",使用布局或模板頁不要選。

在Views\EAV目錄裡,產生的aspx是個單獨的頁面,沒有代碼檔案,所以代碼也要寫在這個檔案裡。

@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>"

  

@ Import Namespace="System" @ Import Namespace="System.Data"

@ Import Namespace="System.Web.UI"

@ Import Namespace="System.IO"

@ Import Namespace="System.Web.UI.WebControls"

  

<script type="text/C#" runat="Server">

protected void Page_Load(object sender, EventArgs e)

{

int i;

Response.Write("<h2>Page_Load</h2><br/>");

Calendar1.SelectedDate = new DateTime(2013, 1, 1);

for (i = 1; i <= 100; ++i)

{

lstRec.Items.Add(string.Format("Item{0:00}", i));

}

}

</script>

<!DOCTYPE html>

  

<html>

<head runat="server">

<title>Index</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

<br />

<asp:listbox ID="lstRec" runat="server" Width="150"></asp:listbox>

</div>

</form>

</body>

</html>

  

這樣就可以使用設計介面了

  

ASPX(C#)視圖引擎預設只產生一個aspx檔案,都寫在一個檔案裡畢竟不是好主意,能不能像傳統的aspx的代碼分離結構呢?答案是可以的!

先加一個Controller:EAVController,仍然按上面的辦法加視圖。

  

  

然後在EAV目錄右單擊,添加建立項。

  

選Web表單,名稱那肯定是index啦。

好,我們需要的出現了。但是運行是要出錯滴。

那 就需要改一下了,開啟代碼檔案index.aspx.cs,加一個命名空間的引用:using System.Web.Mvc;index頁面類繼承於System.Web.UI.Page是不行的,改成 System.Web.Mvc.ViewPage。ViewPage是Page的子類。

  

------index.aspx.cs檔案------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

  

namespace FirstMvc.Views.EAV

{

    public partial class index : System.Web.Mvc.ViewPage

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            Response.Write("<h2>EAV Page_Load</h2><hr/><br/>");

            for (int i = 1; i <= 100; ++i)

            {

                lstRec.Items.Add(string.Format("Item{0:00}", i));

            }

        }

    }

}

  

  

------index.aspx檔案------

@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="FirstMvc.Views.EAV.index"

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:ListBox ID="lstRec" runat="server" Width="150"></asp:ListBox>

</div>

</form>

</body>

</html>

  

看看運行效果:

 

有個注意的地方,因為mvc處理的請求而不是檔案,它沒有asp頁面的狀態和生命週期,所以不能執行post回送的方法。

例如,在aspx頁面裡加一個TextBox和Button:

<asp:TextBox ID="txtTest" runat="server" Text="Hello" />

&nbsp&nbsp
<asp:Button ID="btnsubmit" Text="改變TextBox文本" runat="server" OnClick="submit" />

在代碼檔案裡:

protected void submit(object sender, EventArgs e)
  {
    txtTest.Text = "World";
  }

很簡單,就是按下這個按鈕使TextBox的文本變成World,但是你會發現沒有效果,TextBox裡總是Hello。因為這個訪問的不是aspx頁面,而是http://localhost:51927/eav的形式。

最後:asp.net mvc強烈推薦Razor引擎,本文只是介紹一種方法,在mvc裡裝個web表單也是沒辦法的事情。

使用過成功的例子。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.