Click here to Skip to main content
14,382,805 members
Rate this:
Please Sign up or sign in to vote.
I am trying to call a jquery function from inside a vb.net function and it is not working, although I am not getting any errors. I don't know what I am doing wrong as this is my first time trying to do this.

What I have tried:

Code behind:

Public Sub HideInsertNewRowButton()
ClientScript.RegisterClientScriptBlock(Me.GetType(), "Script", "DisableNewRow();", True)
End Sub

javascript from ASPX page

<script>
 function DisableNewRow() {
      $("#add").hide();
 }


the element I want to hide from ASPX page:

<div class="collapse navbar-inverse navbar-collapse" collapse="navCollapsed">
       <ul class="nav navbar-nav">
         <li id="add" ng-hide="HideAppend">
         <a title="Add A New Row" href="#" ng-click="newRow($event)" ng-class="{'disabled' : ShowLoading}"></a>
         </li>
Posted
Updated 29-Aug-19 12:04pm
v2
Comments
F-ES Sitecore 29-Aug-19 10:07am
   
ids have to be unique so one thing to check is that you don't have more than one element with an id of "add". If that's not the issue use some debugging techniques to see what is happening. Does your HideInsertNewRowButton function get called? If you view the source of the page do you see your "DisableNewRow();" script on the page? Does DisableNewRow get called? Does "$("#add")" return anything? (check $("#add").length is > 0).
Vincent Maverick Durano 29-Aug-19 16:56pm
   
Why are you calling a JavaScript function at the server in the first place? Any specific reason for that? Why not call the JavaScript function directly at the client instead?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can give these a try

register client script block inject after view state

register startup script inject before end of tag

1
ScriptManager.RegisterClientScriptBlock(Page, GetType(String), "myScriptName" + key, $"yourJsFunctionName({jsObject})", True)

2
ClientScript.RegisterStartupScript(GetType(),"hwa","alert('Hello World');",true);

3

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true);

4 Another trick you can try

<body> 
<head runat="server"> 
  <script type="text/javascript"> 
    function showAlertBox() { 
      alert("Invoked from codebehind."); 
    } 
  </script> 
</head>
  <form id="form1" runat="server"> 
  <div> 
    <asp:Label ID="lable1" runat="server" Text=""></asp:Label> 
    <asp:Button ID="btnShow" runat="server" Text="Invoke Dialogue" /> 
  </div> 
  </form> 
</body>


Code behind to trigger the JavaScript function is here.

lable1.Text = "<script type='text/javascript'>showDialogue();</script>";


One more thing try to put alet('Test'); in your js function, it might be calling the function but not able to find your selector.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100