0

I wanna call a javascript function in c sharp code. i wanna hide the specific Li tag whose id is c sharp variable"li_name". For this i am calling a javascript function.

Cs Code:

  li_name = DSRolePermission.Tables[0].Rows[k]["MENUNAME"].ToString();
  StringBuilder script = new StringBuilder();

  script.Append("<script type=\"text/javascript\">");

  script.Append("li_visibility('" + li_name + "')");

  script.Append("</script>");

  Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());

JS code:

 <script type="text/javascript">
           function li_visibility('<%=li_name %>') {
document.getElementById('<%=li_name %>').style.display = 'hidden';

      }      
</script>

But this code is not working properly.

1
  • You are passing "li_name" in server inline side script to the li_visibility function in js.Then why are you again dynamically passing on the client end javscript function "li_visibility('<%=li_name %>')" like this? Commented Jan 25, 2012 at 8:14

4 Answers 4

2

You should change your js-code to this:

<script type="text/javascript">
   function li_visibility(id) {
       document.getElementById(id).style.display = 'none';    
   }      
</script>

I you want to use hidden value, use the visibility property;

element.style.visibility = 'hidden'; //'visible' to show;

Note: When using visibility:hidden, the space occupied by the element is still present. display:none, will hide both the element and the occupied space.

Sign up to request clarification or add additional context in comments.

5 Comments

this would work just pass the li_name in javascript function from server side like Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock","li_visibility("+li_name+")");
this is also not working..:-(Is it necessary to change code of C sharp function.
also ensure that if your server event causing partial load of page then the case may be different. check for error's in firebug console
a little typo in the code in my comment please try this Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock","<script>li_visibility("+li_name+")</script>");
@pallavisahane if still not working please explain exactly what's wrong - you get error? Also, see my answer for something else you're missing.
1

By having the same key, only the first call will be executed and the rest ignored silently.

Assuming you have this code in a loop, add the element ID to the JS key:

Page.ClientScript.RegisterClientScriptBlock(typeof(this), "JavaScriptBlock_" + li_name, script.ToString());

Also changed the type, it's better practice to pass the type of the calling page/control.

1 Comment

+1 for this extra information "Also changed the type, it's better practice to pass the type of the calling page/control."
0

Why you add a c# var inside the JS ? just update it to : <script type="text/javascript"> function li_visibility(x) { document.getElementById(x).style.display = 'hidden'; }</script>

Comments

0
<script type="text/javascript">
    function li_visibility('<%=li_name %>') 
    {
        document.getElementById('<%=li_name.ClientID %>').style.display = 'hidden';
    }      
</script>

This may help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.