An ASPX view is created in an ASP. NET MVC Project

Source: Internet
Author: User

First, add a view from the controller.

The view engine is selected as ASPX (C #) and is not selected using the layout or template page.

In the Views\eav directory, the generated ASPX is a separate page with no code files, so the code is also written in this file.

@ 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 ("

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

for (i = 1; I <= + +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= "></"ASP :listbox>

</div>

</form>

</body>

</html>

So you can use the design interface.

The ASPX (C #) View engine generates only one ASPX file by default, all written in a file is not a good idea, can it be like the traditional ASPX code separation structure? The answer is YES!

Add a Controller:eavcontroller, and still follow the above method to add the view.

Then right-click in the EAV directory to add a new item.

Select Web Form, name that must be index.

Well, we need to show up. But running is going to be an error drip.

Then you need to change it, open the code file Index.aspx.cs, plus a reference to a namespace: Using the System.web.mvc;index Page class inherits from the System.Web.UI.Page is not possible, changed to System.Web.Mvc.ViewPage. ViewPage is a subclass of page.

------Index.aspx.cs File ------

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 ("

For (int i = 1; I <= + +i)

{

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

}

}

}

}

------index.aspx File ------

@ 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= "></"ASP :ListBox>

</div>

</form>

</body>

</html>

Look at the running effect:

There is a note that because MVC handles requests instead of files, it does not have the status and life cycle of the ASP page, so you cannot perform the post loopback method.

For example, add a TextBox and button to the ASPX page:

<asp:textbox id= "txttest" runat= "Server" text= "Hello"/>

&nbsp&nbsp
<asp:button id= "btnsubmit" text= "change textbox text" runat= "server" onclick= "Submit"/>

In the code file:

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

It is simple to press this button to make the text of the textbox into world, but you will find no effect, the textbox is always hello. Because this is not an ASPX page, it is a form of http://localhost:51927/eav.

finally: ASP. NET MVC strongly recommends the Razor engine, this article only introduces a method, in the MVC is not able to install a Web form.

Examples of successful use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.