Si8
Si8

Reputation: 9225

Set ClientID to static in C#

I have a C# code where I am setting the ID of a DIV.

Example:

div.ID = "slider";

The only issue is when the website loads ASP adds a whole bunch of random characters in front of the ID. I cannot use the <% Page %> because I am just using a Web Part.

When I tried to set:

div.ClientID = "static";

I am given an error where the property is read only.

Is there any other way to set it within C#, so the ID does not change when the website runs my page?

I do not want to use <%= slider.ClientID %> in the javascript/css code but if that's the only way then I guess I have no choice.

Upvotes: 6

Views: 16953

Answers (1)

Habib
Habib

Reputation: 223287

You are looking for ClientIDMode property, not ClientID

div.ClientIDMode = System.Web.UI.ClientIDMode.Static;

If you want to define it in aspx page then you can do:

<div id="myDiv" runat="server" ClientIDMode="static" ></div>

Remember it is provided with .Net framework 4.0 and above.

Upvotes: 27

Related Questions