我遇到了这样的问题,请教google,结果大多数只是介绍那个注册js的几个函数,而这几个函数插入的js都在body里面,幸而在老外那里看到了这个代码,其实比较简单,但够有用
1 HtmlGenericControl Include2 = new HtmlGenericControl("script");
2 Include2.Attributes.Add("type", "text/javascript");
3 Include2.InnerHtml = "alert('JavaScript in Page Header');";
4 this.Page.Header.Controls.Add(Include2);
或
Literal li = new Literal();
li.Text = "<script
</script>";
this.Page.Header.Controls.Add(li);
或者
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 //动态加载外部CSS文件
4 HtmlLink link = new HtmlLink();
5 link.Attributes.Add("type", "text/css");
6 link.Attributes.Add("rel", "stylesheet");
7 link.Href = "不知道什么路径" + "/style.css";
8 this.Page.Header.Controls.Add(link);
9
10 //动态加载外部JS文件
11 Page.ClientScript.RegisterClientScriptInclude("Common", "/UserPart/Script/Common.js");
12 this.Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "Common", "/UserPart/Script/Common.js");
13
14
15 //动态生成DIV层
16 HtmlGenericControl div1 = new HtmlGenericControl();
17 div1.TagName = "div";
18 div1.Attributes.Add("id", "layout");
19 div1.Style.Add("width","100%");
20 div1.InnerHtml = "<h1>测试能否显示</H1>";
21
22 StringBuilder str = new StringBuilder();
23 str.Append("<div id=\"ssyj\"onclick=\"alert('123ok')\";>jidfj</div>");
24 div1.InnerHtml = str.ToString();
25 this.Page.FindControl("form1").Controls.Add(div1);
26
27 //动态生成表格
28 HtmlTable talbe = new HtmlTable();
29 talbe.Attributes.Add("borde", "1");
30 talbe.Attributes.Add("id", "tid");
31 this.Page.Title = "向页面发送控件元素";
32 this.Page.FindControl("form1").Controls.Add(talbe);
33 }
34 }
35
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!