Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET button not firing on click event

I am new to Asp.net and my question may not be so professional. I am using the design of Udemy.com in my asp.net web forms project. I put the header and footer of udemy in a masterpage. But when I add a button to one of m web forms the click event will not fire.

<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button" />

protected void Button1_Click1(object sender, EventArgs e)
{
    Page.Title = "Sample Text";
}

To fix this problem, I deleted all of the scripts from my pages. Also I tried to set CausesValidation to False. But nothing helps. Could you help me fix this problem?

Update

I added the OnClick event to my code. But I still face that problem.

like image 344
user3138901 Avatar asked Dec 27 '13 09:12

user3138901


2 Answers

All the previous answers are correct, except that you "already added" the OnClick event to the ".aspx" file. I don't know whether it is something in the template you're using, or somewhere else in the code. But I would recommend that you check the following (Based on my experience in being stuck in similar situations):

  1. Do you have any Field Validators in your ".aspx" page? Perhaps one of those validators are fired and if they were without any Error Messages.
  2. Have you copied that button from somewhere else? If yes, then try to add a new button and add its click event, by double clicking the button in the design mode. As sometimes the "OnClick" button's event is not registered on the ".designer.cs" file.
like image 176
CodingMate Avatar answered Sep 22 '22 02:09

CodingMate


Some points you may try... The code that you posted (after update) should work.

  • If you open the Design part of your visual studio and double click in the button, This will lead you to the event click of that button, if there is not one event, then it'll create one for you.
  • Your button must be within the form tag.
  • To test the event click is firing, use Breakpoints not label or any other markup change, but if you want it ... you should check if the Title component has runat="server" attribute and it's within an update panel.
like image 30
Michel Ayres Avatar answered Sep 20 '22 02:09

Michel Ayres