admin管理员组

文章数量:1301552

initially a table is invisible in the ASP.NET page. on button click event, it should go to the code behind and from there i need to call a function in javascript. In that javascript function i should make the table to be visible. Is this possible?? Somebody please help me out

initially a table is invisible in the ASP.NET page. on button click event, it should go to the code behind and from there i need to call a function in javascript. In that javascript function i should make the table to be visible. Is this possible?? Somebody please help me out

Share asked Aug 6, 2012 at 6:23 user1575995user1575995 411 gold badge1 silver badge3 bronze badges 2
  • Is that table a HTML table (<table>) or a ASP.NET Control? – Andre Calil Commented Aug 6, 2012 at 6:24
  • 1 possible duplicate of How to call javascript function from code behind? – Artem Koshelev Commented Aug 6, 2012 at 6:25
Add a ment  | 

7 Answers 7

Reset to default 2

Try this:

Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "Myfunction();", true);

try the following code

ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "alert('hello');", true);

Here "this" is used for control by which you want to fire this. this.Gettype() is used get type of Client Script "isActive" it is the unique key. After all these the javascript code. it could be function whatever you want to do. and lastly there is true that ask you either you want to script tag for the javascript code you are writing here or not.

     ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "HelloWorld", "HelloWorld();", true);

From code behind you can invoke javascript something like this

ScriptManager.RegisterStartupScript(Page, GetType(Page), "ScriptName", 'document.getelementById("tableId").display = inline;' , True)

But i would remend to do it on client side without posting a page. Post the page only if you need to access server resources.

You can call javascript function using code behind as:

 ScriptManager.RegisterStartupScript(page, typeof(Page), Guid.NewGuid().ToString(), "callJavaScriptFunction();", true);

calling javascript functions from code behind is not possible(if we leave apart the newer sockets).
after the page has loaded once fro the web server then there is no way for the server to know what data to send to which user..
you should probably try out sending an ajax request and execute some code depending upon the response got from the server.

Try This

string str="<script>alert(\"ok\");</script>"; Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);

本文标签: how to call a javascript function from c code behindStack Overflow