asp.net幽灵般的页面加载

时间:2009-08-17 13:13:18

标签: asp.net events

除此之外,我的网站项目中有两个页面。 Default.aspx和TeacherControlPanel.aspx。

用户在Default.aspx中提供了他的凭据,创建了一个cookie然后他将Server.Transfer()编辑为TeacherControlPanel.aspx。

TeacherControlPanel.aspx有一个注销按钮和另一个名为“发送邮件”的按钮。

如果用户按下注销按钮,则会删除cookie并将其重定向到Default.aspx。

如果用户在没有注销的情况下关闭浏览器,当他打开Default.aspx页面时,他会自动重定向到TeacherControlPanel.aspx,因为cookie就在那里。

现在,一切正常,除了我按下TeacherControlPanel.aspx上的“发送邮件”按钮,然后加载Default.aspx然后加载TeacherControlPanel.aspx但是没有执行Button事件处理程序。

为什么Default.aspx再次加载以及Button事件发生了什么?

<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">

    <table>
        <tr>
        <td><asp:HyperLink ID="homePageHyperlink" runat="server" NavigateUrl="~/Default.aspx">Home</asp:HyperLink></td>        
        <td rowspan="5">&nbsp;<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder></td>
        </tr>
        <tr>
        <td><asp:HyperLink ID="studentControlPanelHyperlink" runat="server">Student</asp:HyperLink></td>        
        </tr>
        <tr>
        <td></td>        
        </tr>
        <tr>
        <td></td>  
        </tr>
        <tr>
        <td></td>   
        </tr>
    </table>    
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string st = "";
    }
}


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table style="width: 253px; height: 118px">
        <tr>
            <td style="width: 54px">
            </td>
            <td colspan="2">
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 54px">
                <asp:Label ID="Label1" runat="server" Text="Username :"></asp:Label></td>
            <td colspan="2">
                <asp:TextBox ID="usernameTextBox" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 54px">
                <asp:Label ID="Label2" runat="server" Text="Passord  :"></asp:Label></td>
            <td colspan="2">
                <asp:TextBox ID="passwordTextBox" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 54px">
            </td>
            <td colspan="2">
                <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login" /></td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:Label ID="labLoginMessage" runat="server" Font-Bold="True" Font-Names="Verdana"
                    Font-Size="Small" ForeColor="#C00000" Text="Label" Width="226px"></asp:Label></td>
        </tr>
    </table>
</asp:Content>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Ice_Web_Portal.BO;
using Ice_Web_Portal.ASP.NET.Utils;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AspNetUtil util = new AspNetUtil(this);

        util.DisposeCookie("user");

        UserTypeEnum userType = EnumUtility.ConvertToEnum(util.GetCookieValue("user", "usertype"));
        string username = util.GetCookieValue("user", "username");

        if (userType == UserTypeEnum.Student)
        {
            Server.Transfer("~/Student/StudentControlPanel.aspx?username=" + username);
        }
        else if (userType == UserTypeEnum.Teacher)
        {
            Server.Transfer("~/Teacher/TeacherControlPanel.aspx?username=" + username);
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string username = this.usernameTextBox.Text;
        string password = this.passwordTextBox.Text;

        bool success = Ice_Web_Portal.BO.User.LogIn(username, password);

        if (success)
        {
            Ice_Web_Portal.BO.User user = Ice_Web_Portal.BO.User.GetUserByUserName(username);

            Ice_Web_Portal.BO.UserTypeEnum loginUserType = user.UserTypeEnum;

            if (loginUserType == UserTypeEnum.Student)
            {
                AspNetUtil util = new AspNetUtil(this);
                util.SaveInCookie("user", "username", username, 3600);
                util.SaveInCookie("user", "usertype", "Student", 3600);

                Server.Transfer("~/Student/StudentControlPanel.aspx?username=" + username);
            }
            else if (loginUserType == UserTypeEnum.Teacher)
            {
                AspNetUtil util = new AspNetUtil(this);
                util.SaveInCookie("user", "username", username, 3600);
                util.SaveInCookie("user", "usertype", "Teacher", 3600);

                Server.Transfer("~/Teacher/TeacherControlPanel.aspx?username=" + username);                
            }
            else
            {
                labLoginMessage.Text = "Sorry! Type of user couldn't be determined!";
            }
        }
        else
        {
            labLoginMessage.Text = Ice_Web_Portal.BO.User.LoginMessage;
        }
    }
}

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="TeacherControlPanel.aspx.cs" Inherits="Teacher_TeacherControlPanel" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table style="width: 346px">
        <tr>
            <td>
                <asp:Label ID="labErrorMessage" runat="server" Font-Bold="True" Font-Names="Verdana"
                    Font-Size="Small" ForeColor="#C00000" Text="Error Message"></asp:Label></td>
            <td>
                </td>
            <td>
                </td>
            <td>
                </td>
        </tr>
        <tr>
            <td>
                Teacher Control Panel</td>
            <td>
                Mails</td>
            <td>
                Notices</td>
            <td>
                Uploads</td>
        </tr>
        <tr>
            <td rowspan="3">
                <table style="width: 134px">
                    <tr>
                        <td>
                            Username:</td>
                        <td>
                            <asp:Label ID="labUsername" runat="server" Text="labUsername"></asp:Label></td>
                        <td>
                            Teacher Code:
                        </td>
                        <td style="width: 3px">
                            <asp:Label ID="labTeacherCode" runat="server" Text="labTeacherCode"></asp:Label></td>

                    </tr>
                    <tr>
                        <td>
                            Name :</td>
                        <td>
                            <asp:Label ID="labName" runat="server" Text="labName"></asp:Label></td>
                        <td>
                            Department</td>
                        <td style="width: 3px">
                            <asp:Label ID="labDepartment" runat="server" Text="labDepartment"></asp:Label></td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td style="width: 3px">
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td style="width: 3px">
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <asp:Button ID="btnSendMail" runat="server" Height="24px" Text="Send Mail" Width="130px" OnClick="btnSendMail_Click" PostBackUrl="~/Teacher/TeacherControlPanel.aspx" /></td>
            <td>
                <asp:Button ID="btnSubmitNewNotice" runat="server" Height="24px" Text="Submit New Notice"
                    Width="130px" /></td>
            <td>
                <asp:Button ID="btnViewUploads" runat="server" Height="24px" Text="ViewUploads" Width="130px" /></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnViewOldMails" runat="server" Text="View Old Mails" OnClick="btnViewOldMails_Click" /></td>
            <td>
                <asp:Button ID="btnViewOldNotices" runat="server" Height="24px" Text="View Old Notices"
                    Width="130px" /></td>
            <td>
                <asp:Button ID="btnViewDefaulters" runat="server" Height="24px" Text="View Defaulters"
                    Width="130px" /></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnReceivedMails" runat="server" Height="24px" Text="Received Mails"
                    Width="130px" /></td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td colspan="4" rowspan="1">
                <asp:GridView ID="UploadsGridView1" runat="server">
                </asp:GridView>
            </td>
        </tr>
    </table>
</asp:Content>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Ice_Web_Portal.ASP.NET.Utils;
using Ice_Web_Portal.BO;

public partial class Teacher_TeacherControlPanel : System.Web.UI.Page
{
    string username = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            username = (string)Request.QueryString["username"];

            Teacher teacher = Teacher.GetTeacherByUsername(username);

            if (teacher != null)
            {
                labUsername.Text = username;
                labName.Text = teacher.TeacherName;
                labTeacherCode.Text = teacher.TeacherCode;

                Dept dept = teacher.Department;

                if (dept != null)
                {
                    labDepartment.Text = dept.DeptName;
                }
            }
            else
            {
                labErrorMessage.Text = "No teacher found";
            }
        }
    }

    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        //try
        {
            Server.Transfer(@"~/Teacher/TeacherSendMail.aspx?username=" + username);
            //Response.Redirect(@"~/Student/StudentSendMail.aspx?username=" + username);
        }
        //catch (Exception ex)
        {
            string m;
        }
    }

    protected void btnViewOldMails_Click(object sender, EventArgs e)
    {
        //try
        {
            Server.Transfer(@"~/Teacher/TeacherOldMail.aspx?username=" + username);
            //Response.Redirect(@"~/Student/StudentSendMail.aspx?username=" + username);
        }
        //catch (Exception ex)
        {
            string m;
        }
    }
}

3 个答案:

答案 0 :(得分:3)

当您使用Response.Redirect而不是Server.Transfer时,是否也会发生这种情况?在Server.Transfer之后,您在浏览器地址栏中看到了什么URL?

答案 1 :(得分:0)

你的表单/按钮事件在做什么?听起来你在PostBack上执行代码却没有看到代码,很难说。

答案 2 :(得分:0)

如果您通过默认登录但是不关闭浏览器,“发送邮件”是否正常工作?我的假设是Server.Transfer是问题所在。默认是接收事件,然后将控制转移到TeacherControlPanel但不传递事件。

所以,就像Henk说试试Redirect一样。