How to get item from listview to disable href - c#

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

Related

How to update values in a ListView with ajax in asp.net web form

I'm using a listview and ajax in an asp.net web form. In part of that form, I display comments, which readers can rate, either positive or negative.
This value isn't updated unless the page is refreshed, is there a way to update the value without the need of refreshing the page?
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
<ItemTemplate>
<div class="row comm_info_bar ">
<div class="col-md-5 RightDisplay"><%# Eval("name") %></div>
<div class="col-md-5 comm_info_date"><%# Eval("date") %></div>
<asp:LinkButton ID="negBtn" class="glyphicon glyphicon-minus voteCommentIcon voteContNeg text-danger smallGlyph" runat="server" CommandName="negative" CommandArgument='<%#Eval("id")%>' />
<asp:Label ID="lblnegative" name="lblnegative" class=" voteNumNeg" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "negative") %>'></asp:Label>
<asp:LinkButton ID="posBtn" class="glyphicon glyphicon-plus voteCommentIcon voteContNeg text-success smallGlyph" runat="server" CommandName="positive" CommandArgument='<%#Eval("id")%>' />
<asp:Label ID="lblpositive" class="voteNumPos" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "positive") %>'></asp:Label>
</div>
<div class="row">
<div class="col-md-12 comments"><%# Eval("text") %></div>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
and in code:
static List<Int64> commentsUser = new List<long>();
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string name = e.Item.DataItemIndex.ToString();
long commentId = Convert.ToInt64(e.CommandArgument);
ArticleCommentsDataClass ArticleComment = new ArticleCommentsDataClass();
if (e.CommandName == "positive")
{
if (!searchcomments(commentId))
{
ArticleComment.Comments_positive(commentId);
commentsUser.Add(commentId);
}
}
else
{
if (!searchcomments(commentId))
{
ArticleComment.Comments_negative(commentId);
commentsUser.Add(commentId);
}
}
}
Is there anyone who has an idea on how to do this?
This sounds like you are binding (reading the data of your grid) before executing the command. Since the command changes the data, you should rebind the grid or update it in any other way.
Check out this article that talks about when each event is raised

ASP repeater collecting data from radiobuttons

So I have a Asp repeater that prints out some radiobuttons that a user will use to answer a test. What I need to do is to check which radiobuttons are checked so that I can save them to a XML file.
Here is the code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="repeaterDiv">
<ul id="list">
<li style="list-style: none">
<p style="font-weight: bold">Fråga nummer: <%# Container.ItemIndex + 1 %></p>
<asp:Label ID="q1_label" runat="server" Text='<%#Eval ("text")%>'></asp:Label>
<asp:CheckBox ID="q1_svar1" Text='<%#Eval("q1")%>' runat="server" CssClass="radioButtons;" />
<asp:CheckBox ID="q1_svar2" Text='<%#Eval("q2")%>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar3" Text='<%#Eval("q3") %>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar4" Text='<%#Eval("q4") %>' runat="server" CssClass="radioButtons;"/>
<hr />
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
I dont really know how to target the checked ones, what do I do from here? Thanks in advance.
You can use foreach loop in repeater items and them user FindControl method
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("q1_svar1");
if (chk.Checked)
{
//Your code ...
}
}
}

How to get Footer Item value on code behind with nested Repeater?

I have a nested repeater and I use a textbox in footer template. I wanto to get textbox.text value in button click. Here is my repeater:
<asp:Repeater ID="rprSSFirst" runat="server" OnItemDataBound="rprSSFirst_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
<asp:Repeater ID="rprSSNested" runat="server" > //Nested Repeater
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="btn_Save_Click" Text="Save" />
</div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
//In Code Behind
protected void btn_Save_Click(object sender, EventArgs e)
{
TextBox txtAns = (TextBox)rprSSFirst.Controls[rprSSFirst.Controls.Count - 1].FindControl("textbox");
}
But txtAns Value is always null. How to get footer item textbox value in button click? or any other way?
Thanks for your answers.
You have to find the nested RepeaterItem first where both controls are sitting. You can get it by casting the NamingContainer:
protected void btn_Save_Click(object sender, EventArgs e)
{
Button btnSave = (Button) sender;
RepeaterItem item = (RepeaterItem) btnSave.NamingContainer;
TextBox txtAns = (TextBox) item.FindControl("textbox");
}
You can use Commandname property like this for button of nested repeater:
<asp:Repeater ID="rprSSNested" runat="server" OnItemCommand="rprSSNested_ItemCommand" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" CommandName="cmd" CommandArgument="arg"/>
</div>
</FooterTemplate>
</asp:Repeater>
And add event in c# code like this:
protected void rprSSNested_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.CommandName == "cmd")
{
string ss = ((TextBox)e.Item.FindControl("textbox")).Text;
Response.Write(ss);
}
}
}

Asp.Net repeater control with javascript

I have created FAQ page using Repeater Control, The questions and Answers are bound Run-time from Code Behind as Follows.
ASPX code
<asp:Repeater ID="RepDetails" runat="server">
<ItemTemplate>
<table id="tblRepeater">
<tr id="QARow" runat="server">
<td>
<div id="QuestionDiv" onclick="return show(this, 'AnswerDiv');">
Q:<asp:label id="lblQuestion" runat="server" Text='<%# Eval("Question")%>' CssClass="lblQueClass"></asp:label>
</div>
<div id="AnswerDiv" style="display:none;">
Ans:<asp:Label id="lblAnswerClass" runat="server" Text='<%# Eval("Answer")%>' CssClass="lblAnswerClass"></asp:Label>
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
It Works great ! The thing is i have used following Script for opening and closing the Answer.
<script type="text/javascript">
function show(QuestionDiv, AnswerDiv) {
var arrDIVs = QuestionDiv.parentNode.getElementsByTagName("Div");
for (var i = 0; i < arrDIVs.length; i++) {
var oCurDiv = arrDIVs[i];
if (oCurDiv.id.indexOf(AnswerDiv) >= 0) {
var blnHidden = (oCurDiv.style.display == "none");
oCurDiv.style.display = (blnHidden) ? "block" : "none";
}
}
return false;
}
</script>
It works like, when clicked on One Question It shows the answer of that question.
My Question is: I want to update the script so as when we click on One particular question, It should display the Answer of that question, also it should hide the Answer of other Question.(like http://www.edubrainschool.com/faq.php).
The best way for that would be to bind click of that TR. You can do this by applying a css to the TR.
<asp:Repeater ID="RepDetails" runat="server">
<ItemTemplate>
<table id="tblRepeater">
<tr id="QARow" runat="server" class="TROpenCSS">
<td>
<div id="QuestionDiv" class="QuestionCSS" onclick="return show(this, 'AnswerDiv');">
Q:<asp:label id="lblQuestion" runat="server" Text='<%# Eval("Question")%>' CssClass="lblQueClass"></asp:label>
</div>
<div id="AnswerDiv" class="AnswerCss" style="display:none;">
Ans:<asp:Label id="lblAnswerClass" runat="server" Text='<%# Eval("Answer")%>' CssClass="lblAnswerClass"></asp:Label>
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And the bind the click event of those CSS
$('.TROpenCSS').bind('click',function(){
// Do your code to show the TD
});
Add other bind for all clicks / mouseover
Or you can use "ddaccordion.js" or refer to the following link

Show picture in 30 days ASP.NET

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

Categories