In asp.net, server side control have a property called ClientID, which can be used to get the id that template engine generated for a server side control, is there a similar thing in SilverStripe, to get the id of the input element rather than its .field wrapper?
thanks
Have a look at the ID method of FormField
http://api.silverstripe.org/3.1/class-FormField.html
In your templates, you should be able to use
$Fields.FieldByName(FieldName).ID
Related
Im trying to understand how the 'name' attribute works for elements in markup rendered via ASP.NET
I.e. <select id="lblxyz" name="ctl00$c$ctl341$lblxyz">
What are ctl00, c and ctl1341?
If I create a WebControl, give it an arbitrary id, then place a Button inside its Controls collection, the name attribute does not seem to reflect the container's id?
The key thing as you have discovered is that not all controls trigger this behaviour. The key is the INamingContainer interface. Only Naming Containers will contribute to the name of the control as you have seen, other controls won't.
The MSDN page linked above says it pretty well: that interface "identifies a container control that creates a new ID namespace within a Page object's control hierarchy."
The generated name attribute typically always starts with ctl00, and the rest is generated based on where the control lives in the control hierarchy. Any parent controls implementing the INamingContainer interface will contribute to the names generated for any child controls.
This may be of interest to you, it outlines out the id/names are generated: http://www.mostlylucid.net/archive/2008/11/03/way-too-much-information-on-control-ids-and-asp.net-4.0.aspx
You can control how the client ID appears using the new ClientIDMode:
All about Client ID Mode in ASP.NET 4
I am creating a web user control for a simple poll. I am currently registering it on the page and then referencing it via tagprefix.
The form for the poll is in basic html (no server controls) and is in the front-end of the web control. How can I change the look of the user control depending on the settings passed into it? Is this possible without using server controls?
Update
Can I change the html layout of a user control? If so could someone post some examples. Please note I do not use asp.net form controls, so none of that please :)
You might be able to also use jQuery to replace existing css setting in your code. Create properties on for your user control, and then pass settings in the classes. Then use jQuery to replace them. This however requires jQuery to be linked to your page (or within your control) and you'd have to write the CSS classes out to the jQuery code (using server controls, but you could use the literal control so there's no excess code).
Personally I'd go with the option of using server controls instead of straight up HTML, you'd get alot more flexibility, and then passing through the settings would be pretty straightforward, put something like this in your controls backend code:
Private _TextBoxCssClass As String
Public Property TextBoxCssClass() As String
Get
Return _TextBoxCssClass
End Get
Set(ByVal value As String)
_TextBoxCssClass = value
txtBox1.CssClass = value
txtBox2.CssClass = value
End Set
End Property
You most likely want to have a property or event in the control that changes the css. It may end up best to add some server controls or javascript / jquery to make it easier.
If its only the styles you want to change, then you can expose a property to set the style attribuites of the respective control inside your User Control. If you want to control the whole HTML layout of the control then Custom Control is the viable option.
If you create a table and add it, the ClientID property in the code behind doesn't reflect the ID in the actual file, so document.getElementById doesn't work.
How can I add a control, and be able to access it using Javascript, with only its ID (such as 'table1')?
You will have to render the "ClientId" out to the browser and then grab that value to be able to use it via Javascript.
Starting with .NET 4.0, this will no longer be an issue as you can change the behavior of the iNamingContainer and the id structures.
You could set a javascript variable on the fly to your new table's ClientID and then use its value in getElementById.
This I can't work out
I have ASP.Net user control,which is basically two drop downs, that has a public property Index which is calculated from the drop downs, and works fine
I need to access the value of 'Index' from javascript, but accessing via getElementById was completely wrong, can anybody point me in a better direction
Cheers
The property you're talking about is a server side thing and has no presence in Javascript. Basically, whatever ASP.NET does on the server will generate a single HTML page containing a bunch of HTML tags. The browser is not aware of the user control being a single entity at all.
You should calculate the property on the client by looking up the ID of the drop down directly in Javascript. You can do find out the client ID of the drop down by getting its ClientID property.
Create javascript method as a string from server side and use index property to create script string.
register string on which event it is required.
you can take help from following link to register javascript:
http://msdn.microsoft.com/en-us/library/aa479011.aspx
If any server side control is used in javascript method, use ClientID property as ID of the control.
Hope, It helps
I am working on ASP.NET and not using any ASP.NET's AJAX framework. Now I am trying to update the contents of the textboxes and dropdowns in the Grid controls cell on client side using (classic JavaScript way)AJAX. But problem I am facing is that controls (textbox, dropdown) which I would like to update when rendered on the client side get prefixed by the User Controls id and row ids.
So it becomes like UserContro_row_no_controlId. With this it is becoming difficult to keep track of controls ids on the client side to update them.
Please let me know how this can be simplified?
Can we get what exactly will be prefixed to control when it’s rendered on client side?
What is good way to get ID to client side ? I have tried using control.clientId but it’s giving me only _controlId part not the UserContro_row_no part.
Thanks all,
If you want the id of a control in the same control/page you can do that this way:
<%=txtControl.ClientID%>
If you want the id of a control inside a usercontrol you can do that like this:
<%=userControl.FindControl("txtControl").ClientID%>
Where "txtControl" is the server id of the control and "userControl" is the server id of the usercontrol.