How to change the css styles dynamically - c#

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>

Related

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

Passing arguments to code behind c#

We have looked and looked all over the internet but we were unable to find a solution.
We have a linkbutton and we want to pass a parameter to the code behind.
This is the code:
<% foreach (Academia.CourseEN curso in MyCourses) { %>
<% string id = curso.Id.ToString(); %>
<div class="col-xs-12 col-sm-5 col-md-5 col-lg-3">
<div class="panel panel-warning">
<div class="panel-heading"><%: curso.Title %></div>
<div class="panel-body"><%: curso.Id %></div>
<div class="panel-body"><%: curso.Description %></div>
<div class="panel-body"><%: curso.Price %></div>
<div class="panel-body"><%: curso.Category %></div>
<div class="panel-body">
<div class="col-md-offset-2 col-md-10">
<asp:LinkButton ID="ENameLinkBtn" runat="server" Text="Button" CommandArgument='<%# Eval("id") %>' OnClick="DeleteCourse_Click" ></asp:LinkButton>
</div>
</div>
</div>
</div>
<%} %>
The problem is that eval always returns an empty value, which is not the case as when we display the id it has a value.
I post the code behind code as well:
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
ErrorMessage.Text = yourValue;
I dont know what else to do or what else to try, anything would behighly appreciated.
Thanks everyone.
You can create a javascript function DeleteCourse()
function DeleteCourse()
{
var param = $("#ENameLinkBtn").val();
$.ajax(
url:"your web method",
data: '"param":"' + param + '"'
....)
}
Then assign it to your button OnClick="DeleteCourse()"

Can't assign data source to the repeater asp.net

I'm working on a project and ran into a problem.
For some reason, I can't assign data source to the repeater. I'm using usercontrol
SlideshowDemo.ascx
<asp:Repeater ID="Slideshow" runat="server">
<HeaderTemplate>
<div style="margin:0 auto; width:960px;">
<div class="slideshowInsideContainer">
<div class="slideShowText">
<div style="padding:10px;" class="SlideShowContent">
<%= firstText %>
</div>
</div>
<img id="SlideShowImage" style="position:absolute; z-index:1; top:0;" src="<%= firstImage %>" />
<img id="SlideShowImageBG" style="position:absolute; z-index:0; top:0;" src="<%= firstImage %>" />
<img id="previousBttn" style="position:absolute; z-index:2; top:200px; cursor:pointer; left:0;" src="/images/SlideShow/leftButton.png" />
<img id="nextBttn" style="position:absolute; z-index:2; top:200px; cursor:pointer; right:0;" src="/images/SlideShow/rightButton.png" />
<div class="slideshowItems">
<ul>
</HeaderTemplate>
<ItemTemplate>
<li class='<%# Container.ItemIndex == 0 ? "selected" : "" %>' id="<%# Container.ItemIndex %>">
<div class="slideshowtext">
<%# Eval("text") %>
</div>
<img style="position:absolute; z-index:1; top:0;" src=' <%# Eval("image") %>' />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</div>
</div>
</div>
</FooterTemplate>
</asp:Repeater>
SlideshowDemo.ascx.cs
public void GetSlideshowData()
{
int ID = Convert.ToInt32(slideShowID);
Node parent = new Node(ID);
ArrayList repeaterData = new ArrayList();
Nodes children = parent.Children;
for (int i = 0; i < children.Count; i++)
{
string image = children[i].GetProperty("image").Value.ToString();
string text = children[i].GetProperty("slideshowText").Value.ToString();
if (i == 0)
{
firstImage = image;
firstText = text;
}
repeaterData.Add(new SlideShowItem(image, text));
}
Slideshow.DataSource = repeaterData; //Error: Can't recognize 'Slideshow'
Slideshow.DataBind();
}
The problem is that the SlideshowDemo.ascx.cs doesn't recognize Slideshow
I get error
The name 'Slideshow' does not exist in the current context
I also get Warning
Source file
'obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs'
specified multiple times
I would appreciate any help

Show product details in page

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.

asp:listview add a <div class="clear"> after every 3 items

How would i go about (preferably not in the code behind) adding a clear in ever third item. This is the code in question...
<asp:ListView ID="lvproducts" runat="server" DataSourceID="entyproducts">
<ItemTemplate>
<div class="s_item grid_3"> <a class="s_thumb" href="product.html"><img
src="images/product_180x180.jpg" title="Samsung SyncMaster 941BW" alt="Samsung
SyncMaster 941BW" /></a>
<h3><%# Eval("title") %> </h3>
<p class="s_model"><%# Eval("model") %> </p>
$ <%# Eval("salesprice") %>
<p class="s_rating s_rating_5"><span style="width: 60%;" class="s_percent">
</span></p>
<a class="s_button_add_to_cart" href="product.html"><span class="s_icon_16"><span
class="s_icon"></span>Add to Cart</span></a>
</div>
</ItemTemplate>
</asp:ListView>
In the original markup the designer has that <div class="clear"></div> after every third item in the list. How do I do that (preferably not in the code behind)? Is their a template I can put that in (I think there should be)?
Try this,
<asp:ListView ID="lvproducts" runat="server" DataSourceID="entyproducts">
<ItemTemplate>
<div class="s_item grid_3">
<a class="s_thumb" href="product.html">
<img src="images/product_180x180.jpg" title="Samsung SyncMaster 941BW" alt="Samsung
SyncMaster 941BW" /></a>
<h3>
<a href="products.aspx">
<%# Eval("title") %>
</a>
</h3>
<p class="s_model">
<%# Eval("model") %>
</p>
$
<%# Eval("salesprice") %>
<p class="s_rating s_rating_5">
<span style="width: 60%;" class="s_percent"></span>
</p>
<a class="s_button_add_to_cart" href="product.html"><span class="s_icon_16"><span
class="s_icon"></span>Add to Cart</span></a>
</div>
<%# (Container.DataItemIndex != 0 && (Container.DataItemIndex+1) % 3 == 0) ? #"<div class='clear'></div>" : string.Empty %>
</ItemTemplate>
</asp:ListView>
jQuery is your friend :)
Put this at the top of you your page.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
$(function(){
$("a.s_button_add_to_cart").append("<div class="clear"></div>");}
</script>
More on how to select exactly what you are looking for: http://api.jquery.com/category/selectors/
If you aren't using it today you are missing out on a truly powerful library :) Everyone else is using it.. so why aren't you? :P PEEEER PRESSURE! :D
http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/

Categories