1

I have been trying to call .js file function from code behind but function is not being called.

I have this following html button which needs to be visible from the code behind.

   <input id="btnShowMap" type="button" value="Results On Map" onclick = "ShowMap();" style="visibility: hidden;"/>

I have tried following three methods so far and none of them is working.

 -ClientScript.RegisterStartupScript(Me.GetType(), "VoteJsFunc", "test();")

 -Page.ClientScript.RegisterStartupScript(Me.[GetType](), "VoteJsFunc", "alert('Sorry.You are not legible to vote')", True)

 -ClientScript.RegisterStartupScript(Me.GetType(), "VoteJsFunc", "test();")

Here is .js file function

function test() {
var hdLat = $('input[id$=hdVendorLat]').val();
    var hdLng = $('input[id$=hdVendorLng]').val();
    if (hdLat != 0 && hdLng != 0) {
        $('#btnShowMap').show();
    }
    else {
        $('#btnShowMap').hide();
    }
 }

Here is the pahe html

 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 </asp:Content>

 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="updSearch" UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearch" />
    </Triggers>
    <ContentTemplate>
        <asp:HiddenField ID="hdVendorLat" runat="server" Value="0" />
        <asp:HiddenField ID="hdVendorLng" runat="server" Value="0" />
        <asp:HiddenField ID="hdVenID" runat="server" Value="" />
  <asp:Panel ID="pnlExport" runat="server" Enabled="true">      
  <asp:Button ID="btnSearch" runat="server" Text="Search" Width="90px" />                          
  <input id="btnShowMap" type="button" value="Results On Map" onclick   = "ShowMap();" style="visibility: hidden;"  />
</asp:Panel>

<script type="text/javascript" src="/scripts/inspector-search.js"></script>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
user1263981
  • 2,953
  • 8
  • 57
  • 98

3 Answers3

1

try register script, like this:

Page.ClientScript.RegisterStartupScript(GetType(), "VoteJsFunc", "test()", True)

I checked this locally, worked fine.

rock_walker
  • 453
  • 5
  • 14
0

jQuery method show works over display css property. It is equivalent to call .css( "display", "block").

Since you are using visibility property, method show has no effect on changing this so it remains hidden. I see 2 options to fix this:

  • use .css( "visibility", "visible") instead of .show()

  • replace style="visibility: hidden;" by style="display: none" on your input markup

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

You just need to invoce the mentioned .js file in html before you call the script from which you are calling the function. If you need more information, please include your HTML code.

deanpodgornik
  • 322
  • 2
  • 7