954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Jquery

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

rotemorb
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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!

cocoll
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 1
 

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)

ckchaudhary
Newbie Poster
23 posts since Oct 2011
Reputation Points: 12
Solved Threads: 5
 

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

rotemorb
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

Cruize_Invades
Light Poster
40 posts since May 2007
Reputation Points: 6
Solved Threads: 7
 
<script lang="javascript">
    function hello()
    {
    alert("Hello");
    }
   </script>


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


Try this code

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 

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

rotemorb
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

anyway try this

$(document).ready(function() {
            $('#<%=Button1.ClientID %>').click(function(event) {
                alert("hello");
            });
        });
Cruize_Invades
Light Poster
40 posts since May 2007
Reputation Points: 6
Solved Threads: 7
 

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

martinwhisely
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

rotemorb
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: