asp.net the way to register JavaScript _ practical Tips
Source: Internet
Author: User
1, IsStartupScriptRegistered: Whether the object is registered to enable script.
2, IsClientScriptBlockRegistered: Whether the object registered client script block.
3, Isclientscriptincluderegistered: Whether the object is registered client script contains.
First example:
string info = "<script>alert" (' How are you? ');</script> ";
Determine if the event is registered, written to the area:<body>----<script></body>
if (! Page.ClientScript.IsStartupScriptRegistered ("Test")
{
Dynamically register and execute JavaScript
Page.ClientScript.RegisterStartupScript (this. GetType (), "test", info);
}
A second example:
aspx file:
Name: <input id= "txtUserName" type= "text"/>
<input id= "btnsubmit" type= "button" value= "OK" onclick= "Salert (txtusername.value)"/>
CS File:
string info = "function Salert (userName) {alert (' Your name: ' + userName ')}";
To determine if test is registered
if (! Page.ClientScript.IsClientScriptBlockRegistered (this. GetType (), "test")
{
Dynamically registering JavaScript
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "test", info, true);
}
A third example:
aspx file:
Name: <input id= "txtUserName" type= "text"/>
<input id= "btnsubmit" type= "button" value= "OK" onclick= "Salert (txtusername.value)"/>
Jscript.js
function Salert (userName)
{
Alert ("Your name" + userName);
}
CS File:
To determine if test is registered
if (! Page.ClientScript.IsClientScriptIncludeRegistered (this. GetType (), "test")
{
Dynamically registering JavaScript
Page.ClientScript.RegisterClientScriptInclude ("Test", "jscript.js");
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service