Show picture in 30 days ASP.NET - c#

I am trying to make my first public .net website but I have a little problem.
It's a webshop, where i want to show at new product picture, if the product is not older than the current date with more than 30 days?
Markup:
<asp:Repeater ID="rptProducts" runat="server">
<ItemTemplate>
<table cellpadding="5" cellspacing="5" class="tblSales">
<tr>
<td>
<h3>
<asp:Label ID="Label1" Text='<%#Eval("ProductName")%>' runat="server" /></h3>
<h2>
<a href='<%# "Products.aspx?ID=" + Eval("ProductID") %>'>Read More</a></h2>
</td>
<td >
<h4>
<asp:Label ID="Label2" Text='<%#Eval("ProductPrice")%>' runat="server" />,-</h4>
<div>
<img alt="New product" src="images/NewProduct.png" /><!--This picture should show in 30 days-->
</div>
</td>
</tr>
<hr />
</table>
</ItemTemplate>
</asp:Repeater>
Code-behind:
public partial class sale : System.Web.UI.Page
{
ProductsTableAdapter p = new ProductsTableAdapter();
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
protected void GetData()
{
rptProducts = p.GetProductsByCategoryID(3);
rptProducts();
}
}

EDITED: As a user noted this may be a misinterpretation so...
IF you want to filter items that were added and not display them at all use the following LINQ expression on the GetData method:
rptProducts = p.GetProductsByCategoryID(3).Where(p=>p.ProductAddedDate > DateTime.Now.AddDays(-30));
you need to have a property (in the above example I added a DateTime ProductAddedDate to the product class
IF you want to not show the image based on the date BUT show everything else you could:
In the ItemDataBound event of the repeater you can set asp control (replace static html with asp:Image which allows you to control display using the Visible property i.e.
<asp:Repeater ID="rptProducts" runat="server" OnItemDataBound="rptProducts_Databound">
<ItemTemplate>
<table cellpadding="5" cellspacing="5" class="tblSales">
<tr>
<td>
<h3>
<asp:Label ID="Label1" Text='<%#Eval("ProductName")%>' runat="server" /></h3>
<h2>
<a href='<%# "Products.aspx?ID=" + Eval("ProductID") %>'>Read More</a></h2>
</td>
<td >
<h4>
<asp:Label ID="Label2" Text='<%#Eval("ProductPrice")%>' runat="server" />,-</h4>
<div>
<asp:Image ID="ProdImg" runat="server" AlternativeText="New product" ImageUrl='<%# "~/images/" + Eval("ImageFileName")' Visible="True"/><!--This picture should show in 30 days-->
</div>
</td>
</tr>
<hr />
</table>
</ItemTemplate>
</asp:Repeater>
CS codebehind
protected void rptProducts_Databound(object sender, RepeaterItemEventArgs e)
{
var imgCtrl = (Image)e.Item.FindControl("ProdImg");
var dataItem = (Product)e.Item.DataItem;
imCtrl.Visible = dataItem.ProductAddedDate > DateTime.Now.AddDays(-30);
}

Many thanks, I find out how to make it :)thanks to you, quite simple but i works.
here is the code I made:
<asp:Image ID="Image2" ImageUrl="~/images/new.png" Visible='<%#GetProductSalesPic(Eval("ProductDate"))%>' runat="server" />
C# code:
protected bool GetProductSalesPic(object Date)
{
DateTime actDate = (DateTime)Date;
if (DateTime.Now.AddDays(-30) < actDate)
{
return true;
}
else
{
return false;
}
}
And again, Thanks You, for the fast and good response :D

Related

asp.net c# FindControl inside Reapater inside HTML table return null

I have a repeater inside the HTML table the problem is I'm trying to get label value on item date bound event but it always returns null I tried to use the same code without the HTML table its working fine
so is there a way to get value while I'm using an HTML table
<table class="table table-responsive">
<asp:Repeater ID="DtImages" OnItemDataBound="DtImages_ItemDataBound" runat="server">
<HeaderTemplate>
<thead>
<tr>
<th>Image</th>
<th>update</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td class="cart_product_img">
<img src='<%#"../../../Images/Shoping/"+ Eval("Image") %>' />
</td>
<td class="price">
<asp:ImageButton ID="btninImage" OnClick="PinImage_Click" CommandName="PinImage" ImageUrl="~/img/web/pinn.png" runat="server" />
</td>
<td class="qty">
<div class="qty-btn d-flex">
<div class="quantity">
<asp:ImageButton ID="UpdateImg" OnClick="UpdateImg_Click" CommandName="UpdateImage" ImageUrl="~/img/web/upload.png" runat="server" />
<asp:Label ID="lblProId" runat="server" Text='<%# Eval("ProID") %>' ></asp:Label>
</div>
</div>
</td>
</tr>
</tbody>
</ItemTemplate>
</asp:Repeater>
</table>
code behind
protected void DtImages_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
Label Pinned =(Label) e.Item.FindControl("lblProId") ;// its return null
// some codes
}
When the repeater data bind triggers, headers, alternating rows, and a data row all are feed to that routine.
So, you have to do this:
protected void DtImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) |
(e.Item.ItemType == ListItemType.AlternatingItem))
{
Label Pinned = (Label)e.Item.FindControl("lblProId");
}
}

hide column in an asp.net listview?

I have below list view , how can i hide column by code behind ?
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated">
<LayoutTemplate>
<table class="table table-striped table-bordered small">
<tr class="table-secondary">
<th id="BlockHeader" runat="server" style="white-space: normal;">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" />
</th>
</tr>
<asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="BlockStatus" runat="server">
<%# Eval("BlockStatus")%>
</td>
</tr>
</ItemTemplate>
After binding data with list view i tried below code behind but with this header text only hide but column still can still visible
if (groupOrBlockValue == 'W')
{
AuditLogListView.FindControl("BlockHeader").Visible = false;
AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;
//AuditLogListView.FindControl("BlockStatus").Visible = false;
}
Missing part of the list view?
With this:
<asp:ListView ID="LstMarks" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr style="">
<td><asp:Textbox ID="Course" runat="server" Text='<%# Eval("Course") %>' /></td>
<td><asp:Textbox ID="Mark" runat="server" Text='<%# Eval("Mark") %>' Width="30px"/></td>
<td>
<asp:CheckBox ID="DoneLabs" runat="server" Checked = '<%# Eval("DoneLabs") %>' Width="30px"/>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" class="table">
<tr runat="server" style="">
<th runat="server" >Course</th>
<th runat="server">Mark</th>
<th id= "LabW" runat="server" >Completed Lab Work</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<br />
<br />
<asp:Button ID="cmdHide" runat="server" Text="Hide Lab check box" OnClick="cmdHide_Click" />
So note in the layout, we added a "id" = LabW - that lets you hide the header.
so, a simple button that would toggle (hide/show) the lvColum, then this works:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from StudentCourses",
new SqlConnection(Properties.Settings.Default.TEST4)))
{
cmdSQL.Connection.Open();
LstMarks.DataSource = cmdSQL.ExecuteReader();
LstMarks.DataBind();
}
}
}
protected void cmdHide_Click(object sender, EventArgs e)
{
Control ctrHeader = LstMarks.FindControl("LabW");
ctrHeader.Visible = !ctrHeader.Visible;
foreach (ListViewItem lvRow in LstMarks.Items)
{
CheckBox ckBox = (CheckBox)lvRow.FindControl("DoneLabs");
ckBox.Visible = !ckBox.Visible;
}
}
So, we get this:
And clicking on the button, we get this:
And both the values changed - they persist - even when you click again (to toggle and show the hidden columns).
Edit: =====================================================
So, say this markup:
<asp:ListView ID="AuditLogListView" runat="server"
OnItemCreated="AuditLogListView_ItemCreated">
<LayoutTemplate>
<table class="table table-striped table-bordered small">
<tr class="table-secondary">
<th id="BlockHeader" runat="server" style="white-space: normal;">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="Hotel Name" />
</th>
</tr>
<asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="BlockStatus" runat="server">
<%# Eval("BlockStatus")%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
And code behind button to hide is this:
protected void Button1_Click(object sender, EventArgs e)
{
Control ctrHeader = AuditLogListView.FindControl("BlockHeaderLiteral");
ctrHeader.Visible = !ctrHeader.Visible;
foreach (ListViewItem lvRow in AuditLogListView.Items)
{
Control BlockStat = (Control)lvRow.FindControl("BlockStatus");
BlockStat.Visible = !BlockStat.Visible;
}
}
At which event have you written this code?
Required this code in the OnDataBound event. And in your code this event missing.
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated" OnDataBound="AuditLogListView_DataBound">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" /><asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
C# Code:
protected void AuditLogListView_DataBound(object sender, EventArgs e)
{AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;}

How to get item from listview to disable href

How can i get text of each row from Label1?
My .aspx
<asp:ListView ID="lvRestaurant" runat="server" DataSourceID="ldsOrder" DataKeyName="restaurantID">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<GroupTemplate>
<div class="restaurant">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</GroupTemplate>
<ItemTemplate>
<div><asp:Label ID="Label1" runat="server" Text='<%# Bind("restaurantID") %>' Visible="false"></asp:Label></div>
<div><asp:Image ID="imgRestaurant" runat="server" ImageUrl='<%# Eval("image", "../resImg/{0}") %>' draggable="false" CssClass="restaurant-img"/></div>
<div class="resName"><b><asp:Label ID="lblName" runat="server" Text='<%# Bind("restaurantName") %>'></asp:Label></b></div>
<div class="deliveryFee"><b>MYR<asp:Label ID="lblDeliveryFee" runat="server" Text='<%# Bind("deliverFee") %>'></asp:Label></b> <span style="color:#C3C3C3;">delivery fee</span></div>
</ItemTemplate>
</asp:ListView>
I try to make if cart exist 1 restaurant product, other restaurant will not be able to click.
My .cs
if(Session["BuyItems"]!=null{
string rID = //get from Label1;
if(rID==Session[existID].ToString()){
orderPage.HreF="#";
}
}
Have a look at foreach, maybe this links can help you.
https://www.codeproject.com/Questions/1029041/How-to-loop-through-a-listbox-items-in-asp-net
How to get all labels and its input elements in javascript
https://www.sitepoint.com/community/t/how-to-get-text-from-a-label-in-a-datalist-itemtemplate/6088/18
Pseudocode
if(Session["BuyItems"]!=null
{
string rID = 5; // for example
foreach (var item in YourListWithRestaurants)
{
if(rID != Session[existID].ToString())
{
// Make the restaurants unable to click
}
}
}
You can do this. Add an OnItemCommand with ListView:
OnItemCommand="ListView_ItemCommand" >
Then you can write an eventhandler:
protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Label label1 = (Label)e.Item.FindControl("Label1");
}

Repeater shows wrong column content

I have a repeater to show an order confirmation. It is bound to a table generated with Entity Framework code first. The table contains the right information and the repeater shows all the information right except for the part where I show the totals which I paste below.
I believe that this part also works okay but quantity does not. For example if I add 5 products it will only show one, but if I put a break point and I run the code in debug mode I see that the value inserted in the Quantity table is 5 not 1, so the Quantity value on the table is inserted correctly, but the repeater reports 1.
Here is the code:
<asp:Repeater ID="rptConfirmOrder" runat="server">
<ItemTemplate>
<fieldset class = "OrderConfirmationFieldset"><legend class ="OrderDataLegend">Order Summary</legend>
<td align="left" width="60%" runat="server" id="Td25">
<asp:Label ID="lblQuantity" runat="server" Text="Quantity: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Quantity") %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td26">
<asp:Label ID="lblProductName" runat="server" Text="Product Name: " CssClass = "lblOrderConfirmation">
</asp:Label><%# Eval("ProductName" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td27">
<asp:Label ID="lblProductPrice" runat="server" Text="Product price: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("ProductPrice" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td28">
<asp:Label ID="lblSubtotal" runat="server" Text="Subtotal: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Subtotal" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td29">
<asp:Label ID="lblTotal" runat="server" Text="Total: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Total" ,"{0:c}" ) %>
<br />
</td>
</ItemTemplate>
</asp:Repeater>
Can Anyone help?
Thank you in advance!
Your repeater markup doesn't look right.
have it as follows:
<asp:Repeater ID="rptConfirmOrder" runat="server" OnItemDataBound="rptConfirmOrder_ItemDataBound">
<ItemTemplate>
// stuff
</ItemTemplate>
</asp:Repeater>
And if you have it that way, and this was a paste error, then forget the value in the database/entity etc.
You can verify accurately, what value is being bound by tapping into the row data bound event as follows..
protected void rptConfirmOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem as YOUR_ENTITY_TYPE;
Debug.Assert(5 == dataItem.Quantity);
}
}
I think you may need to add the < itemtemplate > tags in after the opening repeater and close it off before the closing repeater tag.

Group Repeater for each Distinct User

I have the following repeater item:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
on page load I am binding the Repeater
else if(Convert.ToInt32(Session["UserLevel"]) != 0 && Convert.ToInt32(Session["UserLevel"]) < 151)
{
RptLeaveRequests.DataSource = newLeaveLogic().GetManagerUnverifiedLeaveRequests(Convert.ToInt32(Context.User.Identity.Name));
RptLeaveRequests.DataBind();
hasRequests = true;
}
Now how can group the Repeater item for each distinct user returned by the GetManagerUnferifiedLeaveRequests which returns a DataTable in the method below:
protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (hasRequests)
{
//grouping
}
}
I see two possible solutions:
1) Use nested repeaters:
<asp:Repeater ID="RptGroups" runat="server" onitemdatabound="RptGroups_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="RptLeaveRequests" runat="server" onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
2) Use single repeater for groups and render each group using for example DataList or other control for list of items.
In this case you bind list of groups to datasource here:
RptLeaveRequests.DataSource = itemsGroups;
RptLeaveRequests.DataBind();
And bind list of group items here:
protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//bind here list of group items
}
I would prefer the second version.

Categories