开发者

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


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);

  });


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜