簡體   English   中英

事件不觸發的原因是什么?

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

您認為什么導致Web表單中的事件(任何控件,文本框,按鈕等)無法觸發? 創建此類事件時需要檢查和考慮哪些事項?

考慮以下代碼:

<%@ 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事件中回發要么射擊。

確保啟用了自動接線。 如果不是,則將觸發所有事件。

可能的原因可能是您的JavaScript拋出了錯誤。 嘗試在Java腳本中使用window.onerror函數,使其顯示警報。 因此,如果那里有問題,您現在就可以解決。

您是否沒有(以編程方式或聲明方式)注冊事件? 您是否有AutoEventWireup =“ false”?

事件遵循發布者-訂閱者機制。 控件/對象始終根據實現引發(觸發)事件。 問題是是否附加了handler 因此,如果您有興趣訂閱事件,則必須將event handler附加到 event

看一下MSDN文章- 引發事件事件(C#編程指南)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM