简体   繁体   English

服务器端的按钮单击事件未触发

[英]Button click event at server end is not getting fired

PFB the following code which is present in one of my .aspx files. PFB下面的代码存在于我的.aspx文件之一中。 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. 如果我添加一个onClientClick事件,它将被触发。

<%@ 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. 我在2010年和2012年都创建了新的Web窗体项目,但无法重现该问题。 I added your script to the top content and the markup into the "MainContent" area and in both cases the button fired no problem. 我将您的脚本添加到了顶部内容中,并将标记添加到了“ MainContent”区域中,在两种情况下,该按钮均不会触发任何问题。 (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. 由于按钮位于UpdatePanel内部,因此将其更改为使用回发机制,而不是您的浏览器提交表单。

Button.UseSubmitBehavior Property Button.UseSubmitBehavior属性

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

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