tags:

views:

249

answers:

4

I have a Master Page and a Content Page(SomePage.aspx). The Content page has a control called Button1.

I am calling the Button1 using jQuery in MasterPage like this

$('[id$=Button1]').click(function() {
                        alert('');
                    });

However nothing happens. I have added MasterType in SomePage.aspx.

The Button is a linkbutton.

Any suggestions?

A: 

You should take a look at the source code of your page as it is rendered by the browser and check the id of Button1. It is probably not Button1 but something like myContent$Button1.

Ronald Wildenberg
Yes and that's why i am using id$..
Hitz
Hm, good point...
Ronald Wildenberg
+1  A: 

The code you've posted works, although jQuery suggests $("[id$='Button1']") or $('[id$=\'Button1\']').
The master page shouldn't be relevant here, it all goes to one HTML page at the end.
Make sure:

  • The JavaScript is indeed on the page (no master page mistake) - view the source of the page and look for it.
  • jQuery is loaded - try alert(jQuery); somewhere.
  • Your jQuery code is inside $(document).ready
  • JavaScript is executed: very often a small JavaScript syntax error causes the whole page not to work. Try to debug it, or put some old-fashioned alert boxes before and after the code, to see how (and if) it runs.
Kobi
Thanks for the detailed inputs..I have tried all these steps. The Button is a link button and calling $("[id$='Button1']").click(); has not effect. I added alert('') at load and it fired..however inside click nothing happens..
Hitz
Try to add these alerts everywhere: before $(document).ready, inside it, before its end and after it. go nuts. Or, perhaps you can post the entire javascript block?
Kobi
I observed one thing..It works with a button but not with a linkbutton
Hitz
Asny idea why it does not work with LinkButton but works with a button? LinkButton looks like this <asp:LinkButton id="ttt" PostBackUrl="Hut.htm">Click</asp:LinkButton>
Hitz
the LinkButton should be runat="server". Is it?
Kobi
yes it is runat="server"
Hitz
Well, no idea then, it worked for me. Could you post some more code? your JavaScript and generated link button (the <a ...> tag) would probably help.
Kobi
+1  A: 

I think it should be: '#[id$=Button1]'

Shawn Simon
+1  A: 

As Paolo mentions, be sure that it's in document.ready, as JS can't operate on an element that doesn't (yet) exist. Also, double-check your source to make sure the actual rendered ID of the control is as you expect.

You might find these controls helpful: http://clipperhouse.com/jQuery/

Matt Sherman