3

I'm trying to call a jquery function from a asp link button. Here is my link button html:

        <div style="padding-left:75px">
            <asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
        </div>

Here is my jquery function:

        function ShowCCControls() {

          $('#lblCC').show();
          $('#txtCC').show();
      } //end ShowCCControls()

When I try to build, I get the error:

ASP.internal_email_aspx does not contain a definition for 'ShowCCControls' and no extension method 'ShowCCControls' accepting a first argument of type 'ASP.internal_email_aspx' could be found (are you missing a using directive or an assembly reference?)

I have this working on another page using a check box:

                   <asp:CheckBox ID="chkNew" TabIndex="8" runat="server" Text="New Tank" OnClick="SetNewTankControls()"
                   ClientIDMode="Static" />

Anybody see an issue? Thanks

Here is all of the javascript:

    <script  type="text/javascript" language="javascript">

    //document.ready is used for jquery, waits until the doc is ready to be manipulated
    $(document).ready(function () {

        HideControls();

    });  //end document.ready



    function HideControls() {

        $('#lblCC').hide();
        $('#txtCC').hide();
       $('#lblBCC').hide();
        $('#txtBCC').hide();
    }  //end HideControls()

    function ShowBCCControls() {
        $('#lblBCC').show();
        $('#txtBCC').show();
    } //end ShowBCCControls

    function ShowCCControls() {

        $('#lblCC').show();
        $('#txtCC').show();
    }  //end ShowCCControls()

2
  • Is your click handler public? Commented Apr 19, 2012 at 21:16
  • I'm not using a click handler in the code behind. I'm trying to do everything on the client, so I'm trying to use jquery. Commented Apr 19, 2012 at 21:38

4 Answers 4

10

OnClick is for specifying handlers in code-behind. If you want to specify a javascript function, you should use OnClientClick attribute.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick(v=vs.80).aspx

Sign up to request clarification or add additional context in comments.

6 Comments

I tried the OnClientClick, that works, but the only problem is it calls the function I want, then it also runs some other jquery functions also.....I can't figure out why that happens either.
@user1202606, can you be more specific, what "other" jquery function do you mean? Are you sure it's jquery?
@user1202606 Do you mean "it still posts back to the server"? If so, see AutoPostBack.
<script type="text/javascript" language="javascript"> //document.ready is used for jquery, waits until the doc is ready to be manipulated
I edited my original post and added the <script> tag, please see above
|
1
    <div style="padding-left:75px">
        <asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClientClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
    </div>

3 Comments

+1 OnClick is for server-side events. Does AutoPostBack also apply here, though?
I'm trying not to use autopostback, I'm trying to make it all happen on the client side.....
@user1202606 Which is why On Client Click is used. Also set AutoPostBack = false so it will only be "client side". In any case, On Click will never directly invoke JavaScript and will result in Errors on the Server during a PostBack, such as the one posted.
1

You could just set the handler in your client script like this:

  $('#lbAddCC').click(function() {

      $('#lblCC').show();
      $('#txtCC').show();
  });

Since you're not intending to perform server side behaviours with this click event, there isn't any need to define a handler on the server control and then have it render out a call to the client side function when you could just call it directly.

EDIT:

You will of course need to couple the client side script with the removal of the erroneous OnClick handler on the server control as below:

<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" Text="Add CC"></asp:LinkButton>

4 Comments

But that won't fix the error, which is caused by OnClick being used.
Yeah I should have said to remove the onclick event handler, I did in my original answer then removed it! Will update the answer
I tried that and it didn't work, I'm starting to think that the browser has the old code in cache and keeps running it. Do you know how to clear that?
If the browser is hanging onto old/cached code, you want to either delete your offline files/cached content or on most browsers, hit ctrl + f5 will often do the trick. I expect if you're seeing other code executed when the page loads that there is more script than you've added on the page. Can you post all the js from the page in question pls?
0

it could be that the page load the function call before loading jquery code, make sure you put the jquery before the link button and reference jquery liabrary

2 Comments

I reference the jquery library in the master page and I have the <script> tag with the jquery function before the link button
I guess another way is trigger the click by jquery and Make sure you turn off autopostback on link button. Jquery is client side and if the button postback to the server, page reload itself so nothing work. Turn it off by set autopostback= false. The OnClientClick event is the right one for client side. $('<#<%=lbAddCC.ClientID%>').click(function() { $('#lblCC').show(); $('#txtCC').show(); });

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.