This is how I will find out about my like a true or false, and I will do my Repeater.
If my like is false then the display content. but if it is true then the display content on the page.
Right now makes this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control.
<asp:Repeater ID="RepeaterReport" runat="server">
<ItemTemplate>
<%--I have much more different content here.--%>
<% if (Convert.ToBoolean(Eval("like")) != true)
{ %>
<a href="../mentor/report.aspx?like=<%# Eval("IdBesked") %>" class="btn btn-success mr-xs mb-sm">
<i class="fa fa-thumbs-up"></i>Synes godt om
</a>
<% } %>
<%--I have much more different content here.--%>
</ItemTemplate>
</asp:Repeater>
Error are here:
<% if (Convert.ToBoolean(Eval("like")) == false) {%>
Select data:
var MentorBeskedReport = from mb in db.MentorBeskeds
where mb.Id == beskedId && mb.Til_BrugerId == Helper.ReturnBrugerId() && mb.godkendt == true && mb.opretdato < months
select new
{
IdBesked = mb.Id,
Navn = mb.brugere.fornavn + " " + mb.brugere.efternavn,
dag = mb.opretdato,
tekst = mb.tekst,
like = mb.like // Bit in database TRUE/FALSE
};
if (MentorBeskedReport != null)
{
//Report
RepeaterReport.DataSource = MentorBeskedReport.ToList().OrderByDescending(i => i.IdBesked);
RepeaterReport.DataBind();
}
The problem is that you're using the Eval method inside an if statement.
You should change your a tag to runat=server and give it an ID. Then, instead of doing the validation inside your .aspx file, you should add an ItemDataBound event and check it on .aspx.cs:
<asp:Repeater ID="RepeaterReport" ItemDataBound="RepeaterReport_ItemDataBound" runat="server">
<ItemTemplate>
<a id="myAnchor" runat="server" href="../mentor/report.aspx?like=<%# Eval("IdBesked") %>" class="btn btn-success mr-xs mb-sm">
<i class="fa fa-thumbs-up"></i>Synes godt om
</a>
</ItemTemplate>
</asp:Repeater>
public void RepeaterReport_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "like")) != true)
{
((HtmlControl)e.Item.FindControl("myAnchor")).Visible = false;
}
}
}
Related
I have a repeater using for getting an image nad a radio button on the bottom. I want to achieve a combination of this radio button (repeater speaking) with the property of autoexcluyent feature. How can I achieve it?
As far as I got ...
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<div class="col-xs-12 col-sm-4 col-md-3">
<asp:Image ID="img" runat="server" ImageUrl="<%#GetRutaImagen(Eval("id").ToString())%>" />
<span>
<asp:RadioButton runat="server" ID="rb1" Text='<%#Eval("description").ToString()%>' GroupName="nameGroup"/>
</span>
</div>
</ItemTemplate>
</asp:Repeater>
With this code I am getting a one radio button per each image but no one autoexcuyent even I am using GroupName property
USING NET FRAMEWORK 4.6.2.
I don't know if easy way to manage this situation however you can manage by below code. It will be good to wrap your contents into update panel so you can prevent page refresh on checkbox changed.
Additionally, IsChecked property being used to initialize checked on page load. You can remove if not required.
.ASPX
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<div class="col-xs-12 col-sm-4 col-md-3">
<span>
<asp:RadioButton runat="server" ID="rb1" Checked='<%# Eval("IsChecked") %>' AutoPostBack="true" OnCheckedChanged="rb1_CheckedChanged" Text='<%#Eval("description").ToString()%>' />
</span>
</div>
</ItemTemplate>
</asp:Repeater>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<test1> lst = new List<test1>();
lst.Add(new test1() { Id = 1, description = "R1", IsChecked = false });
lst.Add(new test1() { Id = 3, description = "R2", IsChecked = true });
lst.Add(new test1() { Id = 2, description = "R3", IsChecked = false });
lst.Add(new test1() { Id = 4, description = "R4", IsChecked = false });
repeater1.DataSource = lst;
repeater1.DataBind();
}
}
protected void rb1_CheckedChanged(object sender, EventArgs e)
{
foreach (RepeaterItem item in repeater1.Items)
{
(item.FindControl("rb1") as RadioButton).Checked = false;
}
(sender as RadioButton).Checked = true;
}
After researching between SOF and a few forums I implemented a quite right solution using JS. I am handling another problem related with OnCheckedChanged´s event on RadioButton...
But the initial issue is fixed.
Forum Solution
I am posting the solution that works for me, using as base as a help coming from a forum, hoping it helps others as well with this bug.
REPEATER.
<asp:Repeater runat="server" ID="repeater1" OnItemDataBound="repeater1_ItemDataBound">
<ItemTemplate>
<div class="col-xs-12 col-sm-4 col-md-3">
<asp:Image ID="img" runat="server" ImageUrl="<%#GetRutaImagen(Eval("id").ToString())%>" />
<span>
<asp:RadioButton runat="server" ID="rb1" Text='<%#Eval("description").ToString()%>' GroupName="nameGroup" OnCheckedChanged="rb1_CheckedChanged"/>
</span>
</div>
</ItemTemplate>
</asp:Repeater>
JS
<script>
function SetUniqueRadioButton(nameregex, current) {
for (i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i]
if (elm.type == 'radio') {
elm.checked = false;
}
}
current.checked = true;
}
</script>
BACKEND
protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButton rbLogoSeleccionado = (RadioButton)e.Item.FindControl("rb1");
string script = "SetUniqueRadioButton('repeater1.*nameGroup',this)";
rbLogoSeleccionado.Attributes.Add("onclick", script);
}
}
catch (Exception ex)
{
PIPEvo.Log.Log.RegistrarError(ex);
throw;
}
}
I have built a slideshow. Each of the slides has a div placed over the top of it. The div contains some text. What I want to do is if the h2 and p element have no text then hide the div that contains them. I have attempted to hide the div from the repeaters DataBound event but it is still showing the div over the slide.
Here's what I have so far:
aspx:
<asp:Repeater ID="rptSlides" runat="server" ClientIDMode="Static" OnItemDataBound="rptSlides_ItemDataBound">
<ItemTemplate>
<div runat="server" id="slideDiv" class="slide">
<img runat="server" src='<%# Eval("slideImgPath")%>' />
<div id="slideTextdiv" runat="server" class="slideText">
<h2 id="titlePlaceHolder" runat="server" class="slideTitle text-center text-capitalize h2 ">
<%# Eval("slideTitle") %>
</h2>
<p id="textPlaceHolder" runat="server" class="slideDes">
<%# Eval("slideDesc") %>
</p>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void rptSlides_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (RepeaterItem item in rptSlides.Items)
{
HtmlGenericControl titleText = (HtmlGenericControl)item.FindControl("titlePlaceHolder");
HtmlGenericControl slidetextDesc = (HtmlGenericControl)item.FindControl("textPlaceHolder");
HtmlGenericControl slideDiv = (HtmlGenericControl)item.FindControl("slideTextdiv");
if (titleText.InnerText == "" && slidetextDesc.InnerText == "")
{
slideDiv.Style.Add("Display", "none");
}
}
}
Further info
I have also tried the following:
foreach (RepeaterItem item in rptSlides.Items)
{
HtmlGenericControl slideDiv = (HtmlGenericControl)FindControlRecursive(item, "slideTextdiv");
HtmlGenericControl titleText = (HtmlGenericControl)FindControlRecursive(item, "titlePlaceHolder");
HtmlGenericControl slidetextDesc = (HtmlGenericControl)FindControlRecursive(item, "textPlaceHolder");
if (string.IsNullOrWhiteSpace(titleText.InnerText) && string.IsNullOrWhiteSpace(slidetextDesc.InnerText))
{
slideDiv.Attributes["class"] = "emptySlideText";
}
}
I created a class in the css called emptySlideText, like so:
.emptySlideText {
display:none; }
Unfortunatley this has had no effect.
I have also tried changing the line
slideDiv.Style.Add("Display", "none");
to
slideDiv.Attributes.Add("style", "display:none");
But again it made no difference
First of all you don't need a foreach loop inside your rptSlides_ItemDataBound event as ItemDataBound event of a repeater control iterates for each item. Next Div is not a HtmlGenericControl control rather it is HtmlControl control. Once you have this you can hide the div using Visible property like this:-
protected void rptSlides_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HtmlControl slideTextdiv = (HtmlControl)e.Item.FindControl("slideTextdiv");
HtmlGenericControl titlePlaceHolder = (HtmlGenericControl)e.Item.FindControl("titlePlaceHolder");
HtmlGenericControl textPlaceHolder = (HtmlGenericControl)e.Item.FindControl("textPlaceHolder");
if (titlePlaceHolder != null)
titlePlaceHolder.InnerText = Regex.Replace(titlePlaceHolder.InnerText, #"\r\n?|\n", "").Trim();
if (textPlaceHolder != null)
textPlaceHolder.InnerText = Regex.Replace(textPlaceHolder.InnerText, #"\r\n?|\n", "").Trim();
if (String.IsNullOrEmpty(titlePlaceHolder.InnerText) && String.IsNullOrEmpty(textPlaceHolder.InnerText))
{
slideTextdiv.Visible = false;
}
}
}
Please include using System.Text.RegularExpressions; to import Regex class.
I have this code for a blog which is nesting a repeater control within a datalist.
I'm trying to assign the alt tags of the repeater control images to be the title of the post coming through the datalist however, it will not let me though this because it has a different data source.
Any suggestions as to how I can do this?
Here's my code:
<asp:DataList runat="server" ID="DataList1" ItemStyle-CssClass="row1BackgroundBlock" OnItemDataBound="DataList1_ItemDataBound" >
<ItemTemplate>
<!-- <%#Eval("PostId")%> post -->
<div class="dateBox"><asp:Label ID="Label1" runat="server" ForeColor="#ffffff" Font-Bold="False" Text='<%# Eval("PublicationDate","{0:dd/MM/yyyy}") %>' CssClass="postmetadata"></asp:Label></div>
<div class="roundPanelStack">
<h2><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) %>'><%# Eval("Title") %></asp:HyperLink></h2>
<ul><li class="postBy">Posted in <%#Eval("CategoryName")%> by <%#Eval("Author")%></li><li class="last">{<%# Engine2010.Blog.PostComment.GetAllByPostId(int.Parse(Eval("PostId").ToString()), 766).Rows.Count.ToString()%> comments}</li></ul>
<div class="thumbWindow">
<div style="border:7px solid #fff;display:block;height:95px;left:1px;position:absolute;top:1px;width:95px;z-index:5;"></div>
<asp:Repeater ID="repImgs" runat="server">
<ItemTemplate>
<img class="thumb_view" src="/uploads/images/Blog/<%# Eval("Directory").ToString().Substring(Eval("Directory").ToString().LastIndexOf("\\")+1) %>/<%# Eval("Name") %>" alt="" border="0" width="143" height="143" />
</ItemTemplate>
</asp:Repeater>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
<img src="/includes/images/general/default.jpg" alt="Default" border="0" width="110" height="110" />
</asp:PlaceHolder>
</div>
<p>
<asp:Label ID="Label2" runat="server" Text='<%#CommonFunctions.Remove_HTML_Tags(Eval("Description").ToString()) %>' CssClass="entry"></asp:Label>
</p>
<div class="linksMore">
<asp:HyperLink ID="FullStoryLink" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) %>' CssClass="fullstory">Read Full Post</asp:HyperLink><a class="spacer"> // </a><asp:HyperLink ID="CommentsLink" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) + "#comment" %>' CssClass="comments">Comment</asp:HyperLink>
</div>
</div>
<!-- /first post -->
</ItemTemplate>
</asp:DataList>
Backend:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater repImages = (Repeater)e.Item.FindControl("repImgs"); // Child repeater with images for room
string amenText = ((DataRowView)e.Item.DataItem).Row.ItemArray[7].ToString();
DirectoryInfo folderBr = new DirectoryInfo(Server.MapPath("~/uploads/images/Blog/") + amenText);
// List<Engine2010.ImageObj> imageObjs = folderBr.GetImagesInPath();
if (folderBr.Exists)
{
repImages.DataSource = folderBr.GetFiles("thumb*.*"); // Bind room images to child repeater
repImages.DataBind();
if (repImages.Items.Count == 0)
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
else {
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
}
In the ItemDataBound event of your DataList1 after binding repImages repater control, iterate through each item by using RepeaterItem and then find your img control and assign title in alt tag like this -
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater repImages = (Repeater)e.Item.FindControl("repImgs");
string amenText = ((DataRowView)e.Item.DataItem).Row.ItemArray[7].ToString();
DirectoryInfo folderBr = new DirectoryInfo(Server.MapPath("~/uploads/images/Blog/") + amenText);
if (folderBr.Exists)
{
repImages.DataSource = folderBr.GetFiles("thumb*.*"); // Bind room images to child repeater
repImages.DataBind();
if (repImages.Items.Count == 0)
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
// Iterate thru each item of your child repeate control.
foreach (RepeaterItem repeaterItem in repImages.Items)
{
if (repeaterItem.ItemType == ListItemType.Item ||
repeaterItem.ItemType == ListItemType.AlternatingItem)
{
((HtmlImage)(repeaterItem.FindControl("imgId"))).Alt = amenText; // Assume amenText is the title of your post
}
}
}
else
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
}
good luck...
I have three repeaters with a parent child relationship (so we have a parent repeater, and child repeater, and a child-child repeater) with the inner-most repeater not being triggered. here is my aspx page for the layout:
<asp:Repeater ID="rptMission" runat="server">
<HeaderTemplate>
<ul id="acc1" class="ui-accordion-container">
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="ui-accordion-left"></div>
<a class="ui-accordion-link acc1"><%# Eval("Name") %><span class="ui-accordion-right"></span></a>
<div>
<ul class="ui-accordion-container" id="acc2">
<asp:Repeater ID="rptActivity" runat="server">
<ItemTemplate>
<li>
<div class="ui-accordion-left"></div>
<a class="ui-accordion-link acc2"><%# Eval("Name") %>
<span class="ui-accordion-right"></span></a>
<div>
<asp:Repeater ID="rptProject" runat="server">
<ItemTemplate>
<%# Eval("Name") %><br/>
</ItemTemplate>
</asp:Repeater>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
The top two repeaters work great but the 'rptProject' repeater doesn't get triggered. Here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
_presenter = new TierTypesPresenter(this);
rptMission.ItemDataBound += new RepeaterItemEventHandler(rptMission_ItemDataBound);
RaiseStartUp();
}
void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Mission row = (Mission)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptActivity = (Repeater)item.FindControl("rptActivity");
var activity = _presenter.GetActivitiesByMission(row.Id);
rptActivity.DataSource = activity;
rptActivity.DataBind();
}
}
void rptActivity_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Activity row = (Activity)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptProject = (Repeater)item.FindControl("rptProject");
var project = _presenter.GetProjectsByActivities(row.Id);
rptProject.DataSource = project;
rptProject.DataBind();
}
}
public void SetMissions(IEnumerable<Mission> missionList)
{
rptMission.DataSource = missionList;
rptMission.DataBind();
}
If I could trigger the second ItemDataBound event 'rptActivity_ItemDataBound' I think it would work fine but it gets skipped over. Thanks for any insight!
I solved my problem by putting the third repeater binding into the second repeaters ItemBoundEvent. Here is the updated code for any interested:
void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Mission row = (Mission)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptActivity = (Repeater)item.FindControl("rptActivity");
var activity = _presenter.GetActivitiesByMission(row.Id);
var i = 0;
foreach (Activity data in activity)
{
RepeaterItem activityItem = rptActivity.Items[i];
var rptProject = (Repeater)activityItem.FindControl("rptProject");
var project = _presenter.GetProjectsByActivities(data.Id);
rptProject.DataSource = project;
rptProject.DataBind();
i++;
}
rptActivity.DataSource = activity;
rptActivity.DataBind();
}
}
I guess you are missing this line of code:
rptActivity .ItemDataBound += new RepeaterItemEventHandler(rptActivity_ItemDataBound);
It is easier to set the events in the asp file by the way...
Here is the deal,
I got a list of documents which is generated by the repeater and his databinding. In my markup i got a class for "li" tag when active or not.
What i just need is to set class="active" on the Li parent tag of my linkbutton when the document is selected.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="lnk" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' OnCommand="Get_carte"><%# Container.FindControl("lnk").ClientID %> <%# DataBinder.Eval(Container.DataItem, "Name") %> - <%# DataBinder.Eval(Container.DataItem, "id") %> - <%# DataBinder.Eval(Container.DataItem, "compteur") %></asp:LinkButton></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
I've finally found a solution for my issue.
#Kapil : Your code works but got issue with the reload of the aspx page, then i can't use this method.
#Aghilas : I based my solution on your code.
In fact i can't use ItemDataBound event because i bind only 1 time my repeater, then i use my OnCommand event on the linkbutton. Here is my aspx code:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Rpt_DataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li runat="server" ID="li">
<asp:LinkButton ID="lnk" class="linkButton" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' OnCommand="Get_carte"><%# Container.FindControl("lnk").ClientID %> <%# DataBinder.Eval(Container.DataItem, "Name") %> - <%# DataBinder.Eval(Container.DataItem, "id") %> - <%# DataBinder.Eval(Container.DataItem, "compteur") %></asp:LinkButton></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And my GetCarte Method:
protected void Get_carte(object sender, CommandEventArgs e)
{
LinkButton lnk = (LinkButton)sender;
ViewState["liactive"] = lnk.UniqueID.ToString().Substring(0, lnk.UniqueID.ToString().Length - 4);
lbl_carte.Text = lnk.UniqueID + " " + e.CommandArgument.ToString();
foreach (RepeaterItem rI in Repeater1.Items)
{
if (rI.ItemType == ListItemType.Item || rI.ItemType == ListItemType.AlternatingItem)
{
string liactiv = "";
if (ViewState["liactive"] != null)
liactiv = ViewState["liactive"].ToString();
var li = (HtmlControl)rI.FindControl("li");
if (li.UniqueID.ToString().Substring(0, li.UniqueID.ToString().Length - 3) == liactiv) //Adjust your condition
li.Attributes.Add("class", "active");
else
li.Attributes.Remove("class");
}
}
}
Thanks for your help.
PS: I put ID in ViewState in case of needing the value in another part of my code. I also can replace my substring by lastindexof method.
Code Behind - C# :
You can add runat="server" and id="" to your li, in order to modify in your code behind
void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var li = (HtmlControl)e.Item.FindControl("IdOfYourLI");
if(condition) //Adjust your condition
{
li.Attributes.Add("class", "active");
}
}
}
You can also modify with JavaScript :
You add to element id and use GetElementById method
youIdOfControlLI.className = 'active';
Using jQuery:
jsFiddle working link
Set class="linkButton" to you LinkButtons.
<asp:LinkButton ID="lnk" class="linkButton" />
Use the following jQuery Code to to set the Class on the li:
$(function () {
$(".linkButton").each(function (index) {
$(this).on("click", function () {
$(".linkButton").each(function (index) {
$(this).closest("li").removeClass("active");
});
$(this).closest("li").addClass("active");
});
});
});