【问题标题】:Ajax rating tool in GridviewGridview 中的 Ajax 评级工具
【发布时间】:2014-03-18 11:37:19
【问题描述】:

您好,我的代码有问题,我正在实施一个评级应用程序。我已将访问数据库连接到我的网站。然后我在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”的定义,并且找不到接受“ASP.rate_aspx”类型的第一个参数的扩展方法“OnRatingChanged”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • 我已经修复了这个错误,但是每当我尝试运行它时,我都会遇到其他错误,以前有没有人这样做过并且知道代码是否需要更改
  • 您对代码进行了哪些更改以解决该问题以及您遇到的其他错误是什么?

标签: c# asp.net gridview ms-access-2007 ajaxcontroltoolkit


【解决方案1】:

我可以从您的代码的以下部分看到:

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

是您使用 Rating Control 的地方,您将 将 OnChanged 事件分配给 Code behind 方法 OnRatingChanged(在您的代码中称为 OnChanged="OnRatingChanged")并且因为您的 code behind没有任何名为 OnRatingChanged 的​​方法会抛出错误,提示“ASP.rate_aspx”不包含“OnRatingChanged”的定义。

因此,如果您真的不使用此事件,请删除该事件(从您的代码中删除部分:OnChanged="OnRatingChanged"),或者如果您正在使用此事件,则在您的代码后面包含一个适当的“OnRatingChanged”方法。

【讨论】:

  • 是的,它的名称略有不同我已经排序,现在我仍然收到错误 System.InvalidCastException: Specified cast is not valid。
  • @user3432846:此错误可能是由于 DB 端的 diff 数据类型声明。 Ajax Rating 控件的 CurrentRating 属性保存 Initial rating 值。我怀疑你在这里绑定的初始评级值不是int,所以请检查DB中的Rating绑定字段是否有int值(或者它是int类型)
  • @user3432846:此错误可能是由于 DB 端的 diff 数据类型声明。 Ajax Rating 控件的 CurrentRating 属性保存 Initial rating 值。我怀疑你在这里绑定的初始评级值不是int,所以请检查DB中的Rating绑定字段是否有int值(或者它是int类型)
  • 我将其更改为 int 但是数据库中的此字段中没有数据,因为我希望将信息存储在其中。
  • 这是我的跟踪日志 指定的转换无效。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.InvalidCastException:指定的强制转换无效。源错误:
【解决方案2】:

好的,这里是 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

【讨论】:

  • 我已添加此代码,但仍然收到相同的错误 Specified cast is not valid
【解决方案3】:

【讨论】:

    【解决方案4】:

    首先从您的 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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多