views:

621

answers:

5

Is is possible to set clientID of any asp.net server control?

How can i do?

Or any idea ?

+1  A: 

That's not possible. The ClientID is generated by ASP.NET. From MSDN:

The ClientID value is generated by concatenating the ID value of the control and the UniqueID value of its parent control. If the ID value of the control is not specified, an automatically generated value is used.

Ahmad Mageed
+1  A: 

Even i think it is not possible in Visual studio 2008 . Because Control.ClientID Property has only get method

Edit :But in Visual studio 2010(.Net 4.0) it is possible

anishmarokey
+1  A: 

This is an ASP.NET 4.0 feature.

Ryan Hoffman
+4  A: 

The good news is that in VS 2010 .Net 4, you'll have complete control over Client IDs!

Still for the current versions of .Net, you can make due. I assume you need the ID for JavaScript. If so, just get the ID like so:

<script type="text/javascript">
    var myTextBox = $('<%=TextBox1.ClientID%>');
</script>
Michael La Voie
+1 for being a Jedi and reading ebattulga's mind, "Get ClientID I do not Set"
rick schott
A: 

I would advice against doing this unless you are sure you want to do it, but there is a way. You can override the ClientID property from within the server control.

public override string ClientID
{
    get { return "whatever"; }
}

But as others have noted, you can't do it from outside.

Chetan Sastry