Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ClientID to static in C#

Tags:

html

c#

asp.net

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.

like image 645
Si8 Avatar asked Jan 30 '14 15:01

Si8


1 Answers

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.

like image 57
Habib Avatar answered Nov 01 '22 18:11

Habib