显示“TextBox”的ASP.NET登录页面必须放在带有runat = server

时间:2015-12-13 11:01:55

标签: asp.net web

以下是完整的代码

1)是主页面aspx 2)是webform页面aspx 3)是webform C#文件

我想的完整细节

现在这个ASP:内容已经是一个表单了,当我添加表单标签时,它显示页面只能有表格。请问有人可以帮忙吗?

母版页

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>

<form id="form1" runat="server">
    <asp:Label ID="eliblogin" runat="server" Text="Login"></asp:Label>
</form>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">



</asp:ContentPlaceHolder>

</body>
</html>

masterpage.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
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)
 {

    Page.LoadComplete += new EventHandler(set_button);
 }  
 protected void eliblogout_Click(object sender, EventArgs e)
 {
    var user = Context.User.Identity;

    //authentication manager
     var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

    //logout 
    authenticationManager.SignOut();
    Response.Redirect("~/Index.aspx");
   }
 private void set_button(Object sender,EventArgs e)
 {
        var user = Context.User.Identity;
         if (user.Name!=null && user.IsAuthenticated)
         {
             try
             { 
                 //Username.Text = user.Name;
                 eliblogin.Visible = true;
                 eliblogin.Text = "yes man you did it";
              //   eliblogout.Visible = true;
             }
             catch (Exception ex)
             {
                //Username.Text = ex.ToString();
             }

         }
         else 
         {
            // Username.Text = "Hello Guest";
             eliblogin.Visible = true;
           //  eliblogout.Visible = false;

         }
    }
  }

的Login.aspx

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

 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"    Runat="Server">
 <asp:Label ID="liStatus" runat="server"></asp:Label>
 <br />
 <br />
 <asp:Label ID="Label1" runat="server" Text="username"></asp:Label><br />
 <br />
 <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
 <br />
 <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label><br />
 <br />
 <asp:TextBox ID="txtPassword" runat="server" TextMode="Password">   </asp:TextBox>
  <br />
  <br />
  <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
 </asp:Content>

login.aspx.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;

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

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //User Store
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            userStore.Context.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["elibraryConnectionString"].ConnectionString;

            //User Manager
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

            //Create a new User
            var user = manager.Find(txtUsername.Text,txtPassword.Text);
            if (user!=null)
            {
                try
                {
                    //Create user Object
                    //Database will be created Automatically
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user,DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties
                    {
                    IsPersistent=false
                },userIdentity);

                }
                catch (Exception ex)
                {
                    liStatus.Text = ex.ToString();
                }
            }
            else
            {
                liStatus.Text = "invalid username or password";
            }
        }
        catch (Exception ex)
        {
            liStatus.Text = ex.ToString();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你应该移动

<form id="form1" runat="server">
    <asp:Label ID="eliblogin" runat="server" Text="Login"></asp:Label>
</form>

从母版页到login.aspx页面,将登录页面的所有asp内容放入此表单标签。