control ids

N

Nate Hekman

Lets say I have an asp:Image control, and I want to change its image in
client-side script. How can the script reference the <img> object? This
code doesn't work, because although asp:Image has the ID of _img, the
resulting <img> tag is given a different ID.

<asp:Image id="_img" ImageUrl="oldimage.jpb">
<script>
_img.src = "newimage.jpg";
</script>

Thanks for your help.


Nate
 
F

Fredrik Elestedt

The easiest way would be to check the rendered HTML code and check the ID of
the control.
Another idea I havn't tried is to create a literal control instead of _img.
in your script, and then set the text property to _img.ClientID
<asp:Image id="_img" ImageUrl="oldimage.jpb">
<script>
<asp:Literal ID="src" runat="server" />.src = "newimage.jpg";
</script>

src.Text = _img.ClientID;

// Fredrik
 
B

bruce barker

asp.net munges the name, use codebehind or

<asp:Image id="_img" ImageUrl="oldimage.jpb" runat=server>
<script>
<%= _img.UniqueID %>.src = "newimage.jpg";
</script>

-- bruce (sqlwork.com)

| Lets say I have an asp:Image control, and I want to change its image in
| client-side script. How can the script reference the <img> object? This
| code doesn't work, because although asp:Image has the ID of _img, the
| resulting <img> tag is given a different ID.
|
| <asp:Image id="_img" ImageUrl="oldimage.jpb">
| <script>
| _img.src = "newimage.jpg";
| </script>
|
| Thanks for your help.
|
|
| Nate
|
|
 
M

Matt Berther

Hello Nate,

string s = String.Format("<script>{0}.src = 'newimage.jpg';</script>", _img.ClientId);
Page.RegisterClientScriptBlock("myscript", s);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top