Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ASP.net代码隐藏更改javascript src属性_C#_Javascript_Asp.net_Html - Fatal编程技术网

C# 从ASP.net代码隐藏更改javascript src属性

C# 从ASP.net代码隐藏更改javascript src属性,c#,javascript,asp.net,html,C#,Javascript,Asp.net,Html,我正在尝试从代码隐藏文件更改JavaScript src <script type="text/javascript" runat="server" id="srcSurvey" language="JavaScript" src="mypage.asp?p=2"></script> 当我试图从代码隐藏文件访问对象属性时,没有可用的src选项,是否需要将src文件放在其他属性 您不能像这样访问src属性。HTML属性可通过集合访问。属性集合: srcSurvey.A

我正在尝试从代码隐藏文件更改JavaScript src

<script type="text/javascript" runat="server" id="srcSurvey" language="JavaScript" src="mypage.asp?p=2"></script>

当我试图从代码隐藏文件访问对象属性时,没有可用的src选项,是否需要将src文件放在其他属性


您不能像这样访问
src
属性。HTML属性可通过
集合访问。属性
集合:

srcSurvey.Attributes["src"] = "my/directory/file.js";

更改头html标记中ASP.net代码后面的javascript src

// this is the name of the file, it could be any name, but because
// you need it dynamic I add the number at the end.
string jsFileName = string.Format("mypage.asp?p={0}", 1);

// we add a HtmlGenericControl with the tag script (this will work for a
// css also, you just need to change script for LINK, and src for href) 
HtmlGenericControl linkDynamicJavascriptFile = new HtmlGenericControl("script");
// and the you add the relative client url of the resource
linkDynamicJavascriptFile.Attributes.Add("src", 
    ResolveClientUrl("~/" + jsFileName));
// just adding the type attribute, not necesary in html5
linkDynamicJavascriptFile.Attributes.Add("type", "text/javascript");
// we add the script html generic control to the Page Header and we're done
Page.Header.Controls.Add(linkDynamicJavascriptFile);
先前的答案

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
    "stackoverflow", 
    "<script type=\"text/javascript\" src=\"mypage.asp?p=2\"></script>", false);
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),
“堆栈溢出”,
“”为假);
只需将包含脚本标记的硬编码字符串更改为动态字符串

例如:

string code = string.Empty;
var pageNumber = PageRepository.GetPageAsString(); // get page number
code = string.Format("<script type=\"text/javascript\" src=\"mypage.asp?p={0}\"></script>", pageNumber);

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"stackoverflow", code, false);
string code=string.Empty;
var pageNumber=PageRepository.GetPageAsString();//获取页码
代码=字符串。格式(“”,页码);
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),
“stackoverflow”,代码,假);

我想您必须修改页面的innerhtml…我应该在html部分添加什么,因为代码需要精确地位于页面上的那个位置?怎么做?另外,您在本例中使用“stackoverflow”的目的是什么?Thx“stackoverflow”只是脚本的唯一标识符,它可以是任何有效的c#string,您从哪里获得PageRepository函数的?我在代码中找不到该选项。ThxSorry@Laziale我工作很忙,我会改变我的答案,它会更清楚。顺便说一句,PageRepository只是一个模型类,我希望返回一个数字,这只是一个示例,它可以返回任何数字。我做不到这一点,编译器试图读取js文件,CS1012:字符文本中的字符太多了,发生了什么事?听起来像是你试图将实际的js放入src中。只需输入指向文件的字符串,就像我在上面所做的那样。哦,也有可能将文件路径用单引号而不是双引号引起来:)