Web表单和带有Master Page的Web表单有什么区别?

6

我是新手网页应用开发者。我目前使用的是Visual Studio 2013,并且我已经创建了一个网站应用程序。我希望有一个表单,让员工填写,当他们点击提交按钮时,表单将添加、更改或删除数据库中的信息。通过研究,我最初尝试制作Web表单。这样做不允许我保留主页面上的格式,当我试图将其连接到主页面时,由于<form>标签的原因,无法实现连接。之后,我创建了一个带有主页面的Web表单。这使我可以保留主页面上的格式,但现在我无法使用<form>标记创建表单。所以我的问题是,如何创建一个既能够提交表单,又能保留主页面格式的表单?

主页面:

 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> Employee Information</title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />

</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <!--<a class="navbar-brand" runat="server" href="~/">Employee Information</a>-->
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a runat="server" href="~/HomePage">Home</a></li>
                        <!--<li><a runat="server" href="~/About">About</a></li>-->
                        <li><a runat="server" href="~/EmployeeInput">Add Employee</a></li>
                        <li><a runat="server" href="~/EditEmployee">Edit Information</a></li>
                        <li><a runat="server" href="~/Terminate">Terminate Employee</a></li>
                    </ul>
                    <asp:LoginView runat="server" ViewStateMode="Disabled">
                        <AnonymousTemplate>
                            <ul class="nav navbar-nav navbar-right">
                                <li><a runat="server" href="~/Account/Register">Register</a></li>
                                <li><a runat="server" href="~/Account/Login">Log in</a></li>
                            </ul>
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <ul class="nav navbar-nav navbar-right">
                                <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %> !</a></li>
                                <li>
                                    <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                                </li>
                            </ul>
                        </LoggedInTemplate>
                    </asp:LoginView>
                </div>
            </div>
        </div>
        <div class="container body-content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
            <hr />
            <footer>
                <p>&copy; <%: DateTime.Now.Year %> </p>
            </footer>
        </div>
    </form>
</body>
</html>

Web表单

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div id="Column1" style="height:355px;width:250px;float:left;">
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name: <input type="text" name="FirstName" />
        First Name:&nbsp; <input type="text" name="FirstName1" size="20" />
        <select id="Select1" name="D1">
            <option>This</option>
            <option>That</option>
            <option>The other thing</option>
        </select></div>
    <div id="Column2" style="height:355px;width:250px;float:left;">
        First Name: <input type="text" name="FirstName2" size="20" />
        First Name: <input type="text" name="FirstName3" size="20" />
        First Name: <input type="text" name="FirstName4" size="20" />
        First Name: <input type="text" name="FirstName5" size="20" />
        First Name: <input type="text" name="FirstName6" size="20" />
        First Name: <input type="text" name="FirstName7" size="20" />
        First Name: <input type="text" name="FirstName8" size="20" />
        First Name: <input type="text" name="FirstName9" size="20" />
        First Name: <input type="text" name="FirstName10" size="20" />
    </div>
    </body>
</html>

我希望我的网页表单中的所有内容都能成为表单。

你能展示一下你的母版页和网页表单吗?通常你只需要确保有一个类似这样的表单标签:<form runat="server" id="form_1"></form>。更多关于ASP.net表单的信息。 - Mivaweb
2个回答

7
您在组合页面(页面+主控页)中只能有一个带有“runat =“server””属性的表单。
解决方法是将表单放在主控页中,并将内容占位符放在表单内。页面本身不必仅具有内容并声明标记。当页面被执行时,来自页面和主控页的标记将被合并成一个单一的HTML文件发送到客户端。
示例主控页:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>

<!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>Working with Data Tutorials</title>
    <link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="wrapper">

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

            <div id="header">
                <span class="title">Working with Data Tutorials</span>
                <span class="breadcrumb">TODO: Breadcrumb will go here...</span>
            </div>

            <div id="content">
                <asp:contentplaceholder id="MainContent" runat="server">
                  <!-- Page-specific content will go here... -->
                </asp:contentplaceholder>
            </div>

            <div id="navigation">
                TODO: Menu will go here...
            </div>
        </form>
    </div>
</body>
</html>

示例内容页面:

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Home" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h1>Welcome to the Working with Data Tutorial Site</h1>

    <p>This site is being built as part of a set of tutorials that illustrate some of the new data access and databinding features in ASP.NET 2.0 and Visual Web Developer.</p>

<asp:TextBox runat="server" id="TextBox1" /><br />
<asp:TextBox runat="server" id="TextBox2" /><br />
<asp:Button runat="server" id="Btn1" OnClick="Btn1_Click" Text="Click to submit!" />

        </asp:Content>

在页面的代码后端(.cs文件)中,您可以通过以下方式读取文本框中的值:

protected void Btn1_Click(object sender, EventArgs e)
{
    string sometext=TextBox1.Text;
    string somemoretext=TextBox2.Text;
}

示例来自微软开发者网络


好的,我有一个设置为此示例的内容页面。但是,我怎样在其上创建一个表单呢?内容页面不允许使用 '<form>' ,因此我无法为提交按钮放置 action 语句。action 语句也要放在主页面中吗?如果是的话,您能为我演示在哪里放置吗? - Guy Hendrickson
@user3730892 你所说的表单动作语句是什么意思?你是指一个按钮来引起后台回发吗? - mason
就像我之前说的,我对这个非常新。从我到目前为止所读的内容来看,如果我想将一组输入框中的任何内容发布到数据库(如SQL Server),我必须将所有输入框封装在'<form>'中,例如'<form action="something"> <input boxes> <input submit></form>'。 - Guy Hendrickson
@user3730892,我已经更新了我的示例,包括一些文本框和一个按钮,以及在按钮被点击后在服务器端读取该数据所使用的代码。 - mason
那么'runat="server"'是指向我连接的任何数据库吗? - Guy Hendrickson
@user3730892 不是的。Runat server 使控件在服务器端运行,生成的内容将作为 HTML 发送到客户端。在您的 C# 代码中,您需要与数据库进行交互。这些都是基础知识。您应该观看一些 YouTube 视频或阅读一些 ASP.NET 开发方面的书籍,因为 Stack Overflow 不适合教授整个架构。 - mason

0
两者是相同的,但如果你使用 Master Page 创建 Webform,Visual Studio 会隐式地添加 Master Page 的引用。
<%@ Page Title="" Language="C#" MasterPageFile="~/My.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="project.WebForm1" %>

如果您创建了一个Web表单,但没有添加主页的引用,即使主页已经存在。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="project.Bhand" %>

已经有一个解决 OP 问题的方案了。请先看一下那个方案,然后考虑一下你的答案是否添加了一些必要的信息。 - L. Guthardt
这是一个简短的回答! - Kashif Faraz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接