Gridview中的Ajax评级工具

本文关键字:工具 Ajax 中的 Gridview | 更新日期: 2023-09-27 17:50:49

嗨,我有一个问题与我的代码,我正在实现一个评级应用程序。我已经连接了一个访问数据库到我的网站。然后我添加了ajax控件和gridview等。这是我到目前为止的代码。

<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>

这一切似乎都很好,但问题是背后的代码。

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();
    }
}

}

这是我第一次使用ajax评级控制,不确定代码是否适合它,当我运行网页,我得到一个错误消息ASP。rate_aspx'不包含'OnRatingChanged'的定义,也没有扩展方法'OnRatingChanged'接受类型为'ASP '的第一个参数。Rate_aspx '可以找到(您是否缺少using指令或程序集引用?)

Gridview中的Ajax评级工具

从下面的代码中我可以看到:

<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>

是您使用评级控制的地方,您是将OnChanged事件分配给代码背后的方法OnRatingChanged(在您的代码中提到的OnChanged="OnRatingChanged"),并且由于您的代码背后没有任何名为OnRatingChanged的方法,错误被抛出说'ASP。rate_aspx'不包含'OnRatingChanged'的定义。

所以如果你真的不使用这个事件,然后删除事件(从你的代码中删除部分:OnChanged="OnRatingChanged"),或者如果你正在使用这个,那么在你的代码后面包含一个适当的'OnRatingChanged'方法。

下面是c#代码。您必须导入以下名称空间:

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

好的,试试这个网站上的演示,也许你会发现它很有用>http://www.aspsnippets.com/Articles/Using-ASPNet-AJAX-Rating-Control-inside-GridView-TemplateField-ItemTemplate.aspx

首先从ajax工具箱中添加ajax工具箱,然后用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>