简体   繁体   中英

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.

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.

The actual code-behind for these buttons are failr simple - save a record, response.redirect, that sort of thing.

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

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

Verify your CodeFile of your aspx if you makes copy paste

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. In the Designer, click on the control, and go to the Properties box, and look for the OnClick event. 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.

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.

isn't it OnClick="Button1_Click" case sensitive? Have you tried setting a breakpoint in the event handler, to see if it fires? And can it be a Page_Load issue? That you do things without

if (!Page.IsPostBack)

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