Below is my code:
default.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="TestAjax._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
//I set an onClick event on this button:
< input type = "button" id="btnCl" onclick = "doJob" />
</asp:Content>
default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//fires on page load;
}
void doJob()
{
//code here;
}
The question is:
Why didn't the onclick event trigger? (On default.aspx btnCL)
Thanks
Because
dojob()is a server-side function, butonclickon thatinputelement is a client-side event. You're probably getting an error in your JavaScript console saying thatdojob()is an undefined function.Use an
asp:Buttoninstead of aninputto make use of server-side click events. Also,dojob()should beprotected. By not declaring a protection level I think the default isprivateso the page controls might not even be able to see it. It should match the event handler for a button click: