简体   繁体   English

asp.net-事件

[英]asp.net - events

What are the points to check for, when an ASP.NET button is not firing an event? 当ASP.NET按钮未触发事件时,要检查哪些点?

I have double-clicked the button to add an event-handler. 我双击了按钮以添加事件处理程序。

But event is not firing. 但是事件并没有触发。

<%@ 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" /></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)
    {
        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;
        }
    }
}

This is the master page: 这是母版页:

MasterPage.master
-----------------
<%@ 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)
    {

    }
}

Please try the following in order to diagnose the problem: 请尝试以下操作以诊断问题:

  1. Create a new top-level page in the site. 在站点中创建一个新的顶层页面。 Add a single button to it. 向其添加一个按钮。 Double-click the button to add an event handler. 双击按钮以添加事件处理程序。 Set a breakpoint at the event handler and see if it gets hit when you click the button. 在事件处理程序中设置一个断点,然后单击按钮时查看是否被命中。
  2. Create a new content page in the site, using the same master page. 使用相同的母版页在网站中创建一个新的内容页。 Add a single button to it. 向其添加一个按钮。 Double-click the button to add an event handler. 双击按钮以添加事件处理程序。 Set a breakpoint at the event handler and see if it gets hit when you click the button. 在事件处理程序中设置一个断点,然后单击按钮时查看是否被命中。
  3. Create a new master page in the site. 在站点中创建一个新的母版页。 Add a content placeholder to it and nothing else. 向其添加内容占位符,仅此而已。 Create a new content page in the site, with this new master. 使用这个新的母版在网站上创建一个新的内容页面。 Add a single button to it. 向其添加一个按钮。 Double-click the button to add an event handler. 双击按钮以添加事件处理程序。 Set a breakpoint at the event handler and see if it gets hit when you click the button. 在事件处理程序中设置一个断点,然后单击按钮时查看是否被命中。

I use your code in Web Application Project and everything working fine. 我在Web应用程序项目中使用您的代码,并且一切正常。 Did you use a web application or a Web Site project ? 您使用的是Web应用程序还是网站项目?

And what is the version framework you use ? 您使用的是什么版本框架?

which button you try ? 您尝试哪个按钮?

Try another angle - Take the OnClick attribute off of the asp:Button and explicitly add the event handler in the code behind in OnInit 换个角度-将OnClick属性从asp:Button移开,然后在OnInit后面的代码中显式添加事件处理程序

protected override void OnInit(EventArgs e)
{
    btnSendMail.Click += new EventHandler(btnSendMail_Click);
    base.OnInit(e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM