Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
5.00/5 (1 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

HTML
<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}">class="fa fa-edit"></a>
         </li>
Posted
Updated 29-Aug-19 11:04am
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

You can give these a try

register client script block inject after view state

register startup script inject before end of tag

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

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

3

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

4 Another trick you can try

ASP.NET
<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.

C#
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900