1

I have a span in a .master page that uses runat="server". Server-side, c# code adds to the innerHtml of this span. The span is id="tickerfade"; however, when it is rendered on the page the id is changed to id=ctl00_tickerfade. why does this happen and how can I stop it?

steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • 1
    if you want to use this span in `javascript` you can write `$('<%= tickerfade.ClientID %>).click(....)' – Harry89pl Aug 20 '12 at 19:51
  • You can also get the rendered id using the `<%=ControlID.ClientID%>` See also: http://stackoverflow.com/questions/5763557/accessing-control-client-name-and-not-id-in-asp-net – Aristos Aug 20 '12 at 19:51
  • 1
    @harry180 perfect. That worked like a charm. Submit as an answer and I'll accept. – steventnorris Aug 20 '12 at 20:21
  • @steventnorris pls change/add tag to javascript and for future add good tags to your questions :) I've made answer for you to accept;) – Harry89pl Aug 21 '12 at 11:24

2 Answers2

4

You can set the control's ClientIdMode to Static.

The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

Have a look at following link for further informations on ASP.NET Web Server Control Identification:

http://msdn.microsoft.com/en-us/library/1d04y8ss.aspx

If the real problem is how to find the correct id on clientside, you can use the ClientId property.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • When I set the clientidmode i get the error "Error parsing attribute 'clientidmode': Type 'SPTasks.mainpage' does not have a public property named 'clientidmode'." due to lack of support below .net 4.0. Our server is running 3.0. Any work arounds for 3.0? – steventnorris Aug 20 '12 at 19:51
  • ClientIdMode work in 3.0 and up version but only on `server` `controls` like `` @aristos if u don't know pls stop made it up;) – Harry89pl Aug 20 '12 at 19:58
  • @steventnorris: If you need the id on clientside, you can use the `ClientId` property. – Tim Schmelter Aug 20 '12 at 21:01
1

It's happend because Control which is rendered on server side has autogenerated Client side Id.

If you'll use asp.net out of box control (<asp:Label />and so on) you can set ClientIdMode to Static

if you want to use this span in javascript you can write

$('<%= tickerfade.ClientID %>).click(....)

Harry89pl
  • 2,415
  • 12
  • 42
  • 63