Link to home
Start Free TrialLog in
Avatar of smacca
smaccaFlag for Australia

asked on

OnClick event not firing

I am having problems with the OnClick event handling of the ASP button object.
It wont seem to fire the event.
The details are as follows:


------------------------
The webpage contains the following button:

<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnClick="GenerateNewsletter" Runat="server" />

------------------------

The code behind file contains the following function:


            /// <summary>
            /// Generates the newsletter
            /// </summary>
            protected void GenerateNewsletter(Object sender, EventArgs e)
            {
                  content = ((Newsletters)handler).GenerateContent();
            }

-----------------------

When I am debugging, the GenerateNewsletter function is NEVER accessed.
Can someone please help?
      
      
Avatar of dorward
dorward

I don't know ASP very well - but my guess is that you are trying to execute something in the server side environment in the browser.

There is a strong seperation between what runs on the server, and what runs on the browser.

You can't run an ASP function from a client side event (like onclick). You have to have the browser rerequest the ASP page (typically from a link or a form submit).

Its possible that I'm entirely wrong, as I don't know ASP - but to deal with onclick events we need to see the code sent to the browser, not the ASP source. (i.e. what you get if you view source in the browser having requested the page)
Avatar of smacca

ASKER

Thanks for your input!

The code is ASP.NET using C#.
The ASP.NET framework operates alot differently from the old ASP one - you can use OnClick events on "server-side" controls in this frramework.
Avatar of smacca

ASKER

Can I entice any interest at 250 points?
My c# is rusty, but I think you have to do this:

<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnClick="GenerateNewsletter()" Runat="server" />
smacca,

The problem is that you have this button runat the server and the server will never generate a click event.  

The click event is generated (of course) when the user clicks the mouse on the button which is on the client side, not the server side.

Change your runat so it'll run on the client side instead of the server side and then the OnClick event should fire correctly.

frodoman
Avatar of smacca

ASKER

If you remove the runat="server" statement from the button, it disspears from screen.
You want runat="client" instead of runat="server"  -- you can't take the statment out.
Avatar of smacca

ASKER

Hey Fro, thanks heaps for your help but it doesnt work.
Are you flying blind here or are these factual answers - I dont mean to sound rude!!!!!

Just that, you cannot have anything but RunAt="server" on a web server control.
Try this...

Remove _OnClick="GenerateNewsletter()"_ from the asp control. Then try one of the following solutions.

To be added to the  <head> section of your document:

<script language="VBScript" for="btnAutoGenerate" event="onClick">GenerateNewsletter()</script>

Or alternatively, you should also be able to do this:

<script language="VBScript">
Sub btnAutoGenerate_onClick
      'code from GenerateNewsletter() here
End Sub
</script>

Let me know what happens.
No, not flying blind - just tired.

If you're using visual studio, you want to use the button control on the HTML tab instead of the one on the Web Forms tag.  This is the runat client version (although you're correct that it doesn't actually say runat=client).  

This control does fire an onclick event - it's what all of us old asp programmers used before .Net came along.
Avatar of smacca

ASKER

Frodoman - they are all "client side" onClick events NOT "server-side" onClick events. When using the "onClick" event for a HTML control you can invoke a client side Javascript function but can NEVER access server-side code. I need to get the server-side onClick event that is used with ASP.NET web server controls and invoke a server-side function.

Cheers.
Avatar of smacca

ASKER

EE Experts - Can anyone help here, this should be easy points for an ASP.NET pro.
Avatar of smacca

ASKER

This is soo painful!
For the life of me I cannot figure out why the OnClick event is not firing - is there any way to check what events are being triggered?
I have been looking at examples and troubleshooting and all provide the exact coding structure that I am using - can anyone help - client waiting on functionality.

TheKenman - this is server-side onclick functionality - am I right here (i am sure there is a difference, the click event is sent to the server not the client - ??)

Cheers.
Avatar of smacca

ASKER

Does the AutoEventWireup="false" in the page directive have anything to do with event triggering.
Here's some code to play with; I don't have a test environment, so I guess you get the fun part ;)

// Take 1 -->
<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnClick="GenerateNewsletter" Runat="server" />

protected void GenerateNewsletter(Object sender, EventArgs e)
{
      content = ((Button)Sender).GenerateContent();
}

// END Take 1 -->

// Take 2 -->
<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" Runat="server"/>

void Page_Load(Object sender, EventArgs e)
      {
         btnAutoGenerate.Click += new EventHandler(this.GenerateNewsletter);
      }

protected void GenerateNewsletter(Object sender, EventArgs e)
{
      content = GenerateContent();
}
// END Take 2 -->


// Take 3 -->
<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnClick="GenerateNewsletter" Runat="server"/>

protected void GenerateNewsletter(Object sender, EventArgs e)
{
      content = GenerateContent();
}
// END Take 3 -->


// Take 4 -->
<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnCommand="handleBtn" Runat="server" CommandArgument="genNewsLetter" CommandArgument="today"/>

protected void handleBtn(Object sender, CommandEventName e)
{
      String cmd = e.CommandArgument;
      String nam = e.CommandName;
      
      if (cmd == "genNewsLetter")
      {
            Message.Text = "CommandArg = genNewsLetter";
            if (nam == "today")
            {
                  Message.Text += ", CommandName = Today.";
            }

            content = GenerateNewsletter();

      }
}

protected void GenerateNewsletter(Object sender, EventArgs e)
{
      content = GenerateContent();
}
// END Take 4 -->
Oops, correction on take 4!!

Should be something more like this...

// Take 4 -->
<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" OnCommand="handleBtn" Runat="server" CommandArgument="genNewsLetter" CommandName="today"/>

protected void handleBtn(Object sender, CommandEventName e)
{
     String cmd = e.CommandArgument;
     String nam = e.CommandName;
     
     if (cmd == "genNewsLetter")
     {
          Message.Text = "CommandArg = genNewsLetter";
          if (nam == "today")
          {
               Message.Text += ", CommandName = Today.";
          }

          content = GenerateNewsletter();

     }
}

protected void GenerateNewsletter(Object sender, EventArgs e)
{
     return GenerateContent();
}
// END Take 4 -->
Avatar of smacca

ASKER

I AM DYING HERE!!

Ok I used the code model:

<asp:Button ID="btnAutoGenerate" CssClass="button" Text="Auto-Generate" Runat="server"/>

void Page_Load(Object sender, EventArgs e)
      {
         btnAutoGenerate.Click += new EventHandler(this.GenerateNewsletter); // break point here - debugger runs this statement BUT never steps through into GenerateNewsletter function.
      }

protected void GenerateNewsletter(Object sender, EventArgs e)
{
     content = GenerateContent(); //break point here
}

When debugging, after I click on the auto-generate button, the cursor registers the click event but NEVER steps into GenerateNewsletter function (I have break point in there).
The button simply submits the form.

----------------------
I think the code is fine but for some reason the OnClick event is NEVER firing - are there any global configurations (Web.Config) or Page Directives that may affect this.

-----------------------

The button resides WITHIN the following form statement:

<form name="myForm" id="myForm" method="post" runat="server" onsubmit="Javascript:return checkDate(this.ccCalendarControls_dayList.value,this.ccCalendarControls_monthList.value,this.ccCalendarControls_yearList.value);">

Would the "onSubmit" event on the form be affecting the other events. It does "return" checkDate which I assume means return control to page - perhaps that means forget all other code and events and simply return control - is this correct.

------------------------

The KenMan - I greatly appreciate your effort!!!!
Avatar of smacca

ASKER

I removed the onsubmit statement from form and get the same result.
Still picking at straws here.
That's what I was about to suggest. I'm at work right now, and unfortunately, they want me to do THEIR work :p I will look at this some more when I get home later on tonight, sometime after 2am CST.

And you don't have to call me "The Kenman"... It's just Kenman, my normal nick, was already taken :)
Avatar of smacca

ASKER

Fantastic! Thanks again for your help - I will award you points regardless for your effort. Cheers "Kenman" - lol!
ASKER CERTIFIED SOLUTION
Avatar of TheKenman
TheKenman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had the same issue, set the PostBackURL property and it worked.