Jonathan Eckman
Jonathan Eckman

Reputation: 2077

C# Get id of dynamically created labels

I need to be able to set the IDs of dynamically generated labels instead of letting SharePoint prefix my labels with a long cryptic id of its own. Is this possible or is there another property of label that I can use as a unique identifier in a separate method?

Label animal = new Label();
animal.ID = cat;

The id for this label will be something like: ctl00_m_g_e0c173c0_edf3_4a99_a1dd_7bef33144c0b_ctl00_cat

I need it to be cat.

Upvotes: 0

Views: 1206

Answers (3)

Netricity
Netricity

Reputation: 2738

To force the client-side id to be the same as the server-side id, use:

animal.ClientIDMode = ClientIDMode.Static;

Upvotes: 1

Gromer
Gromer

Reputation: 9931

If you're attempting to access the control with javascript and are using jQuery, you can just the various regex selectors:

$("label[id$='cat']")

That will look for a label element with an id that ends with 'cat'

If your javascript is in the aspx file, and not a js file, you can also access it like so:

$('#<%=cat.ClientID %>')

That will just inject that long id created by Sharepoint into your js. Key is it needs to reside in the aspx file, not a js file.

Upvotes: 0

labroo
labroo

Reputation: 2961

You cannot set the ClientId of a server control. It is a read only property.

The cryptic id you see is actually your control heirarchy.

Upvotes: 0

Related Questions