简体   繁体   中英

LinkButton is not calling JavaScript function in OnClick?

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()

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

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

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>

可能是页面在加载jquery代码之前加载函数调用,请确保在链接按钮之前放置jquery并引用jquery liabrary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM