AJAX Rating Control using EDM with LINQ


Code in ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .empty
    {
        background-image:url(ratingStarEmpty.gif);
        width:18px;
        height:18px;
        }
     .filled
     {
         background-image:url(ratingStarFilled.gif);
         width:18px;
         height:18px;
         }
     .saved
     {
         background-image:url(ratingStarSaved.gif);
         width:18px;
         height:18px;
         }
    </style>
</head>
<body>
    <form id="form1" runat="server">
<asp:ToolkitScriptManager
ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
    <div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Rating ID="Rating1" runat="server"
AutoPostBack="True"
onchanged="Rating1_Changed"
StarCssClass="empty"
EmptyStarCssClass="empty"
FilledStarCssClass="filled"
WaitingStarCssClass="saved">
</asp:Rating>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label1"
runat="server"
style="font-weight: 700; color: #FF0000"></asp:Label>
<br />
ASP.NET Info blog&nbsp; is helpful...
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
Code behind:
RatingModel.RatingEntities rt = new RatingModel.RatingEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    protected void GetData()
    {
        int total = 0;
        var x = (from rating in rt.Ratings
              select rating.Rating1);
        if (x != null)
        {
            for (int i = 0; i < x.Count(); i++)
            {
                total += i;
            }
            int average = (total / x.Count());
            if (average <= 5)
                Rating1.CurrentRating = average;
            Label1.Text = x.Count() + "User(s) have this blog Rated...";
        }
    }
    protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        RatingModel.Rating r = new RatingModel.Rating();
        r.Rating1 = Rating1.CurrentRating;
        rt.Ratings.AddObject(r);
        rt.SaveChanges();
        GetData();
    }

DownloadSampleCode

Advertisements

One thought on “AJAX Rating Control using EDM with LINQ

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s