简体   繁体   English

asp.net按钮将不会触发其OnClick事件

[英]asp.net buttons wont fire their OnClick events

I think this one is fairly simple to solve as I remeber having this problem before but cant remember what I did about it... 我认为这个问题很容易解决,因为我记得以前遇到过这个问题,但不记得我对此做了什么...

I decided to do away with a particular asp.net page and incorporate some of its controls on another existing page (a few textboxes and 2 buttons), copying and pasting them on and adding the code-behind. 我决定取消一个特定的asp.net页面,并将其某些控件合并到另一个现有页面(几个文本框和2个按钮)中,将其复制并粘贴到其上并在其后添加代码。

Since doing this none of the buttons on my amended page fire their OnClick events, I tried to add a new one but that doesn't fire either. 由于执行此操作后,修改后的页面上的所有按钮均未触发其OnClick事件,因此我尝试添加一个新按钮,但也不会触发。

The actual code-behind for these buttons are failr simple - save a record, response.redirect, that sort of thing. 这些按钮的实际代码很简单-保存记录,response.redirect之类的东西。

Any Ideas? 有任何想法吗?

thanks 谢谢

button markup 按钮标记

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" 
                Width="91px" />

code-behind 代码隐藏

protected void Button1_Click(object sender, EventArgs e)
    {

        helper hl = new helper();
        hl.UpdateComplaint(txtCaseNo.Text, Forename.Text, Surname.Text, txtActionProgress.Text,            
 Response.Redirect("Complaint.aspx");
    }

Because you defined your delegate in code behind specially OnClick event 因为您在特别是OnClick事件背后的代码中定义了委托

you must copy this code 您必须复制此代码

Button.Click += .....

You can associate this delegate in aspx view or in your code behind, for your case you have made in code behind, and you don't copy 您可以在aspx视图中或在后面的代码中关联此委托,因为您是在后面的代码中创建的,因此您无需复制

Verify your CodeFile of your aspx if you makes copy paste 如果进行复制粘贴,请验证aspx的CodeFile

Here are a couple of things you might try. 您可以尝试以下几项。 No guarantees, but maybe a push in the right direction :) 没有保证,但可能会朝正确的方向发展:)

Open up Visual Studio and the ASP.NET page in question. 打开Visual Studio和有问题的ASP.NET页。 In the Designer, click on the control, and go to the Properties box, and look for the OnClick event. 在设计器中,单击控件,然后转到“属性”框,然后查找OnClick事件。 If it's blank, click in the box and select the event handler you wish to attach from the dropdown that is available. 如果为空白,请在框中单击,然后从可用的下拉列表中选择要附加的事件处理程序。 If there are no events in the list, just double-click to see if Visual Studio will just create a default event handler for you, then paste the code from the previous onClick handler into that new method. 如果列表中没有事件,则只需双击以查看Visual Studio是否将为您创建默认事件处理程序,然后将先前onClick处理程序中的代码粘贴到该新方法中。

Hope that helps. 希望能有所帮助。

Edit Per later comments, suggested that a full copy-and-paste of the codebehind from a different ASP.NET page may have created a partial class declaration that did not match the markup's CodeFile or Inherits directive on the new page, which can cause a variety of odd behavior. 以后的编辑评论指出,从其他ASP.NET页完全复制和粘贴代码后,可能已创建了部分类声明,该声明与新页面上标记的CodeFile或Inherits指令不匹配,这可能会导致各种奇怪的行为。

isn't it OnClick="Button1_Click" case sensitive? OnClick="Button1_Click"区分大小写? Have you tried setting a breakpoint in the event handler, to see if it fires? 您是否尝试过在事件处理程序中设置断点以查看是否触发? And can it be a Page_Load issue? 可以是Page_Load问题吗? That you do things without 你做事没有

if (!Page.IsPostBack)

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

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