无论我做什么,Page_Load都会发射两次

时间:2013-11-24 14:46:24

标签: c# asp.net

我正在开发一个简单的asp.net登录页面。昨天,代码工作正常。今天,事实并非如此。

昨天和今天之间唯一改变的是我今天关闭了我的电脑并重新启动它。

问题是Page_Load正在触发两次(我检查了所有的答案/解决方案,没有一个工作(带有空src的图像,手动处理page_load,将autoEventWireUp设置为false ...))这些似乎没有做到特技。

请有人帮我弄清楚为什么会这样?

以下是页面及其代码的代码:

<%@ Page Title="Login" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="MatchingWebsite.Login" MaintainScrollPositionOnPostback="true" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1><%: Title %></h1>
            </hgroup>
        </div>
    </section>
</asp:Content>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h2 align="center">Looking for someone to spend your time with? Want to have fun with someone you like?<br />You've come to the right place!!</h2><br /><br /><br />
    <img alt="Cupid" src="Images/images.jpg" align="left" />
    <img alt="Couple" align="right" src="Images/matchmaking.jpg" /><br /><br />
    Username:<br />
    <asp:TextBox ID="UserLogIn" runat="server" Width="174px"></asp:TextBox><br /><br />
    Password:<br />
    <asp:TextBox ID="UserPass" runat="server" Width="175px" TextMode="Password"></asp:TextBox><br />        
    <asp:Label ID="LoginError" runat="server" Text="Wrong Username/Password Combination. Try again." Visible="False"></asp:Label><br /><br />
    <asp:Button ID="LoginButton" runat="server" OnClick="Login_Click" Text="Login" Width="81px" />
    <asp:Button ID="NotRegistered" runat="server" Text="Not Registered?"             Width="150px" OnClick="Not_Registered" />
</asp:Content>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .auto-style1
        {
            width: 300px;
            height: 113px;
        }
    </style>
</asp:Content>

以下是代码:

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

namespace MatchingWebsite
{
    public partial class Login : System.Web.UI.Page
    {
        Service1 proxy = new Service1();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"]!=null && !IsPostBack)
                Response.Redirect("~/EnterMyInfo.aspx");
        }
        protected void Login_Click(object sender, EventArgs e)
        {
            string user = UserLogIn.Text;
            string pass = UserPass.Text;
            if (user == "" || pass == "")
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Username/Password cannot be blank.\")</SCRIPT>");
            else
            {
                if (proxy.login_service(user, pass))
                {
                    Response.Redirect("~/EnterMyInfo.aspx");
                }
                else
                    LoginError.Visible = true;
            }
        }

        protected void Not_Registered(object sender, EventArgs e)
        {
            Response.Redirect("~/SignUp.aspx");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

可能网页真的加载了两次。检查您的javascripts(或任何AJAX组件?),或使用浏览器的调试控制台来监控正在发送的网络请求。尝试在PreInit,PostInit(等)事件上添加空方法并设置断点,我敢打赌它们也会被击中两次。