如何使用ascx或其他等价物?

时间:2011-12-22 18:19:03

标签: asp.net asp.net-mvc view

如何在我的asp.net mvc 2.0项目中的每个页面插入登录视图? 我已经“窃取”了这个控制器,并从MS visual studio 2010的非空项目中查看。如何在每个视图上插入登录表单? 我需要在php中使用这样的东西:include。什么是asp.net mvc? ASCX ??? 这是main.master:

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="header">
        <img src="/Content/image/img_06.jpg" width="320" height="192" /></div>
    <div class="naviheader">
        <div class="navl">
        </div>
        <a href="/Home" class="navitext">На главную</a> <a href="/Shedule" class="navitext">
            Расписание</a> <a href="/Home/About" class="navitext">О нас</a> <a href="/Admin"
                class="navitext">АдминПанель</a>
    </div>
    <div id="middle">
        <div id="container">
            <div id="content">
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
            </div>
            <!-- #content-->
        </div>
        <!-- #container-->
        <div class="sidebar" id="sideLeft">
            <asp:ContentPlaceHolder ID="Login" runat="server">
                <% Html.RenderPartial("Login"); %>
            </asp:ContentPlaceHolder>
        </div>
</body>
</html>

我必须插入的地方和内容? 这是登录视图(aspx):

  <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<TrainShedule.Models.LogOnModel>" %>

<asp:Content ID="loginTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Log On
</asp:Content>

<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Log On</h2>
    <p>
        Введите свой логин и пароль:
    </p>

    <% using (Html.BeginForm()) { %>
        <%= Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
        <div>
            <fieldset>
                <legend>Ввод данных:</legend>

                <div class="editor-label">
                    <%= Html.LabelFor(m => m.UserName) %>
                </div>
                <div class="editor-field">
                    <%= Html.TextBoxFor(m => m.UserName) %>
                    <%= Html.ValidationMessageFor(m => m.UserName) %>
                </div>

                <div class="editor-label">
                    <%= Html.LabelFor(m => m.Password) %>
                </div>
                <div class="editor-field">
                    <%= Html.PasswordFor(m => m.Password) %>
                    <%= Html.ValidationMessageFor(m => m.Password) %>
                </div>

                <div class="editor-label">
                    <%= Html.CheckBoxFor(m => m.RememberMe) %>
                    <%= Html.LabelFor(m => m.RememberMe) %>
                </div>

                <p>
                    <input type="submit" value="Log On" />
                </p>
            </fieldset>
        </div>
    <% } %>
</asp:Content>

2 个答案:

答案 0 :(得分:2)

首先,您需要将登录视图页面(aspx)更改为用户控件(ascx)。这不应该太难;通过一些调整来复制和粘贴应该这样做。

获得控件后,您可以将其添加到母版页,它将显示在使用母版页的所有页面上。有两种方法可以将控件添加到页面中。

  1. 将UserControl拖到设计器图面上 enter image description here

  2. Html.RenderUserControl enter image description here

  3. 以下链接将有很多帮助。 asp.net mvc usercontrols start to finish

答案 1 :(得分:0)

使用部分视图(如.ascx)并使用布局(如母版页)。