|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Getting the generated name attribute for use in JavaScriptAs most of us probably know, ASP.NET controls do not use an exact copy of a
control's ID for the HTML element's name or id attribute. When generating JavaScript as part of a control or event, we need to have a way to reference the HTML elements. The only way I have found to reference the HTML elements in my generated Javascript is the following: "document.getElementById('" & Me.ctrl.ClientID & "')" However, this seems like a lot of extra code for each element. Why do the controls have a ClientID property but not a ClientName property? If there were a ClientName property, wouldn't it make the code a lot less complicated and shorten the amount of JavaScript that had to be included with each control? Or is this just one of those Microsoft things where they are trying to get people to use the id attribute rather than the name attribute? "Nathan Sokalski" <njsokal***@hotmail.com> wrote in You can potentially have two objects using the same ClientName - i.e.:news:ecZHJ8qCHHA.4740@TK2MSFTNGP03.phx.gbl: > Why do the > controls have a ClientID property but not a ClientName property? If > there were a ClientName property, wouldn't it make the code a lot less > complicated and shorten the amount of JavaScript that had to be > included with each control? Or is this just one of those Microsoft > things where they are trying to get people to use the id attribute > rather than the name attribute? > Form +MyLabel +MyUserControl +MyLabel Thus the Form can have a MyLabel and a MyUserControl MyLabel... so if you were to refer to MyLabel, which label is it? :-) But in anycase, you *could* refer to objects by name ... if you know the exact layout of the page. If you take a close look at the element IDs, they're generated the same each time (provided the control nesting does not change). Do you have specific example where this behavior is causing you a
problem? Maybe there is another way to approach it. I mostly use the "this" keyword with function copying or pass in "this" with function referencing. ..Attributes.Add("onclick", "return doSomething(this);") ID is unique, name isn't so I am not sure what name buys you except more complexity, unless you have some reason to be using getElementsByName. >>"document.getElementById('" & Me.ctrl.ClientID & "')" Moreover, to generalise the routine that might hold the code above,perhaps pass in the control ID you are interested in working with. Button1.Attributes.Add("onclick", string.Format("return doSomething('{0}')",AnotherControl.ClientID)) Or pass in a direct reference to the other control.
FormView, with 2 dropdownlists
<asp:Repeater>, checkbox values and ASP.NET 2.0 GridView with objectdatasource Searching for a powerfull calendar addon User Control used in Repeater, but no output difficulty setting time values in Calendar Microsoft ASP.NET 2.0 Treeview not being W3C compliant? Gridview -- changing field content Retrieving all properties of control how to -asp.net Multiview and Validation |
|||||||||||||||||||||||