如何在Datalist中显示星级。 [英] How Can I Display The Star Rating In The Datalist.

查看:81
本文介绍了如何在Datalist中显示星级。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示我在datalist内部的网页中使用的星级评分。但是它不起作用。有人可以帮我提供代码。



ProductDetails.aspx



I want to display the star rating i am using in my webpage inside the datalist.But it is not working. Can someone help me with the code.

ProductDetails.aspx



< asp:SqlDataSource ID =SqlDataSource1runat =serverConnectionString =<%$ ConnectionStrings:prdcts_accsrsConnectionString1%>

SelectCommand =SELECT [ProductID],[名称],[描述],[价格],[图片路径]来自[产品] WHERE([ProductID] = @ProductID)>

< SelectParameters>

< asp:QueryStringParameter Name =ProductIDQueryStringField =ProductIDType =Decimal/>

< / SelectParameters>






<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:prdcts_accsrsConnectionString1 %>"
SelectCommand="SELECT [ProductID], [Name], [Description], [Price], [ImagePath] FROM [Products] WHERE ([ProductID] = @ProductID)">
<SelectParameters>
<asp:QueryStringParameter Name="ProductID" QueryStringField="ProductID" Type="Decimal" />
</SelectParameters>




< asp:DataList ID =DataList1runat =serverDataSourceID =SqlDataSource1>

< itemtemplate>



< asp:Image ID =Image1runat =serverImageUrl ='<%#Eval(ImagePath,〜/ prod ucts / Accessories / Images \\ {0})%>'Height =400pxWidth =500px/>




< asp:Label ID =Label4runat =serverText ='<%#Eval(ProductID)%>'Visible =false> ;



< asp:标签ID =NameLabelrunat =serverText ='<%#Eval(Name)%>'样式=font-size:40px;颜色:rgb(17,52,72)>




< asp:标签ID =BrandLabelrunat =serverText =按品牌Style =颜色:rgb(43,431,182)>






< asp:Label ID =Label1 runat =serverText =Availability:Style =font-size:25px> < asp:Label



ID =Label2 runat =serverText =In StockStyle =font-size:20px>





< asp:标签ID =Label5runat =serverText =₹Style =font-size:20px;颜色:绿色>< asp:标签ID =PriceLabelrunat =serverStyle =font-size:20px;颜色:绿色Text ='<%#Eval(Price,{0:## 0.00})%>'>




<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<itemtemplate>

<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImagePath", "~/products/Accessories/Images\\{0}") %>' Height="400px" Width="500px" />


<asp:Label ID="Label4" runat="server" Text='<%# Eval("ProductID") %>' Visible="false">

<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' Style=" font-size:40px; color:rgb(17,52,72)">


<asp:Label ID="BrandLabel" runat="server" Text="By brand" Style=" color:rgb(43,131,182)">




<asp:Label ID="Label1" runat="server" Text="Availability:" Style="font-size:25px">  <asp:Label

ID="Label2" runat="server" Text="In Stock" Style=" font-size:20px">



<asp:Label ID="Label5" runat="server" Text="₹" Style=" font-size:20px; color:Green"><asp:Label ID="PriceLabel" runat="server" Style=" font-size:20px; color:Green" Text='<%# Eval("Price", "{0:##0.00}" ) %>'>




< asp:Button ID =btnAddrunat =serverOnClick =Button1_ClickText =Add to CartCssClass =bttn/>

<asp:Button ID="btnAdd" runat="server" OnClick="Button1_Click" Text="Add to Cart" CssClass="bttn" />

< asp:ImageButton ID =ImageButton1runat =serverImageUrl =〜/ products / TransparentHeart.pngHeight =50pxWidth =50px />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/products/TransparentHeart.png" Height="50px" Width="50px"/>






< asp:Label ID = DescriptionLabelrunat =serverText ='<%#Eval(Description)%>'>


< asp:Label ID =ImageUrlLabelrunat = serverText ='<%#Eval(ImagePath,〜/ products / Accessories / Images \\ {0})%>'Visible =False>






< asp:ScriptManager ID =ScriptManager1runat =server>



< asp:评级ID =Rating1runat =serverAutoPostBack =trueStarCssClass =blankstar

WaitingStarCssClass =waitingstarFilledStarCssClass =shiningstar

EmptyStarCssClass =blankstarOnChanged =Rating1_Changed>






< asp:Label ID =Labelraterunat = serverText =Label>,

< asp:Label ID =Labelrtrunat =serverText =Label>

< ; asp:Label ID =Label3runat =serverText =Visible =false>







 < asp:HyperLink ID =HyperLink1runat =serverNavigateUrl =〜/ products / Accessories / Products.aspx>返回产品页面

< br $>




ProductDetails.aspx.cs





SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionStringRating] .ConnectionString);

protected void Page_Load(object send呃,EventArgs e)

{



Labelrate.Text =0用户评价过此产品;

Labelrt.Text =此产品的平均评分为0;



if(!IsPostBack)

{

foreach(DataList1.Items中的DataListItem项)

{

Label3.Text =((Label)(item.FindControl(Label4)))。Text ;

}

BindRatings();

}

}

protected void Rating1_Changed(对象发送者,AjaxControlToolkit.RatingEventArgs e)

{

con.Open();

SqlCommand cmd = new SqlCommand(INSERT INTO UserRating(Rating,ProductID)VALUES(@ Rating,@ Productid),con);

cmd.Parameters.AddWithValue(@ Rating,SqlDbType.Int).Value = Rating1.CurrentRating;

cmd.Parameters.AddWithValue(@ Productid,SqlDbType.I nt).Value = Label3.Text;

cmd.ExecuteNonQuery();

con.Close();

BindRatings(); < br $>
}

public void BindRatings()

{

int总计= 0;

con.Open();

SqlCommand cmd = new SqlCommand(SELECT Rating FROM UserRating where ProductID = @ productId,con);

cmd.Parameters.AddWithValue( @productId,Label3.Text);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

da.Fill(dt);

if(dt.Rows.Count> 0)

{

for(int i = 0; i< dt.Rows.Count; i ++)

{

Total + = Convert.ToInt32(dt.Rows [i] [0] .ToString());

}

int Average = Total /(dt .Rows.Count);

Rating1.CurrentRating =平均值;

Labelrate.Text = dt.Rows.Count ++用户评价过此产品;

Labelrt.Text =此商品的平均评分为++ Convert.ToString(平均);

}

}

protected void Button1_Click(object sender,EventArgs e)

{

double Price = double.Parse(((Label)DataList1.Controls [0] .FindControl(PriceLabel))。Text);

string ProductName =((Label)DataList1.Controls [0] .FindControl(NameLabel))。Text;

string ProductImageUrl =((Label)DataList1.Controls [0] .FindControl(ImageUrlLabel))。Text;

int ProductID = int.Parse(Request.QueryString [ProductID]);

if(Profile.SCart == null)

{

Profile.SCart = new ShoppingCartExample.Cart();

}

Profile.SCart.Insert(ProductID,Price,1,ProductName,ProductImageUrl);

Server.Transfer(Products.aspx);

}




<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'>

<asp:Label ID="ImageUrlLabel" runat="server" Text='<%# Eval("ImagePath", "~/products/Accessories/Images\\{0}") %>' Visible="False">



<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:Rating ID="Rating1" runat="server" AutoPostBack="true" StarCssClass="blankstar"
WaitingStarCssClass="waitingstar" FilledStarCssClass="shiningstar"
EmptyStarCssClass="blankstar" OnChanged="Rating1_Changed">



<asp:Label ID="Labelrate" runat="server" Text="Label">,
<asp:Label ID="Labelrt" runat="server" Text="Label">
<asp:Label ID="Label3" runat="server" Text="" Visible="false">




 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/products/Accessories/Products.aspx">Return to Products Page



ProductDetails.aspx.cs


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringRating"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

Labelrate.Text = "0 Users have rated this Product";
Labelrt.Text = "Average rating for this Product is 0";

if (!IsPostBack)
{
foreach (DataListItem item in DataList1.Items)
{
Label3.Text = ((Label)(item.FindControl("Label4"))).Text;
}
BindRatings();
}
}
protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO UserRating(Rating,ProductID) VALUES (@Rating,@Productid)", con);
cmd.Parameters.AddWithValue("@Rating", SqlDbType.Int).Value = Rating1.CurrentRating;
cmd.Parameters.AddWithValue("@Productid", SqlDbType.Int).Value = Label3.Text;
cmd.ExecuteNonQuery();
con.Close();
BindRatings();
}
public void BindRatings()
{
int Total = 0;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Rating FROM UserRating where ProductID=@productId", con);
cmd.Parameters.AddWithValue("@productId", Label3.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Total += Convert.ToInt32(dt.Rows[i][0].ToString());
}
int Average = Total / (dt.Rows.Count);
Rating1.CurrentRating = Average;
Labelrate.Text = dt.Rows.Count + " " + "Users have rated this Product";
Labelrt.Text = "Average rating for this Product is" + " " + Convert.ToString(Average);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
int ProductID = int.Parse(Request.QueryString["ProductID"]);
if (Profile.SCart == null)
{
Profile.SCart = new ShoppingCartExample.Cart();
}
Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
Server.Transfer("Products.aspx");
}

推荐答案

ConnectionStrings:prdcts_accsrsConnectionString1%>

SelectCommand =SELECT [ProductID],[Name],[Description],[Price],[ImagePath] FROM [Products] WHERE([ProductID] = @ProductID)>

< SelectParameters>

< asp:QueryStringParameter Name =ProductIDQueryStringField =ProductIDType =Decimal/>

< / SelectParameters>





ConnectionStrings:prdcts_accsrsConnectionString1 %>"
SelectCommand="SELECT [ProductID], [Name], [Description], [Price], [ImagePath] FROM [Products] WHERE ([ProductID] = @ProductID)">
<SelectParameters>
<asp:QueryStringParameter Name="ProductID" QueryStringField="ProductID" Type="Decimal" />
</SelectParameters>




< asp:DataList ID =DataList1runat =服务器 DataSourceID =SqlDataSource1>

< itemtemplate>



< asp :Image ID =Image1runat =serverImageUrl ='<%#Eval(ImagePath,〜/ products / Accessories / Images \\ {0})%>'Height =400px 宽度=500px/>




< asp:Label ID =Label4 runat =serverText ='<%#Eval(ProductID)%>'Visible =false>



< asp:Label ID =NameLabelrunat =serverText ='<%#Eval(Name)%>'Style =font-size:40px;颜色:rgb(17,52,72)>




< asp:标签ID =BrandLabelrunat =serverText =按品牌Style =颜色:rgb(43,431,182)>






< asp:Label ID =Label1 runat =serverText =Availability:Style =font-size:25px> < asp:Label



ID =Label2 runat =serverText =In StockStyle =font-size:20px>





< asp:标签ID =Label5runat =serverText =₹Style =font-size:20px;颜色:绿色>< asp:标签ID =PriceLabelrunat =serverStyle =font-size:20px;颜色:绿色Text ='<%#Eval(Price,{0:## 0.00})%>'>




<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<itemtemplate>

<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImagePath", "~/products/Accessories/Images\\{0}") %>' Height="400px" Width="500px" />


<asp:Label ID="Label4" runat="server" Text='<%# Eval("ProductID") %>' Visible="false">

<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' Style=" font-size:40px; color:rgb(17,52,72)">


<asp:Label ID="BrandLabel" runat="server" Text="By brand" Style=" color:rgb(43,131,182)">




<asp:Label ID="Label1" runat="server" Text="Availability:" Style="font-size:25px">  <asp:Label

ID="Label2" runat="server" Text="In Stock" Style=" font-size:20px">



<asp:Label ID="Label5" runat="server" Text="₹" Style=" font-size:20px; color:Green"><asp:Label ID="PriceLabel" runat="server" Style=" font-size:20px; color:Green" Text='<%# Eval("Price", "{0:##0.00}" ) %>'>




< asp:Button ID =btnAddrunat =serverOnClick =Button1_ClickText =Add to CartCssClass =bttn/>

<asp:Button ID="btnAdd" runat="server" OnClick="Button1_Click" Text="Add to Cart" CssClass="bttn" />

< asp:ImageButton ID =ImageButton1runat =serverImageUrl =〜/ products / TransparentHeart.pngHeight =50pxWidth =50px />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/products/TransparentHeart.png" Height="50px" Width="50px"/>






< asp:Label ID = DescriptionLabelrunat =serverText ='<%#Eval(Description)%>'>


< asp:Label ID =ImageUrlLabelrunat = serverText ='<%#Eval(ImagePath,〜/ products / Accessories / Images \\ {0})%>'Visible =False>






< asp:ScriptManager ID =ScriptManager1runat =server>



< asp:评级ID =Rating1runat =serverAutoPostBack =trueStarCssClass =blankstar

WaitingStarCssClass =waitingstarFilledStarCssClass =shiningstar

EmptyStarCssClass =blankstarOnChanged =Rating1_Changed>






< asp:Label ID =Labelraterunat = serverText =Label>,

< asp:Label ID =Labelrtrunat =serverText =Label>

< ; asp:Label ID =Label3runat =serverText =Visible =false>







 < asp:HyperLink ID =HyperLink1runat =serverNavigateUrl =〜/ products / Accessories / Products.aspx>返回产品页面

< br $>




ProductDetails.aspx.cs





SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionStringRating] .ConnectionString);

protected void Page_Load(object send呃,EventArgs e)

{



Labelrate.Text =0用户评价过此产品;

Labelrt.Text =此产品的平均评分为0;



if(!IsPostBack)

{

foreach(DataList1.Items中的DataListItem项)

{

Label3.Text =((Label)(item.FindControl(Label4)))。Text ;

}

BindRatings();

}

}

protected void Rating1_Changed(对象发送者,AjaxControlToolkit.RatingEventArgs e)

{

con.Open();

SqlCommand cmd = new SqlCommand(INSERT INTO UserRating(Rating,ProductID)VALUES(@ Rating,@ Productid),con);

cmd.Parameters.AddWithValue(@ Rating,SqlDbType.Int).Value = Rating1.CurrentRating;

cmd.Parameters.AddWithValue(@ Productid,SqlDbType.I nt).Value = Label3.Text;

cmd.ExecuteNonQuery();

con.Close();

BindRatings(); < br $>
}

public void BindRatings()

{

int总计= 0;

con.Open();

SqlCommand cmd = new SqlCommand(SELECT Rating FROM UserRating where ProductID = @ productId,con);

cmd.Parameters.AddWithValue( @productId,Label3.Text);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

da.Fill(dt);

if(dt.Rows.Count> 0)

{

for(int i = 0; i< dt.Rows.Count; i ++)

{

Total + = Convert.ToInt32(dt.Rows [i] [0] .ToString());

}

int Average = Total /(dt .Rows.Count);

Rating1.CurrentRating =平均值;

Labelrate.Text = dt.Rows.Count ++用户评价过此产品;

Labelrt.Text =此商品的平均评分为++ Convert.ToString(平均);

}

}

protected void Button1_Click(object sender,EventArgs e)

{

double Price = double.Parse(((Label)DataList1.Controls [0] .FindControl(PriceLabel))。Text);

string ProductName =((Label)DataList1.Controls [0] .FindControl(NameLabel))。Text;

string ProductImageUrl =((Label)DataList1.Controls [0] .FindControl(ImageUrlLabel))。Text;

int ProductID = int.Parse(Request.QueryString [ProductID]);

if(Profile.SCart == null)

{

Profile.SCart = new ShoppingCartExample.Cart();

}

Profile.SCart.Insert(ProductID,Price,1,ProductName,ProductImageUrl);

Server.Transfer(Products.aspx);

}




<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'>

<asp:Label ID="ImageUrlLabel" runat="server" Text='<%# Eval("ImagePath", "~/products/Accessories/Images\\{0}") %>' Visible="False">



<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:Rating ID="Rating1" runat="server" AutoPostBack="true" StarCssClass="blankstar"
WaitingStarCssClass="waitingstar" FilledStarCssClass="shiningstar"
EmptyStarCssClass="blankstar" OnChanged="Rating1_Changed">



<asp:Label ID="Labelrate" runat="server" Text="Label">,
<asp:Label ID="Labelrt" runat="server" Text="Label">
<asp:Label ID="Label3" runat="server" Text="" Visible="false">




 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/products/Accessories/Products.aspx">Return to Products Page



ProductDetails.aspx.cs


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringRating"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

Labelrate.Text = "0 Users have rated this Product";
Labelrt.Text = "Average rating for this Product is 0";

if (!IsPostBack)
{
foreach (DataListItem item in DataList1.Items)
{
Label3.Text = ((Label)(item.FindControl("Label4"))).Text;
}
BindRatings();
}
}
protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO UserRating(Rating,ProductID) VALUES (@Rating,@Productid)", con);
cmd.Parameters.AddWithValue("@Rating", SqlDbType.Int).Value = Rating1.CurrentRating;
cmd.Parameters.AddWithValue("@Productid", SqlDbType.Int).Value = Label3.Text;
cmd.ExecuteNonQuery();
con.Close();
BindRatings();
}
public void BindRatings()
{
int Total = 0;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Rating FROM UserRating where ProductID=@productId", con);
cmd.Parameters.AddWithValue("@productId", Label3.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Total += Convert.ToInt32(dt.Rows[i][0].ToString());
}
int Average = Total / (dt.Rows.Count);
Rating1.CurrentRating = Average;
Labelrate.Text = dt.Rows.Count + " " + "Users have rated this Product";
Labelrt.Text = "Average rating for this Product is" + " " + Convert.ToString(Average);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
int ProductID = int.Parse(Request.QueryString["ProductID"]);
if (Profile.SCart == null)
{
Profile.SCart = new ShoppingCartExample.Cart();
}
Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
Server.Transfer("Products.aspx");
}


查看星级评分

< br $> b $ b

如何使用asp.net c#绑定datalist中的ajax评级[ ^ ]



http://www.dotnetspark.com/links/33221-star-rating-datalist-problem.aspx [ ^ ]



http://www.solveerrors.com/forums/rating-control-in-datalist- 196835.asp [ ^ ]



http://stackoverflow.com/questions/10773567/using-ajaxcontroltoolkit-rating-control-in-a-datalist-doesnt-work [ ^ ]



http://forums.asp。 net / t / 1609298.aspx?Star + rating + in + Datalist + problem [ ^ ]
View for Star Rating


how to bind ajax rating in datalist using asp.net c#[^]

http://www.dotnetspark.com/links/33221-star-rating-datalist-problem.aspx[^]

http://www.solveerrors.com/forums/rating-control-in-datalist-196835.asp[^]

http://stackoverflow.com/questions/10773567/using-ajaxcontroltoolkit-rating-control-in-a-datalist-doesnt-work[^]

http://forums.asp.net/t/1609298.aspx?Star+rating+in+Datalist+problem[^]


这篇关于如何在Datalist中显示星级。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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