开发者

C# asp.net calling javascript

I have a div inside asp:content :

<asp:Content ID="Content1" ContentPlaceHolder开发者_StackOverflow社区ID="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 function showbanner from the code behind and even when I directly call the statement inside showbanner with registerclientscript ...it doesnt get called .

Please help


The properties visibility and display are not the same, change the js function to:

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

also change your code behind to

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

so the script executes after the page has load or else it won't find the element.


I'm not sure why your register script isn't working but have you tried putting the javascript call directly on the page inside a script block? If that works then wrap the script block in a pannel at the bottom of the page with visible="false". In your code behind 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 doesn't get called before the page can actually handle it.

This all assumes of course that your function call is good and that the issue is with register client script.


Okay one of my team mebers Mahi came up with a solution...rather than CleientScript.reg.....

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜