asp.net c#onclick事件没有解雇。相同的设置在不同的页面上触发

时间:2013-11-20 04:34:33

标签: c# asp.net

对于我的生活,我无法弄清楚为什么这些onClick事件处理程序没有针对此特定页面触发。完全相同的设置适用于不同的页面。在另一页上,我确保将一个元素设置为autopostback,在页面加载方法中创建了事件处理程序,并且函数存在且有效。我能错过什么?

来自.aspx页面

<%@ Page Language="C#" CodeFile="forgot.aspx.cs" AutoEventWireup="true" Inherits="forgot" %>
....


<div id="content">

        <asp:Table runat="server">
            <asp:TableRow>
                <asp:TableCell>Email Address*</asp:TableCell>
                <asp:TableCell><asp:TextBox ID="emailEntry" runat="server"></asp:TextBox>    
                </asp:TableCell>
                <asp:TableCell><asp:Button ID="sendPassword" runat="server" Text="Recover Password" causesValidation="true"/>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="3" HorizontalAlign="Center">
                    <asp:Button ID="home" runat="server" text="Goto Main Page"/>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </div>

From the .aspx.cs file

public partial class forgot : System.Web.UI.Page {

protected void Page_Load(object sender, EventArgs e) {

        bool userLoggedIn = false;
        this.Form.Action="forgot.aspx";

        // get application state
        // save to session if user exists

        if (userLoggedIn) {
             loginButton.Click += new System.EventHandler(logout);
            loginButton.Click -= login;
        } else {
            loginButton.Click += new System.EventHandler(login);
            loginButton.Click -= logout;
        }
        emailEntry.AutoPostBack = true;
        sendPassword.Click += new System.EventHandler(emailPasswordToUser);
        home.Click += new System.EventHandler(goToHomePage);

        ....
    }
}

我的想法是我有一些代码正在做一些未定义的行为,这可以解释为什么它在一个页面而不是另一个页面上工作。我从我的搜索结果中添加了Form.Action语句。我是C#的新手,所以我确定我错过了一些重要的设置或事情,我们将非常感谢任何建议。

由于

1 个答案:

答案 0 :(得分:0)

if (userLoggedIn) {
             loginButton.Click += new System.EventHandler(logout);
            loginButton.Click -= login;
        } else {
            loginButton.Click += new System.EventHandler(login);
            loginButton.Click -= logout;
        }

我不会这样做;肯定会有一个你总是附加到按钮上的事件会更简单,并确定是否在处理程序内登录或注销?