1

我有一个服务器控件如下:

    [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 脚本不起作用。将脚本代码保留在服务器控件中是错误的吗?我能做些什么来解决这个问题?

4

1 回答 1

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

我认为您那里的引号字符太多...

于 2013-02-26T12:09:33.240 回答