动态将js添加到asp.net文件 [英] Dynamically adding js to asp.net file

查看:69
本文介绍了动态将js添加到asp.net文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Page_Load中,我从母版页中识别页面标题块,然后将XML文件中的javascript添加到页面的该部分.

In my Page_Load I identify the page header block from my master page and add javascript from an XML file to that part of the page.

它似乎没有及时加载.如果我在页面上明确地放置了脚本引用,那么它将起作用.但是当我通过page_load事件加载时就没有.

It doesn't seem to be loading in time. If I explicitly put the script reference on the page, it works. But not when I load through the page_load event.

应该尽快加载吗?

推荐答案

您没有添加任何代码,所以我无法告诉您您在做什么错.相反,我将告诉您一种从后面的代码添加脚本引用的有效方法.

You're not adding any code so I'm not able to tell you what you're doing wrong. Instead, I'll tell you a valid way to add script references from code behind.

Page_Load 上的类似内容应该可以解决问题

Something like this on Page_Load should do the trick

var js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "myFile.js";
Page.Header.Controls.Add(js);

这篇关于动态将js添加到asp.net文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆