1
$(function () 
{        
    $('#btnnext').click(function ()  
    {
    alert("Hello after click event");
    });
});

And the code for button as:

<asp:Button ID="btnnext" runat="server"  CssClass="BStyle" OnClick="btnnext_Click" Text="Save" />

I have place Button control in update panel control and also have used master.. page

1
  • @kis try to write script in update panel Commented Mar 2, 2015 at 10:25

5 Answers 5

3
$('#<%=btnnext.ClientID%>').click(function ()  
    {
    alert("Hello after click event");
    });

Another way would be to use OnClientClick attribute

<asp:Button ID="btnnext" runat="server" CssClass="BStyle" OnClick="btnnext_Click" OnClientClick="MyFunctionName()" Text="Save" />

<script>
function MyFunctionName(){
    alert('test');
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

try this:

You can Use ClientID to Get AcutalID Generated by ASP.net

$('<%#btnnext.ClientID%>').click(function ()  
    {
    alert("Hello after click event");
    });

1 Comment

The # is missing. try $('#<%=btnnext.ClientID%>')
1

Look at what ASP.net generates (view source on your webpage).

ASP.NET webforms generate another ID for the frontend ID then what you are saying to asp:Button. my advice is to either bind the click on a cssClass instead or if you still want to bind it with an id you need to use [id$="_btnnext"] as a selector.

$(function () {
    $('[id$="_btnnext"]').click(function () {
        alert("Hello after click event");
    });
});

8 Comments

are you getting js errors? What have you tried, if you bind it on a class name instead it will work. Also what ID do you get when you view your source code in the browser?
it generates id as: ContentPlaceHolder1_btnnext
bind it with a CSS class instead on ID, because asp.net wont change that value. or as many pointed out '#<%=btnnext.ClientID%>'
That works. But is it right way to use CSS class instead of ID?
I dont understand what you mean with "The right way"? You can solve it easy with a css class because .NET wont change that, but if thats the only button on your site you could use button as a selector. I personally never bind anything with an ID, because I'm almost never have something that is that unique. So personally I would bind it with a css class yeah.
|
0

You can use your button ID or your Css class as selector

ID selector

$('<%#btnnext.ClientID%>').click(function ()  
    {
    alert("Hello after click event");
    });

Class selector

$('.BStyle').click(function ()  
    {
    alert("Hello after click event");
    });

Comments

-1

Try this, using .on function :

$(function() 
{        
  $('#btnnext').on('click',function()  
  {
    alert("Hello after click event");
  });
});

2 Comments

Have you ever tried to alert something rather than place in click handler like $(function() { alert('hello'); }); .Just wanna check jquery library is load or not. Because there is nothing wrong with the code right now.
Yeah, if i place alert before click event then it automatically alert on page load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.