views:

111

answers:

3

Hello the 'flow!

I need to push something into my ASP.NET web form page from a Silverlight control.

I set the ID of my text box and, as is it's want, ASP.NET "helpfully" adds on the ctl...blah_blah stuff. Can I assume this will always be the same gumpf it puts on the beginning of my id?

Kindness,

Dan

EDIT: Thanks for the responses guys.

Using the HtmlPage.Document from my Silverlight control, I used Doug's solution as such ...

var spec = HtmlPage
            .Document
            .GetElementsByTagName("input")
            .FirstOrDefault(so => ((HtmlElement)so).CssClass == "spec");
+4  A: 

No, it could easily change if you ever add another control in front of it, or nest it in another control. Additionally, the id is different depending on the browser. In some browsers .NET splits with _ other browsers use $. I haven't worked with Silverlight, but can you find an element with a certain class? You could apply a class name to your element and ASP.NET won't edit it at all.

Doug Neiner
Thanks, Doug. Nice idea RE: cssclass ... I'll give that a shot.
Daniel Elliott
Edited question with my use of your solution ... thanks again to you and Joel!!
Daniel Elliott
+4  A: 

No. In some circumstances it can be rendered differently and changes elsewhere in the page might change this control. The correct way to handle this is to reference the control's .ClientID property.


Update

This is how I normally handle client ids:

<head runat="server">
   ...
   <script language="javascript">
   var ControlIDs = {
       SomeControl = <%="'" + SomeControl.ClientID%>',
       OtherControl = <%="'" + OtherControl.ClientID%>'
   };
   </script>

   ... other script references here ...
</head>

I put the script first in the head element so that it's accessible to other script files; this way I don't need to pass javascript files through the asp.net processor and I only have to write out each client id once. There's no need to worry about escaping anything, because the ' and \ characters will never be part of a rendered client ids. The reason for the controlIDs object is to avoid naming collisions with other scripts, and the reason for concatenating the "'" at the front in server code is that asp.net won't see the <% otherwise.

Again: this is what I normally do, but for some simple pages it might be overkill.

Joel Coehoorn
Thanks, Joel! Could you give me an example of some circumsatnces when this is the case, please?
Daniel Elliott
See @Doug's answer. He pretty well covers it. The point is that you need to use the ClientID property.
Joel Coehoorn
@Joel, what is the best way to pass it out? `<script type="text/javascript">var myid = "<%= PagePlacehodler.ClientID %>"</script>` Something like that?
Doug Neiner
I'll append my normal method to the answer.
Joel Coehoorn
Nice, +1 great answer
Doug Neiner
A: 

This may not help you right now, but it is worth noting that .NET 4.0 features a new ClientIDMode property which will alleviate the headaches surrounding this behavior. In particular, using the Predictable and Static settings will yield easily determined IDs. Take a look at the previous link and scroll down for an example of what the predictable mode would look like. I also suggest reading ASP.NET Web Server Control Identification.

Ahmad Mageed