Salvatore
Salvatore

Reputation: 31

How to pass a value in a .net label through Javascript

I am coding in c# and javascript and I have some problems with setting a value through a javascript .js file, in order to use it in the code behind.

This is the .js file code

$(document).ready(function() {


$('#example1').ratings(10).bind('ratingchanged', function(event, data) {
    $('#examplerating1').text(data.rating);
$('#lblRating').text(data.Rating);

  });

And this is the html page code.

 Your Rating: <span id="examplerating1">not set</span>
            <asp:Label ID="lblRating" runat="server" Text="Label"></asp:Label>

Now the span value is setted correctly if I not use the runat="server". Now in order to access the value in the code behind i tried to put it in a label. But the value is not setted in the label. How can I use the data.rating value in the code behind passing though the HTML page?

Thanks

Upvotes: 0

Views: 786

Answers (2)

Katie Kilian
Katie Kilian

Reputation: 6993

Check the rest of your code. If you have the label inside another .NET control, such as a master page, the span ID generated by .NET isn't the same as the ID you assigned in the .aspx file. For example, if it is inside a Content control named ContentPlaceHolder1, the ID will be ctl00_ContentPlaceHolder1_lblRating. As Cybernate says above, the ID that is generated depends on the NamingContainer that the label is part of.

Upvotes: 0

Chandu
Chandu

Reputation: 82913

ASP.Net controls ID gets changed based on the NamingContainer.

Change your jQuery to as follows:

$(document).ready(function() {
   $('#example1').ratings(10).bind('ratingchanged', function(event, data) {
    $('#examplerating1').text(data.rating);
    //Notice the change in the selector here.
    $('[id$="_lblRating"').text(data.Rating);

  });

Upvotes: 1

Related Questions