您的位置:首页 > Web前端 > JavaScript

C#向前台页面中输出JS代码

2012-09-18 10:14 387 查看
[csharp] view
plaincopy

//需要注意的是

//当向页面输出带有相同的Type和Key的的参数时可以避免重复输出

------------------------------------------------------------------------------------------------------------------------------------------------

//在UpdatePanel以外按钮输出JS方法

ClientScriptManager cs = this.ClientScript;

cs.RegisterArrayDeclaration("Hello", "1, 2, 3");

//输出结果:

//<script type="text/javascript">

//var Hello = new Array(1, 2, 3);

//</script>

cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");

//输出结果:

//<script src="HelloWorld.js" type="text/javascript"></script>

//// cs.RegisterClientScriptResource(

cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");

//输出结果:

//向Button1按钮注册一个Hello的属性

//<script type="text/javascript">

//var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");

//Button1.Hello = "World";

//</script>

cs.RegisterHiddenField("hello", "world");

//输出结果:

//<input type="hidden" name="hello" id="hello" value="world" />

cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");

//输出结果:

//(OnSubmit)事件,点击按钮指出提示框

//<script type="text/javascript">

//function WebForm_OnSubmit() {

//return window.confirm('Do you really want to submit the form?');

//return true;

//}

//</script>

cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);

//输出结果:

//在页面顶部

//<script type="text/javascript">

//function helloWorld(){alert(1);}

//</script>

cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");

//输出结果:

//在页面底部

//<script>alert('The page has loaded!')</script>

--------------------------------------------------------------------------------------------------------------------------------------------

//在UpdatePanel以内按钮输出JS方法

//为某个UpdatePanel输出JS脚本

//注:只有当按钮所在的UpdatePanel更新时此方法生效

ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);

//为当前页面输出JS脚本

//注:无论当前UpdatePanel是否更新都向页面输出JS脚本

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "UpdatePanel2", "alert(2)", true);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: