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