Get management server id using jQuery

How to get control server id using jQuery?

eg. I have

<asp:Label ID="label1" runat="server""></asp:Label>

and now I want to get "label1",

 var id = ?? 
+7
source share
6 answers

If you are using ASP.NET 4.0, you can set the ClientIDMode = "Static" attribute, and your code will look like this:

 <asp:Label ID="label1" runat="server" ClientIDMode="Static"></asp:Label> 

JS:

 var id = 'label1'; 
+11
source
 var labelID = $('#<%= label1.ClientID %>'); 

You need to get the client ID.

If you only need the identifier and not the actual value of the control, you don't even need jQuery.

 var labelID = '<%= label1.ClientID %>'; 
+9
source
 var $lblObj = $("label[id$='label1']:first") 
+3
source

jQuery runs on the client side, so it can only access the identifier of the html element, and not the asp control on the server.

0
source

Do you use the main page. If so, give the ContentPlaceHolderID along with the ID of the control.

For example:

  jQuery("#ContentPlaceHolderID_ControlId").val; jQuery("#body_label1").text; 

You can see it in the view.

0
source

Labels are displayed as span tags. Therefore, if you want to select all the shortcuts:

  $(document).ready(function() { $Labels = $("span"); $Labels.each(function() { alert(this.id); }); }); 
-one
source

All Articles