How do we call client script from asp.net server side script?
How do we call clien开发者_JS百科t script from asp.net server side script?
You can try code below:
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString("N"), "alert('ok');", true);
Try this: How to: Add Client Script Dynamically to ASP.NET Web Pages from MSDN
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.
You can call following line on any of your server side event.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "alert('Test Event');", true);
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
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);
精彩评论