Search code examples
c#asp.netajaxcontroltoolkit

Retrieve Ajax star rating value to Asp.net backend


This is my rating control in aspx page. When user click the star, i want to update the label3.

<style type="text/css">
    .Star {
        background-image: url(img/Star.gif);
        height: 17px;
        width: 17px;
    }

    .WaitingStar {
        background-image: url(img/WaitingStar.gif);
        height: 17px;
        width: 17px;
    }

    .FilledStar {
        background-image: url(img/FilledStar.gif);
        height: 17px;
        width: 17px;
    }
</style>
<div>

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <ajaxToolkit:Rating ID="Rating1" AutoPostBack="true" OnChanged="OnRatingChanged" runat="server"
                StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star" CurrentRating="2"
                FilledStarCssClass="FilledStar">
            </ajaxToolkit:Rating>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Label runat="server" ID="label3"></asp:Label>
    <asp:Label runat="server" ID="label4" Text="above"></asp:Label>
</div>

I tried out using Rating1.CurrentRating.ToString() but there is nothing on Label3.

protected void OnRatingChanged(object sender, RatingEventArgs e)
    {
        label3.Text = Rating1.CurrentRating.ToString();
    }

Is my code wrong or I can get the value in another way? I'm planning to get the value from backend because later I will add it into my database. Please help. Thank you.


Solution

  • I think what are you looking for is OnClick() not OnChange(), as when user click on Rate control and change it's rating value you want to get current value of Rate control and show it in a label, BTW you should do following below desc,
    before doing anything think about the properties of UpdatePanel like UpdateMode and ChildrenAsTriggers that you can set them like UpdateMode="Always" ChildrenAsTriggers="true" for UpdatePanel1 if by setting them it still doesnt work do the below steps:

    1- Firstly move that label controls into the ContentTemplate of UpdatePanel
    2- Then handle Click() event of ajaxToolkit:Rating
    3- At the end you must have something like in design mode:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <ajaxToolkit:Rating ID="Rating1" AutoPostBack="true" runat="server"
                StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star"
                FilledStarCssClass="FilledStar" OnChanged="OnRatingChanged" OnClick="Rating1_Click">
            </ajaxToolkit:Rating> 
            <asp:Label runat="server" ID="label3"></asp:Label>
            <asp:Label runat="server" ID="label4" Text="above"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    and the code behind for Rating1_Click:

    protected void Rating1_Click(object sender, RatingEventArgs e)
    {
        label3.Text = Rating1.CurrentRating.ToString();
    }
    

    this works like a charm!!!.