3

How do we call client script from asp.net server side script?

2
  • Please provide more details. Currently your question doesn't make sense. Commented May 24, 2010 at 8:41
  • quesion incomplete..what u exactly asking Commented May 24, 2010 at 9:00

6 Answers 6

7

You can try code below:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString("N"), "alert('ok');", true);
Sign up to request clarification or add additional context in comments.

Comments

3

Try this: How to: Add Client Script Dynamically to ASP.NET Web Pages from MSDN

Comments

3

Here are two of these methods :

  Page.ClientScript.RegisterStartupScript(Type, String key, String script) 

  Page.ClientScript.RegisterClientScriptBlock(Type, String key, String script)

The "Type " & "Key" pair distinguish between the different registered script. So you can't register two script having both same type & key pair. The above two methods do the same thing with a basic difference that specify where to

use these functions.

      1.RegisterClientScriptBlock()  method add the script before the controls are renderd in the page. So the scripts we are registered can't acess the controls inside the page. 


      e.g : var name = document.getElementById('txtName');  //will not work as excepted.



      2.RegisterStartupScript() method add the script before the end of body tag after all the controls are rendered in the browser. So the registered script can acess the controls inside the page .


      e.g : var name = document.getElementById('txtName');  //will work fine.

Comments

1

You can call following line on any of your server side event.

Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "alert('Test Event');", true);

1 Comment

This is the one that worked for me, Thanks!; I called a function instead of (alert) Like this [Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "functionName();", true);]
0

You can do this by adding button and on any event on that button for as your requirement.

Suppose you want to delete the text box after insertion of a string.

Then you simply double click on that button itself and go to that code behind file and simply put code like:

txtbox1.text = null;

after each click event your textbox is empty automatically

Comments

0

Insert the button and double click it.

Add this property: runat="server"

In your code behind file write this code:

Page.ClientScript.RegisterStartupScript(this.GetType(), "clientscript",
    "document.getElementById('Button').style.visibility = 'visible';" ,true);

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.