hiding div element in code behind - c#

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.

Related

Get a value from Repeater in code-behind

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSourceTeklifler">
<ItemTemplate>
<div class="panel panel-primary">
<div class="panel-body">
<strong>Teklif Kodu:</strong> <%#Eval("TeklifId") %><br />
<strong>Teklif Tarihi:</strong> <%#Eval("TeklifTarih") %><br />
<strong>Teklifi Hazırlayan:</strong> <%#Eval("Name") %> <%#Eval("Surname") %><br />
<strong>Firma Adı:</strong> <%#Eval("FirmaAdi") %><br />
<strong>Sipariş:</strong> <%#Eval("FUrunId") %><br />
<strong>Teklif Tutarı:</strong> <%#Eval("TeklifTutar") %><br />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
As you can see I have a Repeater and it displays my data without a problem. I need to access TeklifIds in code-behind. I am going to make something like:
if(TeklifId == 1)
{
//do something
}
else if(TeklifId == 2)
{
//do something else
}
And to do this, I need to get all TeklifId while it is adding to the Repeater.
Ideally you should include the data withing some ASP.NET controls like Label, Textbox control within ItemTemplate tag because it is easy to work with them. But I am not sure why you are adding the normal html tags directly.
Anyways, to find the value you will have to find it within the ItemDataBound control of repeater control but for that you will have to make the strong tag a server control by adding runat="server" attrribute like this:-
<strong id="TeklifId" runat="server">Teklif Kodu:</strong> <%#Eval("TeklifId") %>
Then, add the ItemDataBound event in your repeatre control like this:-
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"
Finally in the code behind you can find the value like this:-
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
HtmlGenericControl TeklifId = e.Item.FindControl("TeklifId") as HtmlGenericControl;
string TeklifId = TeklifId.InnerText; //value here
}
}
Place TeklifId in a Label control so that you can use ID and FindControl to get the values like this:
<asp:Label ID="TeklifId" runat="server" Text='<%#Eval("TeklifId") %>'></asp:Label>
And then:
foreach (RepeaterItem item in Repeater1.Items)
{
var TeklifId = (Label)item.FindControl("TeklifId");
if (TeklifId == 1)
{
//do something
}
}
Repeater Code :
<td>
<span runat="server" id="lbBranchname" style="font-style:italic;"><%# Eval("branchname")%></span>
</td>
Code behind : rptBranch_ItemCommand
HtmlGenericControl lbBranchname = e.Item.FindControl("lbBranchname") as HtmlGenericControl;
BranchName = lbBranchname.InnerText;

must have an IF in my Repeater

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

Unable to add Style to last item of Repeater

I am using Bootstrap's Progress Bar inside a repeater to show score of different items. The aspx is like this:
<asp:Repeater ID="rptFinalScore" runat="server" OnItemDataBound="rptFinalScore_ItemDataBound">
<ItemTemplate>
<div class="row">
<div class="col-sm-2">
<asp:Label ID="rpt_Score" runat="server" Text='<%#Eval("TotalScore") %>'>></asp:Label>
</div>
<div class="col-sm-10">
<div class="progress">
<div id="rpt_proBar" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" runat="server">
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
and the codebehind is:
protected void rptFinalScore_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (RepeaterItem item in rptFinalScore.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var Score = item.FindControl("rpt_Score") as Label;
var ProgBar = item.FindControl("rpt_proBar") as HtmlGenericControl;
string BuildingScore = ((Label)Score).Text;
ProgBar.Attributes.Add("style", string.Format("width:{0}%;", BuildingScore));
}
}
}
The problem I am facing is that if suppose there are 5 items in the repeater then it will correctly apply the style="width:x%" to first 4 items but not for the last item. The output is like this. Can someone please help me out with this.
Why are you looping in each ItemDataBound event handler call ? You have to set styles for the corresponding item that's stored in e argument. Last item's style is not set because repeater doesn't have that item in its Items collection when ItemDataBound event is fired.
You have to change your ItemDataBound handler to something like this:
protected void rptFinalScore_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var Score = e.Item.FindControl("rpt_Score") as Label;
var ProgBar = e.Item.FindControl("rpt_proBar") as HtmlGenericControl;
string BuildingScore = ((Label)Score).Text;
ProgBar.Attributes.Add("style", string.Format("width:{0}%;", BuildingScore));
}
}

Nested repeater not being triggered

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...

Updatepanel issue with client side "class" changing of a control inside repeater

I'm having trouble with using UpdatePanel and changing the 'class' attribute of a control inside a repeater by javascript.
Here some code:
--on the aspx--
<script type="text/javascript">
function changeClass(ctl) {
if (ctl.className == "marked") {
ctl.className = "unmarked";
} else {
ctl.className = "marked";
}
}
</script>
<!-- some html -->
<asp:UpdatePanel ID="upp" runat="server">
<ContentTemplate>
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<a id="aButton" runat="server" href="javascript:void(0)">
<!-- some other controls -->
</a>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
--Codebehind--
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
MyClass obj = (MyClass)e.Item.DataItem;
((HtmlAnchor)e.Item.FindControl("aButton")).Attributes.Add("class", "marked");
//some other code....
}
}
//method called after the bind on 'rpt1'
private void mymethod()
{
foreach (RepeaterItem ri in rpt1.Items)
{
HtmlAnchor aButton = (HtmlAnchor)ri.FindControl("aButton");
if (Must-be-unmarked)
aButton.Attributes.Add("class", "unmarked");
aButton.Attributes.Add("OnClick", "changeClass(this);");
}
}
The problem is, when I click on an "aButton" the class is changed normally, but when I come in codebehind and get de 'class' of control to check if it's marked or unmarked, I only get the controls marked in ItemDataBound of repeater, not the "aButton"s marked by me at execution time.
here is what I do to get the "aButton"s marked:
private void checkMarked()
{
foreach (RepeaterItem ri in rpt1.Items)
{
if (((HtmlAnchor)ri.FindControl("aButton")).Attributes["class"] == "marked")
{
//do something...
}
}
}
When you change class property from client-side code, the server side will not know about it.
You'll need to add a hidden <input> with marked/unmakred so you can check the contents from the server on a post-back.
Another approach would be to sipmly have your javscript postback to the server directly when an item changes from marked/unmakred.

Categories

Resources