user3432257
user3432257

Reputation: 415

asp.net onclick function is not working for a tag

I have this aspx code

<form id="form1" runat="server">
        <div id="tabs">
            <ul>
                <li><a href="#tabs-1" >Today</a></li>
                <li><a href="#tabs-2" runat="server" onclick="TodayTab_Click">Tomorrow</a></li>
                <li><a href="#tabs-3">Any Date</a></li>

although i have used onclick function, this function is not being fired,

why please?7

this is the function and I make a break point on it

protected void TodayTab_Click(object sender, EventArgs e) {
            int i = 9;
            i++;
        }

edit2

You guys told me that I have to change my a tag to asp:Hyperlink and I did, but the function is still not being fired.

and this is the updated code

<form id="form1" runat="server">
        <div id="tabs">
            <ul>
                <li><a href="#tabs-1" >Today</a></li>
                <li><asp:Hyperlink href="#tabs-2" runat="server" onclick="TodayTab_Click">Tomorrow</asp:Hyperlink></li>
                <li><a href="#tabs-3">Any Date</a></li>

Upvotes: 3

Views: 2722

Answers (3)

Smeegs
Smeegs

Reputation: 9224

I would recommend either using <asp:linkbutton> which has a serverside click event. But then navigation will have to be handled in javascript by manually navigating to the hashtag in the href. (I don't recommend this method as you don't need a full postback)

Keep in mind that this will do a full postback and will require an updatepanel if you don't want a full refresh.

Or continue using a standard hyperlink button, but have a pagemethod that handles the server side action you need and call it via ajax. I recommend this method as you stay 100% on the client as your server side needs only rely on posting very little data to the server.

Upvotes: -1

freefaller
freefaller

Reputation: 19953

You should use a <asp:LinkButton> control - this is instead of <a runat="server"> or <asp:HyperLink>.

The <asp:LinkButton> will use the OnClick event handler as per your requirement.

(As a side note, if you need to do any client-side processing, use the OnClientClick attribute to generate the onclick attribute on the rendered control)

Here's what that would end up looking like:

<asp:LinkButton runat="server" onclick="TodayTab_Click" Text="Tomorrow" />

Upvotes: 3

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14216

You need to use onServerClick function instead of onClick

onServerClick="TodayTab_Click"

Upvotes: 0

Related Questions