在GridView的阿贾克斯评级工具 [英] Ajax rating tool in Gridview

查看:156
本文介绍了在GridView的阿贾克斯评级工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有我的code的一个问题,我实现一个等级的应用程序。我已经连接一个数据库的访问我的网站。然后,我在AJAX控件和GridView等..加
这是我的code到目前为止..

 < CC1:ToolkitScriptManager ID =ToolkitScriptManager1=服务器>
< / CC1:ToolkitScriptManager>  < ASP:GridView控件ID =gvwMake=服务器的DataKeyNames =机号
   背景色=白BORDERCOLOR =#CCCCCC边框=无边框宽度=1像素
   CELLPADDING =3AllowPaging =真
   OnSelectedIndexChanged =gvwMake_SelectedIndexChanged>
        < FooterStyle背景色=白前景色=#000066/>
        < HeaderStyle背景色=#006699FONT-粗体=真前景色=白/>
        < PagerStyle背景色=白前景色=#000066Horizo​​ntalAlign =左/>
        < RowStyle前景色=#000066/>
        < SelectedRowStyle背景色=#669999FONT-粗体=真前景色=白/>
        < SortedAscendingCellStyle背景色=#F1F1F1/>
        < SortedAscendingHeaderStyle背景色=#007DBB/>
        < SortedDescendingCellStyle背景色=#CAC9C9/>
        < SortedDescendingHeaderStyle背景色=#00547E/>
        <柱体和GT;
            < ASP:BoundField的数据字段=制作的HeaderText =做/>
            < ASP:ImageField的DataImageUrlField =图片>< / ASP:ImageField的>
            < ASP:的TemplateField的HeaderText =机评>
            <&ItemTemplate中GT;
                    < CC1:等级ID =额定值1
                    的AutoPostBack =真=调用onChangedOnRatingChanged=服务器
                    StarCssClass =星WaitingStarCssClass =WaitingStar
                    EmptyStarCssClass =星
                    FilledStarCssClass =FilledStar
                    CurrentRating ='<%#的eval(等级)%GT;'>
               < / CC1:评级>
< / ItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
< / ASP:GridView的>

这一切似乎都工作正常但问题是,与code后面。

 使用系统;
System.Collections中使用;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data这;
使用System.Data.OleDb;
使用System.Configuration;
使用System.Data.SqlClient的;
使用AjaxControlToolkit;公共部分类率:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        DataSet的DS = MachineClass.getMachine();
        gvwMake.DataSource = ds.Tables [dtMachine];
        gvwMake.DataBind();
    }
}保护无效gvwMake_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
{
    gvwMake.PageIndex = e.NewPageIndex;
    DataSet的DS = MachineClass.getMachine();
    gvwMake.DataSource = ds.Tables [dtMachine];
    gvwMake.DataBind();
}保护无效gvwMake_SelectedIndexChanged(对象发件人,EventArgs的发送)
{    串strID = gvwMake.SelectedRow.Cells [2]。文本;
    会话[TID] = strID;
    的Response.Redirect(〜/ Result.aspx);}
保护无效btnSearch_Click(对象发件人,EventArgs的发送)
{
    DataSet的DS = MachineClass.getMachine(txtSearch.Text);
    gvwMake.DataSource = ds.Tables [dtMachine];    gvwMake.DataBind();}私人无效ShowData()
{    使用(DA OleDbDataAdapter的=新OleDbDataAdapter的(
        SELECT TOP 20 Products.ProductID,Products.ProductName,+
        Products.UnitPrice,Products.SupplierID,+
        Products.CustomerRating产品来自
        新的OleDbConnection(
        ConfigurationManager.ConnectionStrings [comac.mdb.accdb]。的ToString())))
    {
        DataTable的DT =新的DataTable();
        da.SelectCommand.Connection.Open();
        da.Fill(DT);
        this.gvwMake.DataSource = DT;
        this.gvwMake.DataBind();
    }}保护无效Rating1_Changed(对象发件人,
          AjaxControlToolkit.RatingEventArgs五)
{
    AjaxControlToolkit.Rating myRating =
              (AjaxControlToolkit.Rating)发送;
    System.Text.RegularEx pressions.Regex ​​rexLineNo =
      新System.Text.RegularEx pressions.Regex(CTL \\\\ D +);    this.updateRating(this.MachineId(
       rexLineNo.Match(myRating.UniqueID)的ToString()),e.Value);}私人字符串设备ID(字符串LINENO)
{
    的foreach(在this.gvwMake.Rows GridViewRow R)
    {
        标签lblMachineId =(标签)r.FindControl(lblMachineId);        如果(lblMachineId.UniqueID.Contains(LINENO))
        {
            返回lblMachineId.Text;
        }
    }    返回的String.Empty;
}私人无效updateRating(串机号,串等级)
{    OleDbParameter paramRating =新OleDbParameter(@等级,评级);
    OleDbParameter paramMachineId =
       新OleDbParameter(@机号,机号);
    使用(OleDbCommand的CMD =新的OleDbCommand(
        UPDATE机SET CustomerRating+
        @评估= WHERE Machines.MachineID=@MachineId
        新的OleDbConnection(
         ConfigurationManager.ConnectionStrings [comac.mdb.accdb]。的ToString())))
    {
        cmd.Parameters.Add(paramRating);
        cmd.Parameters.Add(paramMachineId);
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();    }
}

}

这是使用AJAX等级控制和不知道,如果codeS适合它,当我运行网页,我得到一个错误消息,我第一次
ASP.rate_aspx'不包含'OnRatingChanged的定义,并没有扩展方法'OnRatingChanged接受型ASP.rate_aspx'的第一个参数可以找到(是否缺少using指令或程序集引用?)


解决方案

好吧这里是C#code。您必须导入下列命名空间:

 使用System.Data这;
使用System.Configuration;
使用System.Data.SqlClient的;
使用AjaxControlToolkit;//将page_onload添加以下code 如果(!的IsPostBack)
 {
        gvFruits.DataSource =的GetData(SELECT FruitId,FruitName,ISNULL((SELECT AVG(额定值)Fruit_Ratings WHERE FruitId = Fruits.FruitId),0)评级从水果);
        gvFruits.DataBind();
 }私有静态数据表的GetData(查询字符串)
{
    DataTable的DT =新的DataTable();
    字符串构造= ConfigurationManager.ConnectionStrings [构造]的ConnectionString。
    使用(SqlConnection的CON =新的SqlConnection(构造))
    {
        使用(CMD的SqlCommand =新的SqlCommand(查询))
        {
            使用(SqlDataAdapter的SDA =新SqlDataAdapter的())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = CON;
                sda.SelectCommand = CMD;
                sda.Fill(DT);
            }
        }
        返回DT;
    }
}//这里是评级code全功能的应用程序。[全功能的评价程序code] [1]  [1]: http://www.aspsnippets.com/Articles/Using-ASPNet-AJAX-Rating-Control-inside-GridView-TemplateField-ItemTemplate.aspx

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

解决方案

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

这篇关于在GridView的阿贾克斯评级工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆