简体   繁体   English

事件不触发的原因是什么?

[英]What is the cause when event does not fire?

What do you think causes events (of any control, textbox, button, etc) in webforms to not fire? 您认为什么导致Web表单中的事件(任何控件,文本框,按钮等)无法触发? What are the things that need to be checked and considered when creating such an event? 创建此类事件时需要检查和考虑哪些事项?

Consider this code: 考虑以下代码:

<%@ 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();
    }

I do have master page on this page but i can't see a reason why this code does not fire the onclicked event. 我在此页面上具有母版页,但是我看不到此代码不触发onclicked事件的原因。 I only want to click and Imagebutton then a modalpopup(ajax control) to appear with certain fields to be filled up then click submit and save it to the database but onclicked event does not fire and also the postback in page_load event of the page is not firing either. 我只想单击,然后单击Imagebutton,然后出现一个modalpopup(ajax控件),并显示要填充的某些字段,然后单击“提交”并将其保存到数据库中,但是不会触发onclicked事件,并且也不会在page_load事件中回发要么射击。

Ensure that autowireup is enabled. 确保启用了自动接线。 If it's not, non of the event will be triggered. 如果不是,则将触发所有事件。

Probable reason could be your javascript is throwing up error. 可能的原因可能是您的JavaScript抛出了错误。 Try window.onerror function in your java script and make it to display alerts. 尝试在Java脚本中使用window.onerror函数,使其显示警报。 So, you will now if there is some issue going on there. 因此,如果那里有问题,您现在就可以解决。

Did you not register the event (either programmatically or declaratively)? 您是否没有(以编程方式或声明方式)注册事件? Did you have AutoEventWireup="false"? 您是否有AutoEventWireup =“ false”?

Events follows publisher-subscriber mechanism. 事件遵循发布者-订阅者机制。 Control/object always raise (fire) the events as per implementation. 控件/对象始终根据实现引发(触发)事件。 The question is whether a handler is attached or not. 问题是是否附加了handler So if you are interested to subscribe the event then you must have to attach an event handler to that event . 因此,如果您有兴趣订阅事件,则必须将event handler附加到 event

Take a look at MSDN article - Raising an Event and Events (C# Programming Guide) . 看一下MSDN文章- 引发事件事件(C#编程指南)

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

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