C # asp.net javascript call

I have a div inside asp: content:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <div id="slidebar" style="display:none;" >There are some pending approvals.Please approve by the 25th of this month <a href="#" id="slink" style="margin-left:10px;" onclick="fade('slidebar');">Click here to close <sup style="font:caption;color:#373636;">X</sup></a> </div> <script language="javascript" type="text/javascript"> function showbanner() { document.getElementById("slidebar").style.visibility="visible"; } </script> <\asp:content> 

and the code behind:

  ClientScript.RegisterClientScriptBlock(Page.GetType(), "displaybanner", "return showbanner();", true); 

I cannot call the showbanner function from the code behind and even when I directly call the statement inside showbanner with registerclientscript ... it is not called.

Please, help

+4
source share
3 answers

The visibility and display properties do not match, change the js function to:

 function showbanner() { document.getElementById("slidebar").style.display="block"; } 

also change your code to

 ClientScript.RegisterStartupScript(Page.GetType(), "displaybanner", "showbanner();", true); 

therefore, the script is executed after the page loads, otherwise the element will not be found.

+4
source

I'm not sure why your script register is not working, but you tried to put a javascript call directly on the page inside the script block? If this works, wrap the script block in the panel at the bottom of the page with visible = "false". In your code, when you determine that you want it to work, set the visibility to true.

If you are using jQuery, I would also wrap the contents of your script block in:

 $(document).ready(function() { //javascript call here }); 

Just to make sure it is not called before the page can process it.

All of this assumes, of course, that your function call is good and that the problem is with the script registration client.

0
source

Well, one of my teammates, Mahi, came up with a solution ... not CleientScript.reg .....

we used Page.RegisterStartupScript and it works great :))

0
source

Source: https://habr.com/ru/post/1342175/


All Articles