Can I count on ctl00_PagePlaceHolder_myId to stay the same?

I need to click something on my ASP.NET web form page from a Silverlight control.

I set the identifier of my text field and, as it wants, ASP.NET “useful” adds to ctl ... blah_blah. Can I assume that it will always be the same gumpf that it puts at the beginning of my identifier?

Kindness,

Dan

EDIT: Thanks for the answers to the guys.

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

var spec = HtmlPage .Document .GetElementsByTagName("input") .FirstOrDefault(so => ((HtmlElement)so).CssClass == "spec"); 
+4
source share
3 answers

No, this can easily change if you ever add another control in front of it or nest it in another control. In addition, the identifier differs depending on the browser. In some browsers, .NET is split using _ , other browsers use $ . I did not work with Silverlight, but can you find an element with a specific class? You can apply the class name to your element, and ASP.NET will not edit it at all.

+4
source

Not. In some cases, this can be done differently, and changes elsewhere on the page may change this control. The proper way to handle this is to reference the control property .ClientID .


Update

This is how I usually handle client identifiers:

 <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 is available to other script files; this way I don't need to transfer javascript files through the asp.net processor, and I only have to write out each client identifier once. There is no need to worry about avoiding anything because the characters "and" will never be part of client identifiers. The reason for the controlIDs object is to avoid naming collisions with other scripts, and the reason for concatenating "" on the front panel in the server code is because asp.net will not see <% otherwise.

Again: this is what I usually do, but for some simple pages this might be redundant.

+4
source

This may not help you right now, but it is worth noting that .NET 4.0 has a new ClientIDMode property that will ease the headaches surrounding this behavior. In particular, using the Predictable and Static settings will result in easy identifier identification. Take a look at the previous link and scroll down for an example of how the predicted mode will look. I also suggest reading ASP.NET Web Server Authentication .

0
source

All Articles