Govind Malviya
Govind Malviya

Reputation: 13763

Can I set Desired ClientID of dynamic Generated controls

I want to set desired ClientID in dynamic generated controls. ClientID is read only if you have any idea to set desired ClientID to controls than tell me

Upvotes: 2

Views: 2169

Answers (2)

Ozgur Ozturk
Ozgur Ozturk

Reputation: 1305

In ASP.Net 4, you can set "ClientIDMode" of controls to "Static", so you can assign your own id.

Panel imageCard = new Panel
        {
            ID = dr["DisplayOrder"].ToString(),
            CssClass = "portlet portlet-family",
            ClientIDMode = System.Web.UI.ClientIDMode.Static 
        };

ClientID doesn't have a setter. In this mode, the clientID will be the same as ID.

If a static ClientIDMode is used in a repeating control the developer is responsible for ensuring client side ID uniqueness. (More: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx)

Upvotes: 0

Markive
Markive

Reputation: 2400

This can only be done in .Net 4 onwards

It shouldn't matter though, for CSS always use classes which asp.net won't touch and for javascript you can sprinkle:

$("#<%=myElement.ClientID%>").blah()

So whatever .Net decides the ID will be it will all wire up correctly:

$("#ct101_myElement").blah();

I use this sort of thing extensively..

Upvotes: 4

Related Questions