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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top