事件不发生时的原因是什么?

时间:2011-12-16 04:58:05

标签: c# asp.net events webforms

您认为Webforms中的事件(任何控件,文本框,按钮等)不会触发?在创建此类事件时需要检查和考虑的事项是什么?

考虑以下代码:

<%@ Page Language="C#" MasterPageFile="~/App_Theme/TranByEmployeeMaster.master" AutoEventWireup="true"
    CodeFile="ProcessEmployeePenalty.aspx.cs" Inherits="Transactions_ProcessEmployeePenalty"
    Title="Untitled Page" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:GridView ID="gvEmployees" runat="server" HeaderStyle-CssClass="HeaderStyle"
        RowStyle-CssClass="RowStyle" AlternatingRowStyle-CssClass="AlternatingRowStyle"
        FooterStyle-CssClass="FooterStyleGrid" EmptyDataRowStyle-BackColor="White" AutoGenerateColumns="False"
        EmptyDataText="No result were found." ShowFooter="true" DataKeyNames="EmployeePenaltyID">
        <Columns>
            <asp:TemplateField HeaderText="No.">
                <ItemTemplate>
                    <asp:Label ID="lblNo" runat="server" Text='<%# gvEmployees.Rows.Count + 1 %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Update">
                <ItemTemplate>
                    <asp:ImageButton ID="btnUpdateEmployeeGrid" runat="server" ImageUrl="~/App_Resources/images/content/Edit.gif"
                        CommandName="Update" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="btnDeleteEmployeeGrid" runat="server" ImageUrl="~/App_Resources/images/content/Delete.png"
                        CommandName="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField HeaderText="EmployeeID" DataTextField="EmployeeID" />
            <asp:ButtonField HeaderText="Penalty Nature" DataTextField="PenaltyNature" />
            <asp:ButtonField HeaderText="Penalty Description" DataTextField="PenaltyDescription" />
            <asp:ButtonField HeaderText="Penalty Amount" DataTextField="PenaltyAmount" />
        </Columns>
    </asp:GridView>
    <%--<asp:Button ID="btnAddPenalty" runat="server" Text="Add Penalty" OnClick="btnAddPenalty_Clicked"
                OnClientClick="return confirm('Are you certain to post the selected cutoff?');" />--%>
    <asp:ImageButton ID="btnAddPenalty" runat="server" SkinID="Add" OnClick="btnAddPenalty_Clicked" />
    <asp:DropDownList ID="sample" runat="server" AutoPostBack="true" OnSelectedIndexChanged="sample_selected">
        <asp:ListItem Value="0" Text="Sample0" />
        <asp:ListItem Value="1" Text="Sample1" />
        <asp:ListItem Value="2" Text="Sample2" />
    </asp:DropDownList>
    <asp:Panel ID="pnlPopUp" runat="server">
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblsample" runat="server" Text="Employee : " />
                </td>
                <td>
                    <asp:DropDownList ID="ddlEmployees" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Penalty Nature : " />
                </td>
                <td>
                    <asp:TextBox ID="tbxPenaltyNature" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Penalty Description : " />
                </td>
                <td>
                    <asp:TextBox ID="tbxPenaltyDescription" runat="server" TextMode="MultiLine" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Penalty Amount : " />
                </td>
                <td>
                    <asp:TextBox ID="tbxPenaltyAmount" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnClose" runat="server" Text="Close" />
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Clicked" />
                </td>
            </tr>
        </table>
    </asp:Panel>
    <asp:Label ID="lblDummy" runat="server" />
    <ajax:ModalPopupExtender ID="MP" runat="server" TargetControlID="lblDummy" PopupControlID="pnlPopUp"
        CancelControlID="btnClose">
    </ajax:ModalPopupExtender>
</asp:Content>

protected void btnAddPenalty_Clicked(object sender, EventArgs e)
    {
        MP.Show();
    }

我在这个页面上有母版页,但是我看不出这个代码没有触发onclicked事件的原因。我只想点击和Imagebutton然后一个modalpopup(ajax控件)显示某些字段要填充然后单击提交并将其保存到数据库但是onclicked事件不会触发,并且页面的page_load事件中的回发也不是解雇。

4 个答案:

答案 0 :(得分:0)

确保已启用autowireup。如果不是,则不会触发事件。

答案 1 :(得分:0)

可能的原因可能是你的javascript引发了错误。在java脚本中尝试window.onerror函数,并使其显示警报。所以,如果那里有一些问题,你现在就会。

答案 2 :(得分:0)

您是否未注册该事件(以编程方式或声明方式)? 你有AutoEventWireup =“false”吗?

答案 3 :(得分:0)

事件遵循 publisher-subscriber 机制。控件/对象总是根据实现引发(触发)事件。问题是是否附加了handler。因此,如果您有兴趣订阅该活动,则必须将event handler附加到 event

查看MSDN文章 - Raising an EventEvents (C# Programming Guide)