3

I am trying to change JavaScript src from a code behind file.

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

When I am trying to access the object properties from the code behind file, no src option is available, do I need to put the src file at some other property?

enter image description here

1
  • 2
    You're going to have to modify the innerhtml of the page I think... Commented May 1, 2012 at 20:21

2 Answers 2

5

You can't access the src attribute like that. HTML attributes are accessible via the .Attributes collection:

srcSurvey.Attributes["src"] = "my/directory/file.js";
Sign up to request clarification or add additional context in comments.

3 Comments

I can't make that work, the compiler tries to read the js file, CS1012: Too many characters in character literal, what's happening there?
sounds like you're trying to drop the actual JS into the src. Just put in the string that points to the file, like I did above.
oh, it's also possible that you put your file path in single quotes instead of double quotes :)
3

Change javascript src from ASP.net Code Behind in the Head html tag

// 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);

Previous Answer

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
    "stackoverflow", 
    "<script type=\"text/javascript\" src=\"mypage.asp?p=2\"></script>", false);

Just change the hardcoded string containing the script tag with a dynamic one.

for example:

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);

4 Comments

what should I put in the html part, since the code needs to be exactly at that position on the page? How to do that? Also, what are you using 'stackoverflow' in this case for? Thx
"stackoverflow" is just a unique identifier for the script it could be any valid c# string
from where did you get the PageRepository function? I can't find that option in the code. Thx
Sorry @Laziale I was busy at work, I'll change my answer and it will be more clear. By the way PageRepository is just a mockup class that I expect to return a number, it was just an example it could return any number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.