user3432846
user3432846

Reputation: 57

Ajax rating tool in Gridview

Hi I have a problem with my code, I am implementing a rating application. I have connected a access database to my website. I then added in the ajax control and gridview etc.. this is my code so far..

<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>

  <asp:GridView ID="gvwMake" runat="server" DataKeyNames="MachineID"
   BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
   CellPadding="3" AllowPaging="True"                                           
   OnSelectedIndexChanged="gvwMake_SelectedIndexChanged">
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
        <Columns>
            <asp:BoundField DataField="Make" HeaderText="Make" />
            <asp:ImageField DataImageUrlField="Image"></asp:ImageField>
            <asp:TemplateField HeaderText="Machine Rating">
            <ItemTemplate>
                    <cc1:Rating ID="Rating1"
                    AutoPostBack="true" OnChanged="OnRatingChanged" runat="server"
                    StarCssClass="Star" WaitingStarCssClass="WaitingStar" 
                    EmptyStarCssClass="Star"
                    FilledStarCssClass="FilledStar" 
                    CurrentRating='<%# Eval("Rating") %>'>
               </cc1:Rating>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

That all seems to be working fine however the problem is with the code behind.

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;
using AjaxControlToolkit;

public partial class rate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataSet ds = MachineClass.getMachine();
        gvwMake.DataSource = ds.Tables["dtMachine"];
        gvwMake.DataBind();
    }
}

protected void gvwMake_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvwMake.PageIndex = e.NewPageIndex;
    DataSet ds = MachineClass.getMachine();
    gvwMake.DataSource = ds.Tables["dtMachine"];
    gvwMake.DataBind();
}

protected void gvwMake_SelectedIndexChanged(object sender, EventArgs e)
{

    string strID = gvwMake.SelectedRow.Cells[2].Text;
    Session["TID"] = strID;
    Response.Redirect("~/Result.aspx");

}


protected void btnSearch_Click(object sender, EventArgs e)
{
    DataSet ds = MachineClass.getMachine(txtSearch.Text);
    gvwMake.DataSource = ds.Tables["dtMachine"];

    gvwMake.DataBind();

}

private void ShowData()
{

    using (OleDbDataAdapter da = new OleDbDataAdapter(
        "SELECT TOP 20 Products.ProductID, Products.ProductName," +
        " Products.UnitPrice, Products.SupplierID, " +
        "Products.CustomerRating FROM Products",
        new OleDbConnection(
        ConfigurationManager.ConnectionStrings["comac.mdb.accdb"].ToString())))
    {
        DataTable dt = new DataTable();
        da.SelectCommand.Connection.Open();
        da.Fill(dt);
        this.gvwMake.DataSource = dt;
        this.gvwMake.DataBind();
    }

}

protected void Rating1_Changed(object sender,
          AjaxControlToolkit.RatingEventArgs e)
{
    AjaxControlToolkit.Rating myRating =
              (AjaxControlToolkit.Rating)sender;
    System.Text.RegularExpressions.Regex rexLineNo =
      new System.Text.RegularExpressions.Regex("ctl\\d+");

    this.updateRating(this.MachineId(
       rexLineNo.Match(myRating.UniqueID).ToString()), e.Value);

}

private string MachineId(string LineNo)
{
    foreach (GridViewRow r in this.gvwMake.Rows)
    {
        Label lblMachineId = (Label)r.FindControl("lblMachineId");

        if (lblMachineId.UniqueID.Contains(LineNo))
        {
            return lblMachineId.Text;
        }
    }

    return string.Empty;
}

private void updateRating(string MachineId, string Rating)
{

    OleDbParameter paramRating = new OleDbParameter("@Rating", Rating);
    OleDbParameter paramMachineId =
       new OleDbParameter("@MachineId", MachineId);


    using (OleDbCommand cmd = new OleDbCommand(
        "UPDATE Machines SET CustomerRating " +
        "= @Rating WHERE Machines.MachineID=@MachineId",
        new OleDbConnection(
         ConfigurationManager.ConnectionStrings["comac.mdb.accdb"].ToString())))
    {
        cmd.Parameters.Add(paramRating);
        cmd.Parameters.Add(paramMachineId);
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();

    }
}

}

This is my first time using the ajax rating control and not sure if the codes suitable for it when i run the webpage i get an error message 'ASP.rate_aspx' does not contain a definition for 'OnRatingChanged' and no extension method 'OnRatingChanged' accepting a first argument of type 'ASP.rate_aspx' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 1677

Answers (4)

Shekhar Patel
Shekhar Patel

Reputation: 671

first add ajax toolkit from your ajax toolbox and then apply followin coding with css.

<style type="text/css">
.Star
{
    background-image: url(Images/rsz_star-deselect.png);
    height: 17px;
    width: 17px;
}
.WaitingStar
{
    /*background-image: url(images/WaitingStar.gif);*/
    height: 17px;
    width: 17px;
}
.FilledStar
{
    background-image: url(Images/rsz_star-select.png);
    height: 17px;
    width: 17px;
}
</style>

<ItemTemplate>
                <cc1:Rating ID="Rating1" AutoPostBack="true" runat="server"
                    StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star"
                    FilledStarCssClass="FilledStar" CurrentRating='<%# Eval("Rating") %>'>
                </cc1:Rating>
 </ItemTemplate>

Upvotes: 0

Terabyte
Terabyte

Reputation: 455

Okay here is the C# code. You will have to import the following namespaces:

using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using AjaxControlToolkit;

//the page_onload add the following code

 if (!IsPostBack)
 {
        gvFruits.DataSource = GetData("SELECT FruitId, FruitName, ISNULL((SELECT AVG(Rating) FROM Fruit_Ratings WHERE FruitId = Fruits.FruitId), 0) Rating FROM Fruits");
        gvFruits.DataBind();
 }

private static DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        return dt;
    }
}

//and here is the code full functioning app for rating.[Full functioning rating app code][1]



  [1]: http://www.aspsnippets.com/Articles/Using-ASPNet-AJAX-Rating-Control-inside-GridView-TemplateField-ItemTemplate.aspx

Upvotes: 1

Terabyte
Terabyte

Reputation: 455

Okay Try the demo from this website probably you will find it usefull> http://www.aspsnippets.com/Articles/Using-ASPNet-AJAX-Rating-Control-inside-GridView-TemplateField-ItemTemplate.aspx

Upvotes: 0

user3240361
user3240361

Reputation: 417

What I could see from the below part of your code:

<ItemTemplate>
                    <cc1:Rating ID="Rating1"
                    AutoPostBack="true" OnChanged="OnRatingChanged" runat="server"
                    StarCssClass="Star" WaitingStarCssClass="WaitingStar" 
                    EmptyStarCssClass="Star"
                    FilledStarCssClass="FilledStar" 
                    CurrentRating='<%# Eval("Rating") %>'>
               </cc1:Rating>
</ItemTemplate>

is the place where you use the Rating Control, you are assigning OnChanged Event to a Code behind method OnRatingChanged (mentioned as OnChanged="OnRatingChanged"in your code) and since your code behind does not have any method named OnRatingChanged the error is thrown saying 'ASP.rate_aspx' does not contain a definition for 'OnRatingChanged'.

So if you are really not using this event then remove the event (remove the part: OnChanged="OnRatingChanged" from your code) or in case if you are using this then include an appropriate 'OnRatingChanged' method in your code behind.

Upvotes: 1

Related Questions