How to use IF ELSE using EVAL condition in aspx c# - c#

I am trying to use if else conditions in aspx page using Eval conditions. But its showing error red line in else condition and in ) in if condition.
Code
<div class="price_mth text-center">
<% if (string.IsNullOrEmpty(Eval("PromoPrice", "{0:,00}").ToString()) %>) { %>
<asp:panel id="pricePanel" runat="server" >
<h2><i class="fa fa-usd" aria-hidden="true"></i> <%# Eval("Price", "{0:,00}") %><sup>.00/mo</sup></h2>
</asp:panel>
<% } %>
<% else { %>
<asp:panel id="promopanel" runat="server">
<div class="style-1">
<h2> <del><i class="fa fa-usd" aria-hidden="true"></i> <span class="amount"><%# Eval("Price", "{0:,00}") %></span> </del><sup>.00/mo</sup></h2>
<h2><ins><i class="fa fa-usd" aria-hidden="true"></i> <span class="amount"><%# Eval("PromoPrice", "{0:,00}") %></span></ins><sup>.00/mo</sup></h2>
</div>
</asp:panel>
<% } %>

You can use a method as shown below:
<asp:Panel ID="p1" Visible='<%# BLL.Logic.ShowPanel(Eval("PromoPrice", "{0:,00}")) %>' runat="server">
</asp:Panel>

Related

Display nested replies for a comments in a blog

I'm developing a blog that has a section for getting some comments and replies from the users. The first part is of my code for getting the comments and replies is the following one:
C# code:
var a = commentBLL.GetAll(BlogID);
rp_MainComments.DataSource = a;
rp_MainComments.DataBind();
protected void rp_MainComments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton btn = e.Item.FindControl("btn_Comment_Reply") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btn);
HiddenField lt = (HiddenField)e.Item.FindControl("hf_Main_CommentID");
Repeater rpcommentreply = (Repeater)e.Item.FindControl("rp_Comment_Reply");
Panel panel_Comment_Reply = e.Item.FindControl("panel_Comment_Reply") as Panel;
if (!string.IsNullOrEmpty(lt.Value))
{
var b = commentBLL.GetByCommentID(Convert.ToInt32(lt.Value));
rpcommentreply.DataSource = b;
rpcommentreply.DataBind();
panel_Comment_Reply.Visible = false;
}
}
}
ASPX code:
<asp:Repeater ID="rp_MainComments" OnItemDataBound="rp_MainComments_ItemDataBound" OnItemCommand="rp_MainComments_ItemCommand" runat="server">
<ItemTemplate>
<asp:HiddenField ID="hf_Main_CommentID" runat="server" Value='<%# Eval("Comment_Id") %>' />
<ul class="comments">
<li>
<div class="comment">
<div class="img-thumbnail d-none d-sm-block">
<%--<asp:Image ID="Image_7" CssClass="avatar" runat="server" ImageUrl="http://placehold.it/40x40" />--%>
<i class="fa fa-user fa-fw fa-5x"></i>
</div>
<div class="comment-block">
<div class="comment-arrow"></div>
<span class="comment-by">
<strong><%# Eval("Name") %></strong>
<span class="float-right">
<span>
<asp:LinkButton ID="btn_Comment_Reply" Text="Reply" ClientIDMode="AutoID" runat="server" OnClick="btn_Comment_Reply_Click" CommandName="Edit" CommandArgument='<%# Eval("Comment_Id") %>' /></span>
</span>
</span>
<p><%# Eval("Message") %></p>
<span class="date float-right">
<%# Eval("Comment_Date") %></span>
</div>
</div>
</li>
<li>
<asp:HiddenField ID="hf_PanelValue" Value='<%# Container.ItemIndex %>' runat="server" />
<asp:Repeater ID="rp_Comment_Reply" OnItemCommand="rp_Comment_Reply_ItemCommand" runat="server">
<ItemTemplate>
<ul class="comments reply" id="commentreply">
<li>
<div class="comment">
<div class="img-thumbnail d-none d-sm-block">
<%--<asp:Image ID="Image_8" CssClass="avatar" runat="server" ImageUrl="http://placehold.it/40x40" />--%>
<i class="fa fa-user fa-5x fa-fw"></i>
</div>
<div class="comment-block">
<div class="comment-arrow"></div>
<span class="comment-by">
<strong><%# Eval("Name") %></strong>
<span class="float-right">
<span>
<asp:LinkButton ID="btn_Reply" Text="Reply" ClientIDMode="AutoID" runat="server" OnClick="btn_Reply_Click" CommandName="Edit" CommandArgument='<%# Eval("Parent_Id") %>' /></span></span>
</span>
<p><%# Eval("Message") %></p>
<span class="date float-right"><%# Eval("Comment_Date") %></span>
</div>
</div>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
This is the output:
Now, I would like to to show the nested replies to which user reply in a post it's shown only upto second level but I need a third as well as fourth level.
How is it possible?

How Can I Put <hr /> in the Repeater After 3 Replies

I want to put <hr /> in the repeater after 3 replies/products.
This is my repeater code:
<asp:Repeater ID="RptrProduct" runat="server">
<ItemTemplate>
<figure class="span4 slide">
<a href="#">
<img src="<%#"/images/product/"+Eval("ProductImage")%>" alt="" class="pro-img" />
</a>
<span class="title" style="margin-left: 50px;"><%#Eval("ProductName") %></span>
</figure>
</ItemTemplate>
</asp:Repeater>
And this is my .cs code for bind repeater:
BL_Product blp = new BL_Product();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RptrProduct.DataSource = blp.ListProduct();
RptrProduct.DataBind();
}
}
Just add <% if ((Container.ItemIndex + 1) % 3 == 0) { %> <hr /><% } %> below the figure tag closing.
<asp:Repeater ID="RptrProduct" runat="server">
<ItemTemplate>
<figure class="span4 slide">
<a href="#">
<img src="<%#"/images/product/"+Eval("ProductImage")%>" alt="" class="pro-img" />
</a>
<span class="title" style="margin-left: 50px;"><%#Eval("ProductName") %></span>
</figure>
<% if ((Container.ItemIndex + 1) % 3 == 0) { %>
<hr />
<% } %>
</ItemTemplate>
</asp:Repeater>

The 'href' property had a malformed URL: Cannot use a leading .. to exit above the top directory

I did a lot of research on this topic but I am not able to find any solution to this problem. I checked my page thoroughly but I am not able to fine "../" in my page. Need suggestions.My code on page is like this:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="PassRecovery.aspx.cs" Inherits="Login_PassRecovery" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<style>
input[type="text"], input[type="password"], input[type="email"], select, textarea {
height: 2em;
background:none;
border-radius:0px;
}
input[type="submit"], input[type="reset"], input[type="button"], .button {
padding: 16px;
line-height:0em;
height:2em;
border-radius:0px;
}
</style>
<div class="container">
<section id="main" class="container" style="padding-top:6em;">
<header>
<h2>Forgot your password?</h2>
<p>No problem, we can send it to you.</p>
</header>
<div class="row" style="width:65%;margin:0 auto;min-height:331px;">
<div class="12u">
<!-- Lists -->
<section class="box">
<div class="row collapse-at-2">
<div class="requiredFieldPrompt 3u" >
Your Email :
</div>
<div class="9u">
<div class="12u">
<asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox></div>
<div class="12u"> <asp:RequiredFieldValidator ID="valEmail" runat="server" ErrorMessage="Please Enter your Email address"
ControlToValidate="txtEmailAddress" Display="Dynamic"></asp:RequiredFieldValidator></div>
<div class="12u"> <asp:RegularExpressionValidator ID="valEmailAddress" runat="server" ErrorMessage="Invalid Email Address"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtEmailAddress"
Display="Dynamic"></asp:RegularExpressionValidator></div>
<br />
</div>
</div>
<div class="12u" style="text-align:center;">
<asp:LinkButton ID="lnkGetUser" runat="server" CssClass="button" Text="Submit" OnClick="lnkGetUser_Click"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CssClass="button" href="Login.aspx" Text="Back" CausesValidation="false"
OnClick="lnkCancel_Click"></asp:LinkButton>
</div>
<div class="12u" style="text-align: center;">
<h4>
<asp:Label ID="lblMessage" runat="server"></asp:Label></h4>
</div>
<div class="12u" style="text-align:center;">
<asp:LinkButton ID="lnkContinue" runat="server" href="Login.aspx" Text="Sign in to your account"
CausesValidation="false" onclick="lnkContinue_Click"></asp:LinkButton>
</div>
</section>
</div>
</div>
</section>
</div>
</asp:Content>
Got the answer. Problem was with the master page. my masterpage was in the main directory and I was using ../ on it. Thx All. :)

How do I bind a string array(list of files in my case) to a variable in the item template of a repeater?

How do I bind a string array(list of files in my case) to a variable in the item template?
Here is what I have so far but I am not sure what to do for the code behind itemdatabound.
I am trying to put each url in the <% Photo_URL %> variable.
Any help would be appreciated.
Thanks in advanced.
Page Code
<asp:Repeater id="unorderedList" runat="server" OnItemDataBound="unorderedList_ItemDataBound">
<HeaderTemplate>
<ul class="thumbs noscript">
</HeaderTemplate>
<ItemTemplate>
<li>
<a class="thumb" href='<%# Photo_URL %>'>
<img src='<%# Photo_URL %>'>
</a>
<div class="caption">
<div class="download">
<a href='<%# Photo_URL %>'>Download Original</a>
</div>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Code Behind
private void Page_Init(object sender, EventArgs e)
{
string[] photos = Directory.GetFiles(ImagesLocation);
unorderedList.DataSource = photos;
unorderedList.DataBind();
}
protected void unorderedList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//what goes here
}
<div class="download">
<a href='<%# Container.DataItem %>'>Download Original</a>
</div>
No need for the ItemDataBound event, you just need to use the <%# Container.DataItem %> syntax, like this:
<ItemTemplate>
<li>
<a class="thumb" href='<%# Photo_URL %>'>
<img src='<%# Container.DataItem %>'>
</a>
<div class="caption">
<div class="download">
<a href='<%# Container.DataItem %>'>Download Original</a>
</div>
</div>
</li>
You can use the Container.DataItem syntax:
<asp:Repeater id="unorderedList" runat="server" OnItemDataBound="unorderedList_ItemDataBound">
<HeaderTemplate>
<ul class="thumbs noscript">
</HeaderTemplate>
<ItemTemplate>
<li>
<a class="thumb" href='<%#Container.DataItem%>'>
<img src='<%#Container.DataItem%>'>
</a>
<div class="caption">
<div class="download">
<a href='<%#Container.DataItem%>'>Download Original</a>
</div>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
You'll need to add a GridView in the ItemTemplate. Set it's DataSource to <%# Eval("string_list_name") %> where string_list_name is the variable name of your List of strings.

C# ASP.NET update panel inside a repeater error

I have a problem with an updatepanel when I put it inside a repeater control I get an error, and I need to refresh only a single post when the user click on a link button, here is my code...
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="MyButtonCommandEvent">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="post">
<asp:HiddenField ID="postID_hidden" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem,"posts_ID") %>' />
<div class="Thumb">
<img src="thumbs/abdo_thumb.jpg"> </img></div>
<span class="user"><%#DataBinder.Eval(Container.DataItem, "poster_name")%>
</span>
<div class="post-body">
<p>
<%#DataBinder.Eval(Container.DataItem,"description")%>
</p>
</div>
<div class="post-options" style=" height:22px; ">
<span class="first"><%#DataBinder.Eval(Container.DataItem,"post_date")%></span>
<ul style="display:inline; list-style-type: none;">
<li>
<div class="tooltip">
Comments
<img class="tool-img" src="Images/comments.png"> : <%#DataBinder.Eval(Container.DataItem,"comment_num") %>
</img>
</div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="like_linkbtn" runat="server" CommandName="Like"><%#(DataBinder.Eval(Container.DataItem, "name_like").ToString() == "") ? "Like" : DataBinder.Eval(Container.DataItem, "name_like")%></asp:LinkButton>
<img class="tool-img" src="images/likes.png"> : <%#DataBinder.Eval(Container.DataItem,"like_counter") %>
</img></div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="hate_linkbtn" runat="server" CommandName="Hate"><%#(DataBinder.Eval(Container.DataItem, "name_hate").ToString() == "") ? "Hate" : DataBinder.Eval(Container.DataItem, "name_hate")%></asp:LinkButton>
<img class="tool-img" src="images/hate.png"> : <%#DataBinder.Eval(Container.DataItem,"hate_counter") %>
</img></div>
</li>
</ul>
</div>
<div class="finish">
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
The error is : Compiler Error Message: CS1061: 'System.Web.UI.Control' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?).
This is making me really frustrated please help.
PS: It works if I removed the updatepanel and made it outside the repeater (The repeater inside the updatepanel instead of the updatepanel inside the repeater but that's not what I want)
Another solution is to cast Container as IDataItemContainer (((IDataItemContainer)Container)).
Instead of:
<%# DataBinder.Eval(Container.DataItem, "Column") %>
Use:
<%# DataBinder.Eval(((IDataItemContainer)Container).DataItem, "Column") %>
This solution is ideal if you are casting the DataItem as an object, eg:
<%# ((MyClass)Container.DataItem).ColumnName %>
This becomes:
<%# ((MyClass)((IDataItemContainer)Container).DataItem).ColumnName %>
Put Update panel out side of repeater. It will work.
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="MyButtonCommandEvent">
<ItemTemplate>
<div class="post">
<asp:HiddenField ID="postID_hidden" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem,"posts_ID") %>' />
<div class="Thumb">
<img src="thumbs/abdo_thumb.jpg"> </img></div>
<span class="user"><%#DataBinder.Eval(Container.DataItem, "poster_name")%>
</span>
<div class="post-body">
<p>
<%#DataBinder.Eval(Container.DataItem,"description")%>
</p>
</div>
<div class="post-options" style=" height:22px; ">
<span class="first"><%#DataBinder.Eval(Container.DataItem,"post_date")%></span>
<ul style="display:inline; list-style-type: none;">
<li>
<div class="tooltip">
Comments
<img class="tool-img" src="Images/comments.png"> : <%#DataBinder.Eval(Container.DataItem,"comment_num") %>
</img>
</div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="like_linkbtn" runat="server" CommandName="Like"><%#(DataBinder.Eval(Container.DataItem, "name_like").ToString() == "") ? "Like" : DataBinder.Eval(Container.DataItem, "name_like")%></asp:LinkButton>
<img class="tool-img" src="images/likes.png"> : <%#DataBinder.Eval(Container.DataItem,"like_counter") %>
</img></div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="hate_linkbtn" runat="server" CommandName="Hate"><%#(DataBinder.Eval(Container.DataItem, "name_hate").ToString() == "") ? "Hate" : DataBinder.Eval(Container.DataItem, "name_hate")%></asp:LinkButton>
<img class="tool-img" src="images/hate.png"> : <%#DataBinder.Eval(Container.DataItem,"hate_counter") %>
</img></div>
</li>
</ul>
</div>
<div class="finish">
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>

Categories