Click here to Skip to main content
15,898,773 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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="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="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="btnAdd" runat="server" OnClick="Button1_Click" Text="Add to Cart" CssClass="bttn" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/products/TransparentHeart.png" Height="50px" Width="50px"/>



<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");
}
Posted
Updated 30-Jun-14 21:54pm
v4
Comments
[no name] 29-Jun-14 9:06am    
You would need to format your code so people can read it. And, define what it is that you mean by "not working".
ZurdoDev 29-Jun-14 20:50pm    
Please debug it and let us know where you are stuck.
Member 10878414 1-Jul-14 3:56am    
Rating ID is not accessible when i put it inside the datalist. It is working fine outside,but inside datalist. It is showing error
What is that error?
Member 10878414 1-Jul-14 3:58am    
and also label rate and labelrt when i put them inside datalist they are also not accessible. Can you please tell me how can i fix it.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900