Set ClientID in asp.net

19,209

Solution 1

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>

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

ASP.NET 4 has ClientIDMode property on each control for that. If you want to turn off ClientID completely you can use this trick - it works for any non-postback control

Share:
19,209

Related videos on Youtube

ebattulga
Author by

ebattulga

using Stackoverflow.com; using Superuser.com; using Google.com; namespace dotnet{ public class Developer { public static Developer ebattulga; public void Ask(string question){...} } }

Updated on June 04, 2022

Comments

  • ebattulga
    ebattulga almost 2 years

    Is it possible to set the ClientID of any asp.net server control? How can I do this?

    • Dan Diplo
      Dan Diplo over 14 years
      Explain what you are trying to achieve and then we might be able to suggest an alternative.
  • rick schott
    rick schott over 14 years
    +1 for being a Jedi and reading ebattulga's mind, "Get ClientID I do not Set"
  • KSwift87
    KSwift87 over 10 years
    Don't forget to put the # sign in front of the <%= pending you want to get the actual DOM object and not just the text of the control's ClientID.
  • Yousaf
    Yousaf over 10 years
    Thx @Kswift87 that's absolutely right. i've updated my answer
  • FrenkyB
    FrenkyB about 7 years
    How is it possible? In VS2015 this method is still only get.