简体   繁体   中英

Button click event at server end is not getting fired

PFB the following code which is present in one of my .aspx files. I have added a button and added a method in the code behind. I have attached the process to the debugger but the button click event is not getting fired. Everything in the rest of my application is working fine but only on this screen no event is getting triggered at server end. If I add a onClientClick event it is getting fired.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Site2.Master" AutoEventWireup="true"
CodeBehind="AdminPage.aspx.cs" Inherits="RoomBook.Views.AdminPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script language="javascript" type="text/javascript">
    $(function () {
        var allPanels = $('.accordion > dd').hide(1000);
        $('.accordion > dt > a').click(function () {
            allPanels.slideUp();
            $(this).parent().next().show(1000);
        });
        return false;
    });

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager runat="server">
</asp:ScriptManager>
<dl class="accordion">
    <dt><a href="#">Modify / Delete Room</a></dt>
    <dd>
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <label id="roomNumber0" class="itemLeft">
                    Room Name
                </label>
                <asp:DropDownList class="inputTextRoom" ID="ddRoomName" runat="server">
                </asp:DropDownList>
                <br />
                <label id="Label1" class="itemLeft">
                    Status</label>
                <asp:RadioButtonList class="radioButton" ID="rbStatus" runat="server">
                    <asp:ListItem Text="Active"></asp:ListItem>
                    <asp:ListItem Text="Deactive"></asp:ListItem>
                </asp:RadioButtonList>
                <asp:Button ID="btnModify" runat="server" Text="Button" OnClick="btnModify_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </dd>
</dl>
</asp:Content>

In the code behind the method definition is :

    protected void btnModify_Click(object sender, EventArgs e)
    {

        RoomDomain roomdomain = new RoomDomain();
        string userName = (WindowsIdentity.GetCurrent().Name.ToString().Split('\\'))[1];

        if (!(ddRoomName.SelectedIndex == 0))
        {
            roomdomain.RoomName = ddRoomName.SelectedItem.Value;

            if (rbStatus.SelectedItem.Value == "Active")
            {
                roomdomain.IsActive = true;
            }
            else
            {
                roomdomain.IsActive = false;
            }
            adminPage.ModifyExistingRoom(roomdomain, userName);
        }

    }

I tried adding several buttons on the page but none of them work. When I attach the process the breakpoint hits on other events but not on button click. Any suggestions as to what blunder I have committed in this piece of code?

Your button is inside an update panel so you need to add the button as a trigger to the update panel. Add to the bottom of your update panel:

</ContentTemplate>
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="btnModify" />
</Triggers>
</asp:UpdatePanel>

您可以使用

<asp:AsyncPostBackTrigger ControlID="btnModify" />

I created new Web Forms projects in both 2010 and 2012 and could not reproduce the problem. I added your script to the top content and the markup into the "MainContent" area and in both cases the button fired no problem. (I altered the code-behind to simply toggle the radio buttons, and again, in both projects, it worked fine.) I'm wondering if your script(s) could somehow be causing the issue.

<asp:Button ID="btnModify" runat="server" Text="Button" OnClick="btnModify_Click" UseSubmitBehavior="false"/>

Since the button is inside the UpdatePanel change it to use the postback mechanism instead of your browsers form submit.

Button.UseSubmitBehavior Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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