0

更新:我找到了这个错误的原因,我无法在 masterpage 中创建表格布局,为什么?

错误 1 ​​当前上下文中不存在名称“txtUsername”

源代码:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="Login.aspx.cs" Inherits="login" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <form id="form1" runat="server">
    <div>
         Username: <asp:TextBox ID="TextBox1" runat="server" /><br>
        Password:<asp:TextBox ID="TextBox2" runat="server" /><br>
        <asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="Login" /><br>
        <asp:Label ID="Label1" runat="server" Text="Please login" />
    </div>
    </form>
</asp:Content>

更新:补充源代码

这是后面的代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;


public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FormsAuthentication.Authenticate(txtUsername.Text, txtPassword.Text))
        {
            lblStatus.Text = ("Welcome " + txtUsername.Text);
            FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
        }
        else
        {
            lblStatus.Text = "Invalid login!";
        }
    }
}

这是母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>
 <style>
  body {
   margin:0;
   padding:0;
  }
</style>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>


        <asp:Table ID="Table1" width="100%" runat="server">
            <asp:TableRow ID="TableRow1" runat="server">
                <asp:TableCell ID="TableCell1" runat="server" BackColor="Blue" ColumnSpan="3">Header</asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow2" runat="server">
                <asp:TableCell ID="TableCell2" runat="server" Width="160">
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
                </asp:TableCell>
                <asp:TableCell ID="TableCell3" runat="server" Width="800" BackColor="Red" >col2</asp:TableCell>
                <asp:TableCell ID="TableCell4" runat="server" Width="160" ColumnSpan="3">col3</asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow3" runat="server">

                <asp:TableCell ID="TableCell5" runat="server">Footer</asp:TableCell>

            </asp:TableRow>
        </asp:Table>

</body>
</html>

后面的代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
4

0 回答 0