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, but onclick
on that input
element is a client-side event. You're probably getting an error in your JavaScript console saying that dojob()
is an undefined function.
Use an asp:Button
instead of an input
to make use of server-side click events. Also, dojob()
should be protected
. By not declaring a protection level I think the default is private
so the page controls might not even be able to see it. It should match the event handler for a button click:
protected void dojob(Object sender, EventArgs e)
{
}