Hello
I'm quite new to Jquery, so bare with me please.
I've searched google, but I couldn't find an answer to my question.
I'm developing an asp.net 4.0 application, and i wrote a Jquery inside my content page:

$(document).ready(function () 
{ $("#Button1").click(function () 
{
 alert("Hello");
 }); 
});

I have a button:

<asp:Button ID="Button1" runat="server" Text="Button" />

From some reason, the alert won't fire when I press the button,,,
any Ideas?

I might add: it's a content page.
Thanks in advance,
Rotem

Recommended Answers

All 9 Replies

hello!
because when you click the button it will postback to the server so you won't see the message.
change it to html input and you will get what you want,or you can use onclientclick property of the asp button and right a js function to do the alert.
Regards!

see the source of the page in any browser wen u run your page. Most of the times your controls doesn't have the id as you have specified. your button's id might have changed from 'Button1' to something like 'ctplh100_Button1....' and thus jquery can't find any element with the id 'Button1' and you never see the alert. You can try
1.

$("#<%=Button1.ClientID%>").click.....

2. set the button's client id mode to static
3. check the generated id of the button and then put that as your jquery selector :) (don't do that)
4. give a class attribute to your button and do something like

$(" .buttonClass").click.....

(not recommended though)

see the source of the page in any browser wen u run your page. Most of the times your controls doesn't have the id as you have specified. your button's id might have changed from 'Button1' to something like 'ctplh100_Button1....' and thus jquery can't find any element with the id 'Button1' and you never see the alert. You can try
1.

$("#<%=Button1.ClientID%>").click.....

2. set the button's client id mode to static
3. check the generated id of the button and then put that as your jquery selector :) (don't do that)
4. give a class attribute to your button and do something like

$(" .buttonClass").click.....

(not recommended though)

Hello.
Thanks for the fast replies.
I tried your advices.
indeed, the button's clientId is ContentPlaceHolder1_Button1.
Unfortunetely, nothing happens!
The page is sort-of refreshed, and that's it..
what could be the reason? :)
thanks

as cocool stated, you are using an asp:button which by default calls a server events and do a postback.

if you want to call an javascript upon the click of asp:button, you might want to add a onclientclick event to the button to call a javascript then add

return false;

to it. this would cancel the post event of the button.

<script lang="javascript">
    function hello()
    {
    alert("Hello");
    }
   </script>


<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="hello" />

Try this code

Hi Guys.
I appreiciate your help, but it's not what I'm looking for.
I need to use Jquery, not Javascript. The purpose of my Jquery is not to fire an alert. I just did that, in order to make a simple Jquery to work, before I use a more advanced one.
Cruize_Invades, I tried the onclientclick and everthing else you all advised, but stil my alert won't fire, using that Jquery.

Please help :)

Thanks :)

Rotem

are there any code behind that is need to be process?

anyway try this

$(document).ready(function() {
            $('#<%=Button1.ClientID %>').click(function(event) {
                alert("hello");
            });
        });

Please checkout these Links I hope that would be more helpful to you.

Ok guys, maybe I was wrong not clearfing execlty what it is I want to achieve:

I want to use this Jquery:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <script src="http://code.jquery.com/jquery-latest.js"  type="text/javascript">

            $(document).ready(function () {
                $("#tblGames tr:even").addClass("alt");
                $("#tblGames div:odd").addClass("alt");

            });

    </script>

in order to alternate coloring a table (zebra style)

this is the table definition:

<table id="tblGames" runat="server" border="1" style="border-style: solid;" frame="border" class="mytable">

this doesnt work..
Please help I'm despert.

Rotem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.