2013-02-26 58 views
1

我有如下的服務器控制:開發定製asp.net服務器控件呈現腳本代碼

[DefaultProperty("ContentKey")] 
    [ToolboxData("<{0}:ContentRating runat=server></{0}:ContentRating>")] 
    public class ContentRating : WebControl 
    { 
     [Bindable(true)] 
     [Category("Appearance")] 
     [DefaultValue("")] 
     [Localizable(true)] 
     public string ContentKey 
     { 
      get 
      { 
       String s = (String)ViewState["ContentKey"]; 
       return ((s == null) ? "[" + this.ID + "]" : s); 
      } 

      set 
      { 
       ViewState["ContentKey"] = value; 
      } 
     } 

     protected override void RenderContents(HtmlTextWriter output) 
     { 
      StringBuilder builder = new StringBuilder(@"<div id=""divContentRating""> 
    <div id=""divAskForRating""> 
     #Question 
     <br /> 
     <a id=""likeIcon""><img src=""~/Content/Images/like.jpg""/></a> 
     <a id=""neutralIcon""><img src=""~/Content/Images/neutral.jpg""/></a> 
     <a id=""unlikeIcon""><img src=""~/Content/Images/unlike.jpg""/></a> 
    </div> 
    <div id=""divPositiveRating""> 
     <div> 
      <img src=""~/Content/Images/like.jpg""/> #PositiveAnswerMessage <br /> 
      <a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a> 
     </div> 
    </div> 
    <div id=""divNegativeRating""> 
     <div> 
      <img src=""~/Content/Images/unlike.jpg""/> #NegativeAnswerMessage <br /> 
      <a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a> 
     </div> 
    </div> 
    <div id=""divNeutralRating""> 
     <div> 
      <img src=""~/Content/Images/neutral.jpg""/> #NeutralAnswerMessage <br /> 
      <a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a> 
     </div> 
    </div> 

    <input type=""hidden"" id=""HasRated"" value=""False""> 
    <input type=""hidden"" id=""Rate"" value=""Rate""> 
    <input type=""hidden"" id=""ContentKey"" value=""#ContentKey""> 
    <input type=""hidden"" id=""RatingId"" value=""RatingId""> 

    <script type=""text/javascript""> 
    $(document).ready(function() { 
     var protocol = location.protocol; 
     var host = window.location.host; 

     if ($(""#HasRated"").val() == """"True"") 
     { 
      var rate = $(""#Rate"").val(); 
      if (rate == 1) { 
       setPositiveRatedView(); 
      } 
      else if (rate == 0) { 
       setNeutralRatedView(); 
      } 
      else if (rate == -1) { 
       setNegativeRatedView(); 
      } 
      else { 
       setNotRatedView(); 
      } 
     } 
     else { 
      setNotRatedView(); 
     } 

     $(""#likeIcon"").click(function() { 
      alert(""like""); 
      setPositiveRatedView(); 
      ratePage(1, """"); 
     }); 

     $(""#neutralIcon"").click(function() { 
      alert(""neutral""); 
      setNeutralRatedView(); 
      ratePage(0, """"); 
     }); 

     $(""#unlikeIcon"").click(function() { 
      alert(""unlike""); 
      setNegativeRatedView(); 
      //mkMPopClc('NegativeRatingReason', 200, 300, 0, 0); 
     }); 

     $("".updateRate"").click(function() { 
      setNotRatedView(); 
     }); 

//  $('.clsStl').click(function() { 
//   ratePage(-1, """"); 
//   $('.mkMPop').fadeOut(); 
//  }); 
// 
//  $('#ShareComment').click(function() { 
//   ratePage(-1, $(""#Comment"").val()); 
//   $('.mkMPop').fadeOut(); 
//  }); 

     function setNotRatedView() { 
      $(""#divNeutralRating"").fadeOut(); 
      $(""#divPositiveRating"").fadeOut(); 
      $(""#divAskForRating"").fadeIn(); 
      $(""#divNegativeRating"").fadeOut(); 
     } 

     function setPositiveRatedView() 
     { 
      $(""#divNegativeRating"").fadeOut(); 
      $(""#divNeutralRating"").fadeOut(); 
      $(""#divAskForRating"").fadeOut(); 
      $(""#divPositiveRating"").fadeIn(); 
     } 

     function setNegativeRatedView() { 
      $(""#divNeutralRating"").fadeOut(); 
      $(""#divPositiveRating"").fadeOut(); 
      $(""#divAskForRating"").fadeOut(); 
      $(""#divNegativeRating"").fadeIn(); 
     } 

     function setNeutralRatedView() { 
      $(""#divNegativeRating"").fadeOut(); 
      $(""#divPositiveRating"").fadeOut(); 
      $(""#divAskForRating"").fadeOut(); 
      $(""#divNeutralRating"").fadeIn(); 
     } 

     function ratePage(rating, comment) 
     { 
      //alert(rating + """" """" + comment); 
      var contentKey = $(""#ContentKey"").val(); 
      var hasRated = $(""#HasRated"").val(); 
      var ratingId = $(""#RatingId"").val(); 

      $.getJSON(protocol + '//' + host + '/tr/Rating/RatePage?contentKey=' + contentKey + '&rating=' + rating + '&ratingUpdate=' + hasRated + '&ratingId=' + ratingId + '&comment=' + comment, function (data) { 
       $(""#HasRated"").val(data.HasRated); 
       $(""#Rate"").val(data.Rate); 
       $(""#ContentKey"").val(data.ContentKey); 
       $(""#RatingId"").val(data.RatingId); 
       $(""#Comment"").val(data.Comment); 
      }); 
     } 
    }); 
</script> 
</div>"); 

      builder.Replace("#ContentKey", this.ContentKey); 

      output.Write(builder); 
     } 
    } 

當我加入我的控制,我的網頁,我看到控制渲染,因爲我期待,但jQuery腳本不起作用。在服務器控件中保存腳本代碼是錯誤的嗎?我能做些什麼來解決這個問題?

+0

有您控制一個潛在的問題:$(文件)。就緒(函數(){...})。如果您可以使用PreRender事件並使用Page.RegisterClientScript註冊JS,那麼最好。 – 2013-02-28 06:18:32

回答

2
if ($(""#HasRated"").val() == """"True"") 

我認爲你有太多的引號字符有...

+0

你說得對。謝謝 – anilca 2013-02-26 12:52:16

相關問題