0

I am trying to access javascript document from ASP.net page code behind. How do I do same thing as below in c# code behind?

document.getElementById(id).src = "myurl.com"

Thank you for your help.

2
  • use Page.ClientScript.PageClient.RegisterStartupScript Commented Nov 7, 2012 at 16:13
  • Note @Tony you are asking to get the src url of a random div/panel/etc, not get "access" to it (I'm not even sure what that would mean) Commented Nov 7, 2012 at 16:25

1 Answer 1

1

Add runat="server" and an ID to the element. You should then be able to access the src via the attributes collection.

<img src="http://www.google.com/img.png" id="imgGoogle" runat="server" />

Dim src as String = imgGoogle.Attributes("src")
Sign up to request clarification or add additional context in comments.

5 Comments

What relevance your answer has to do with above question?
From what I could make out from the question, and I may be wrong, is that he wanted to be able to access the element via the code behind (or server side). "I am trying to access JavaScript document from ASP.net page code behind. How do I do same thing as below in c# code behind?" although I accidentally provided vb.net code
It looks right to me, he wanted to just get the src URL from the element.
What i am trying to do is to create a ASP.NET that refreshes to certain list of URL in given interval period (Like sildeshow). One of approach that I am using is to use Iframe on the page and assgin a url to Document.elementById('myframe').src = url. But, I am looking for a way to do this in code behind, not from javascript.
If you want to do it server side, you will need to do post backs or partial postbacks which isn't efficient at all. What you could rather do is load all the images before hand, hide them and show only one at a time. That way there are no postbacks/async postbacks to call the next image.

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.