Show product details in page - c#

I need to show products details in a page.
My aspx is like below...
<div class="col-xs-12 col-sm-4 no-margin product-item-holder hover"> <!-- this div will be repeated for each product -->
<div class="product-item">
<div class="image">
<img runat="server" id="img" alt="" src="" />
</div>
<div class="body">
<div class="label-discount clear"></div>
<div class="title">
<a runat="server" id="name" href="single-product.html"></a>
</div>
</div>
<div class="prices">
<div class="price-prev"></div>
<div runat="server" id="price" class="price-current pull-right"></div>
</div>
<div class="hover-area">
<div class="add-cart-button">
Enquiry
</div>
</div>
</div>
and my codebehind is like below...
dbConnection cn = new dbConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getLoopData();
}
}
public void getLoopData()
{
cn.con.Open();
cn.cmd.Connection = cn.con;
cn.cmd.CommandText = "select * FROM products";
MySqlDataReader reader = cn.cmd.ExecuteReader();
while (reader.Read())
{
//int id = reader.GetInt32(0);
name.InnerText = reader["InventionName"].ToString();
price.InnerText = reader["Price"].ToString();
img.Src = reader["Picture"].ToString();
}
reader.Close();
cn.con.Close();
}
But it is showing only last product details.
How should I repeat the div for each product and how to show every products in page ??

You can use Asp Repeater to do that.
Check this link out : http://www.w3schools.com/aspnet/aspnet_repeater.asp

You need a datagrid control, repeater or similar to show tabular data.
Right now you only have one control per attribute to show all records, that's why last one wins.

Your view/page could be like this:
<asp:Repeater ID="ProductRepeater" runat="server" EnableViewState="False">
<ItemTemplate>
<div class="product-item">
<div class="image">
<img id="img" alt="" src='<%# Eval("Picture") %>' />
///...
</ItemTemplate>
</asp:Repeater>
In your code you could do this:
cn.con.Open();
cn.cmd.Connection = cn.con;
cn.cmd.CommandText = "SELECT InventionName, Price, Picture FROM products";
MySqlDataReader reader = cn.cmd.ExecuteReader();
ProductRepeater.DataSource = reader;
ProductRepeater.DataBind();

You are iterating a list, and assigning data to name, price and img.Src, but you overwrite the values on each iteration, therefore you only have the values from the last element.
You should bind you data source (reader) to an <asp:Repeater>'s DataSource property. In your markup you specify a header (HeaderTemplate), a footer (FooterTemplate) and content (ItemTemplate). You can omit the header and footer template if you don't need them. You can also use an AlternatingItemTemplate if you need to do different things with odd/even rows (e.g. alternating row color in a table).
You can access properties of the databound datatype using the <%# Eval("SomeProperty") %> which is a databinding expression.
HTML markup would be something like this:
<asp:Repeater id="Products" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="col-xs-12 col-sm-4 no-margin product-item-holder hover"> <!-- this div will be repeated for each product -->
<div class="product-item">
<div class="image">
<img class="img" alt="" src="<%# Eval("Picture") %>" />
</div>
<div class="body">
<div class="label-discount clear"></div>
<div class="title">
<a class="name" href="single-product.html"><%# Eval("InventionName") %></a>
</div>
</div>
<div class="prices">
<div class="price-prev"></div>
<div class="price" class="price-current pull-right"><%# Eval("Price") %></div>
</div>
<div class="hover-area">
<div class="add-cart-button">
Enquiry
</div>
</div>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
and wire up the datasource in codebehind like this:
cn.con.Open();
cn.cmd.Connection = cn.con;
cn.cmd.CommandText = "select * FROM products";
MySqlDataReader reader = cn.cmd.ExecuteReader();
Products.DataSource = reader; // rather than iterating manually, you assign the datasource to the repeater.
Products.DataBind();
reader.Close();
cn.con.Close();
Side note: I changed the attribute from id to class on the name, price and img element. Because when this markup is rendered it would have been with multiple elements with the same id, which it shouldn't since id is supposed to be unique according to specification.

Related

How to display 10 recent news from database using Bootstrap?

I wanted to display 10 recent news from database. I have used LIMIT 10 in sql query to display 10 recent news. I am using bootstrap.
I am getting only 1 news from the database. I know that here I have to use datalist or listview in design. But I don't know how to implement it using Bootstrap.
Default.aspx:
<div class="container">
<h1 class="main-module-title">Recent <span>News</span></h1>
<%-- <asp:datalist runat="server">
<ItemTemplate>--%>
<div class="row-fluid">
<div class="row">
<div class="col-xs-12 col-sm-3 col-md-3">
<a href="#">
<asp:Image runat="server" ID="ImgNews" class="img-responsive img-box img-thumbnail"/>
</a>
</div>
<div class="col-xs-12 col-sm-9 col-md-9">
<h4><asp:Label ID="newsheader" runat="server" Text=""></asp:Label></h4>
<p runat="server" style="text-align:justify" id="newscontent"> </p>
</div>
</div>
<hr/>
</div>
<%-- </ItemTemplate>
</asp:datalist>--%>
</div>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MySqlConnection conn = null;
string newsitem = null;
if (!(Request.QueryString["newstitle"] == null))
{
Page.Header.Title = Request.QueryString["newstitle"] + " - DubaiExporters ";
}
else
{
Page.Header.Title = "DubaiExporters - Dubai Business News - UAE Exports";
}
try
{
string connStr = ConfigurationManager.ConnectionStrings["mysqldbeConnectionString"].ToString();
string newssql = null;
newssql = "SELECT * FROM news WHERE status = b'1' AND linkstatus = b'1' ORDER BY datepublished DESC LIMIT 10";
conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand(newssql, conn);
conn.Open();
MySqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(r.Read())//while(r.Read())
{
newsheader.Text = HttpUtility.HtmlDecode(r["newstitle"].ToString().Trim()).ToString();
newscontent.InnerHtml = HttpUtility.HtmlDecode(r["newsbrief"].ToString().Trim()).ToString();
ImgNews.ImageUrl = "~/images/newspictures/" + r["image"].ToString();
}
}
catch (MySqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
try the below template and bind the DataList to DataSource in the Page Load event inside !Page.IsPOstBack condition
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<div class="row-fluid">
<div class="row">
<div class="col-xs-12 col-sm-3 col-md-3">
<a href="#">
<asp:Image runat="server" ID="ImgNews" class="img-responsive img-box img-thumbnail" ImageUrl='<%# "~/images/newspictures/" + Eval("image")%>' />
</a>
</div>
<div class="col-xs-12 col-sm-9 col-md-9">
<h4><a href="#">
<asp:Label ID="newsheader" runat="server" Text='<%# Eval("newstitle") %>'></asp:Label></a></h4>
<p runat="server" style="text-align: justify" id="newscontent"><%# Eval("newsbrief") %></p>
</div>
</div>
<hr />
</div>
</ItemTemplate>
</asp:DataList>

How to change the css styles dynamically

I am trying to change the css style of the repeater to a different color on different status. The expected output is to print 6 different statuses like: In progress, complete, withdrawn so on.. Currently it has a single color, so it shows one color only. the challenging part here is to change the color dynamically based on the status. how do I achieve this? Currently it pulls the style based on "status noAction text-center" class below. Should I make any changes in the code behind or is it just a front end css change. Can someone provide me an example please.
.aspx code:
<div class="row">
<asp:Repeater ID="rptStatuses" runat="server">
<ItemTemplate>
<div class="col-md-4 col-sm-4">
<div class="status noAction text-center">
<div class="banner">
<asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
</div>
<div class="label"><%# Eval("Name") %></div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
.cs code relevant part
private void GB()
{
var surveyId = 55;
var stateLabels = _manageDatasets.GetStateLabels(surveyId);
List<Status> statusesList = new List<Status>();
foreach (var sl in stateLabels)
{
if (sl.Key != -1)
statusesList.Add(new Status { ID = sl.Key.ToString(), Name = sl.Value }
);
}
this.rptStatuses.DataSource = statusesList;
this.rptStatuses.DataBind();
}
I think what you want to do is change the class in the your html and just use the embedded code to be able to dynamically change the banner color, etc. based on the status. The embedded code that you want would look something like this -
<div class="col-md-4 col-sm-4">
<% if(status == "noAction") { %>
<div class="status noAction text-center">
<div class="banner">
<asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
</div>
<div class="label"><%# Eval("Name") %></div>
</div>
<% } %>
If you have any column in you table from where repeater is getting datasource then that would be pretty easy to do
something like that
<asp:Repeater ID="rptStatuses" runat="server">
<ItemTemplate>
<div class="col-md-4 col-sm-4">
<div class='<%# Convert.toInt32(eval("ActionColumnName"))==1 ? "status In progress text-center" : Convert.toInt32(eval("ActionColumnName"))==2 ? "status complete text-center" : Convert.toInt32(eval("ActionColumnName"))==3 ? "status withdrawn text-center" : "status noAction text-center" '>
<div class="banner">
<asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
</div>
<div class="label"><%# Eval("Name") %></div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>

How to loop div?

I am trying to loop few div's in aspx. This the code I would like to loop:
<div class="col-md-3 product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="men-thumb-item">
<img src="uploadImage/3.png" class="pro-image-front" />
<img src="uploadImage/3.png" class="pro-image-back" />
</div>
<div class="item-info-product ">
<h4>Tie Clip</h4>
<div class="info-product-price">
<span class="item_price">RM100</span>
</div>
Add to cart
</div>
</div>
</div>
The page looks like below:
the cart need to loop. I have tried using c# as below:
StringBuilder cart = new StringBuilder();
for (int x = 0; x < 3; x++) {
cart.Append(" <div class=\"col - md - 3 product - men\">");
cart.Append("<div class=\"men - pro - item simpleCart_shelfItem\">");
cart.Append("<div class=\"men - thumb - item\">");
cart.Append("<img src = uploadImage/1.jpg class='pro - image - front />");
cart.Append("<img src = uploadImage/1.jpg class=pro - image - back />");
cart.Append("</div>");
cart.Append(" <div class=\"item - info - product \">");
cart.Append("<h4><a href = \"single.html\" > Tie Clip</a></h4>");
cart.Append("<div class=\"info - product - price\">");
cart.Append("<span class=\"item_price\">RM100</span>");
cart.Append("</div>");
cart.Append("Add to cart");
cart.Append("</div>");
cart.Append("</div>");
cart.Append("</div>");
}
Literal1.Text = cart.ToString();
This don't seem work out.. Any idea how to get this cart loop?
You should use Embedded Code Blocks in the .aspx itself instead of .cs.
You would normally use a asp.net Control for that, like a Repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="col-md-3 product-men">
<div class="men-pro-item simpleCart_shelfItem">
<div class="men-thumb-item">
<img src="uploadImage/<%# Eval("product_image_front") %>" class="pro-image-front" />
<img src="uploadImage/<%# Eval("product_image_back") %>" class="pro-image-back" />
</div>
<div class="item-info-product ">
<h4><%# Eval("product_name") %></h4>
<div class="info-product-price">
<span class="item_price"><%# Eval("product_price") %></span>
</div>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("product_id") %>' runat="server" CssClass="item_add single-item hvr-outline-out button2">Add to cart</asp:LinkButton>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Then in code behind you would assign a DataSource to the Repeater.
Repeater1.DataSource = source;
Repeater1.DataBind();
Code in aspx Page
<asp:Literal ID="ltrMessage" runat="server"></asp:Literal>
Code Snippet in cs page.
for(int i=0;i<limit;i++)
{
ltrMessage.Text+=#"<div class='col-md-3 product-men'>
<div class='men-pro-item simpleCart_shelfItem'>
<div class='men-thumb-item'>
<img src='uploadImage/3.png' class='pro-image-front' />
<img src='uploadImage/3.png' class='pro-image-back' />
</div>
<div class='item-info-product '>
<h4><a href='single.html'>Tie Clip</a></h4>
<div class=''info-product-price'>
<span class='item_price'>RM100</span>
</div>
<a href='#' class='item_add single-item hvr-outline-out button2'>Add to cart</a>
</div>
</div>
</div>";
}

How can I use the Query String to return data from sql

I have a website for movies. If you click on a movie, it will take you to another page where it shows you all the details in that movie. Just think of it as an image for the movie and if you click on it the browser takes you to another page using the query string. Now, I have done all that but the problem is I can't show the requested information from the database. I need help to see what am doing wrong. Here is the html/asp.net code for the page with the different movie images
<asp:Repeater ID="repMovies" runat="server" OnItemDataBound="repMovies_ItemDataBound" >
<ItemTemplate>
<a href="MovieDetail.aspx?MovieTitle=<%#Eval("ID") %>">
<main class="col-md-3 movie" style="background:#eee;margin-top:10px;padding:10px;border-radius:10px;border:solid 2px #333;height:280px;">
<div class="col-md-4"></div>
<div class="col-md-4">
<img src="Images/<%#Eval ("Poster") %>" class=" img-responsive img-rounded" style="height:100px;width:100%"/>
</div>
<div class="col-md-4"></div>
<div class="col-md-12">
<div class="row">
<h6><strong>Title:</strong><%#Eval("Title") %></h6>
</div>
<div class="row">
<h6><strong>Year :</strong><%#Eval("mYear") %></h6>
</div>
<div class="row">
<h6 style="font-size:9px;"><strong style="font-size:11px;">Starring:</strong><%#Eval("Starring") %></h6>
</div>
<div class="row">
<div class="menu">
<h6 style="font-size:11px;"><strong>Summery:</strong></h6>
<p style="font-size:9px;"><%#Eval("Summary") %>
</div>
</div>
</div>
</main>
</a>
</ItemTemplate>
</asp:Repeater>
and here the code behind the page of the Movie detail
if (!IsPostBack)
{
string movieId = Request.QueryString["ID"];
repMovie.DataSource = db.Movies.Where(m => m.ID == movieId).ToList();
repMovie.DataBind();
}
You are using the wrong query string parameter name. As shown in the code snippet below, name of your query string parameter is MovieTitle. To get the movie id, you are trying to fetch it considering its name to be ID in your code behind string movieId = Request.QueryString["ID"];
<a href="MovieDetail.aspx?MovieTitle=<%#Eval("ID") %>">
Fetch the query string in your code behind as shown below and you should be good:
string movieId = Request.QueryString["MovieTitle"];

Multiple bootstrap sliders using asp:repeater control

I have to use multiple bootstrap carousel in single page, using the asp repeater control but it doesn't work, it just show one slider on the page
Here is the html and asp code
<div class="col-md-12">
<div id="Carousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item">
<div class="row">
<asp:Repeater runat="server" ID="Slider1">
<ItemTemplate>
<div class="col-md-2">
<div class="row">
<a target="_blank" href="<%# Eval("URL") %>">
<img class="caption" src="../Includes/gaceta/<%# Eval("Titulo") %>/files/res/pages/page_0000.jpg" alt="<%# Eval("Titulo") %>" width="188" height="222" /></a>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<!--.row-->
</div>
</div>
<!--.carousel-inner-->
<a data-slide="prev" href="#Carousel" class="left carousel-control">‹</a>
<a data-slide="next" href="#Carousel" class="right carousel-control">›</a>
</div>
<!--.Carousel-->
</div>
<hr />
<div class="col-md-12">
<div id="Carousel2" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item">
<div class="row">
<asp:Repeater runat="server" ID="Slider2">
<ItemTemplate>
<div class="col-md-2">
<div class="row">
<a target="_blank" href="<%# Eval("URL") %>">
<img class="caption" src="../Includes/gaceta/<%# Eval("Titulo") %>/files/res/pages/page_0000.jpg" alt="<%# Eval("Titulo") %>" width="188" height="222" /></a>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<!--.row-->
</div>
</div>
<!--.carousel-inner-->
<a data-slide="prev" href="#Folletos" class="left carousel-control">‹</a>
<a data-slide="next" href="#Folletos" class="right carousel-control">›</a>
</div>
<!--.Carousel-->
the javascript
$(document).ready(function () {
$('#Carousel').carousel('pause');
$('#Carousel2').carousel('pause');
$("div.item:first").addClass("active");
});
and the methods to bind the repeater
protected void Page_Load(object sender, EventArgs e)
{
BindSlider1(5);
BindSlider2(1);
}
protected void BindSlider1(int Ubicacion)
{
using (SqlConnection con = Conexion.Conecta())
{
using (SqlCommand cmd = new SqlCommand("SelGacetaUbicacion", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#UBICACION", SqlDbType.Int).Value = Ubicacion;
con.Open();
Slider1.DataSource = cmd.ExecuteReader();
Slider1.DataBind();
}
}
}
protected void BindSlider2(int Ubicacion)
{
using (SqlConnection con = Conexion.Conecta())
{
using (SqlCommand cmd = new SqlCommand("SelGacetaUbicacion", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#UBICACION", SqlDbType.Int).Value = Ubicacion;
con.Open();
Slider2.DataSource = cmd.ExecuteReader();
Slider2.DataBind();
}
}
}
I tried your code, and found this issues:
1- Review how to build the carousel markup depending on which version of bootstrap you are using. I tried with Bootstrap v3.1.0 and had to change parts of your markup. Refer to documentation or this sample.
For instance, <div class="item"> should be placed inside your repeater, and should have a <div class="container"> immediatly inside of it.
2- You are not initializing correctly both carousels from jquery, do something like this to correct it:
$("#Carousel div.item:first").addClass("active");
$("#Carousel2 div.item:first").addClass("active");

Categories