If I have a repeater going through a list of data to retieve the values I use
<%# Eval("current_item") %>
And with a bit of luck it outputs the information for "current_item"
However if I want to check if "current_item" equals 1 for instance, this will tell me whether to print the next lot of information. So how do I use that information to determine the output, effectivily I want to put that information into an variable.
<%# myInt = int.Parse(Eval("current_item")) %>
The above code is what I want to do really.
Then I will do something like this:
<% if (myInt == 1) { %>
<p>Information to display if myInt = 1</p>
<% } else { %>
<p>Other Information</p>
<% } %>
Any Ideas?
I have used something similar to the following in a GridView:
<p>
<asp:Label Visible='<%# ((int)Eval("current_item") == 1) %>' Text="This is the first item" runat="server" />
<asp:Label Visible='<%# ((int)Eval("current_item") != 1) %>' Text="This is not the first item" runat="server" />
</p>
Give that a shot and see if it works for a Repeater too.
Related
I'm trying to enable users to edit their respective comments they have made on my project. What I would like to do is match the current user in session with the eval value of the user that has made the respective comment. Below is what I have done so far:
<% if (Session["user"] != null && Session["user"] == Eval("first_name"))
{
%>
Edit
<%
}
else
{
}
%>
However, it gives an error:
'Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control.'
Here's the full code:
<asp:Repeater ID="r2" runat="server" OnItemCommand="r2_ItemCommand">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<% if (Session["user"] != null)
{
%>
<br />
<asp:Hyperlink runat="server" ID="myLink" Text="Edit" Visible="false"></asp:Hyperlink>
<%
}
else
{
}
%>
<br /> <%#Eval("title") %><br />
By <%#Eval("first_name") %> <%#Eval("last_name") %> on <%#Eval("date") %><br />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater><br />
Any ideas on how to fix this or any better solutions that can be done?
Eval only works when you're setting a bindable property on a server control.
To do something like this, you could either have a server control container, or make an asp Hyperlink control.
HTML
<asp:Hyperlink runat="server" ID="myLink" Text="edit" Visible="false"></asp:Hyperlink>
C#
if (Session["user"] != null && Session["user"] == myObject.first_name))
myLink.Visible = true;
I have a nested repeater control and I am trying to list out the items underneath that nested repeater over a few spaces including the check box associated with it. But it seems like every other line is a checkbox without a label, can someone shed a little light?
<asp:Repeater ID="parentRepeater" runat="server">
<ItemTemplate>
<br />
<b>
<input type="checkbox" id="chk_ParentProgram" name="chk_ParentProgram" runat="server"
value='<%# ((programsRepeat)Container.DataItem).Level == 1 ? ((programsRepeat)Container.DataItem).ProgramID.ToString() : "" %> '
/>
<label for="chk_ParentProgram">
<%# ((programsRepeat)Container.DataItem).Level == 1 ? ((programsRepeat)Container.DataItem).ProgramName : "" %>
</label>
</b>
<asp:Repeater>
<ItemTemplate>
<br/>
<input type="checkbox" id="chk_ChildProgram" name="chk_Child" runat="server"
value='<%# ((programsRepeat)Container.DataItem).Level == 2 ? ((programsRepeat)Container.DataItem).ProgramID.ToString() + " from the child repeater" : "" %>'
/>
<label for="chk_ChildProgram">
<%# ((programsRepeat)Container.DataItem).Level == 2 ? ((programsRepeat)Container.DataItem).ProgramName : "" %>
</label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
The problem is whether you have a valid value or empty sting for (programsRepeat)Container.DataItem).ProgramID.ToString() you still rendering the checkbox like below
<input type="checkbox" value="" />
This will render the empty checkbox.
Better check the value and render the checkbox like
<% if((programsRepeat)Container.DataItem).Level == 2 &&
((programsRepeat)Container.DataItem).ProgramID!="")
{
//not sure about the inline syntax here.
//add checkbox code
}
%>
Inline Syntax Reference or check this
I have a list of activities that is displayed with a Repeater. I already use Timers to display these, so the Timer isn't the problem. It's the code that i have to put into the Timer_tick method.
The "highlight-Timer" is supposed to highlight the items/rows one at a time, and then I want to display some info related to the highlighted row.
If it's easier with another control that's no problem. I doesn't have to be a Repeater. I just use it because of the styling-possibilities (it isn't displayed in just one line, but with several line-breaks etc.)
As requested:
(Repeater)
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div id="divActivity" runat="server">
<span style="font-size:30px;">
<%# DataBinder.Eval(Container.DataItem, "act_headline") %>
</span>
<br />
<img alt="mapIcon" src="../img/mapIcon.gif" height="15px" width="15px" style="position:absolute;" />
<span style="font-size:12px; font-style:italic; margin-left:23px;">
<%# DataBinder.Eval(Container.DataItem, "act_place") %>
</span>
<img alt="watchIcon" src="../img/watchIcon.png" height="15px" width="15px" style="position:absolute;" />
<span style="font-size:12px; font-style:italic; margin-left:23px;">
<%# DataBinder.Eval(Container.DataItem, "act_start") %> - <%# DataBinder.Eval(Container.DataItem, "act_end") %>
</span>
<br />
<div style="word-wrap: break-word; width:1000px; margin-top:20px; padding:0;">
<%# DataBinder.Eval(Container.DataItem, "act_text") %>
</div>
<br />
<img alt="infoIcon" src="../img/infoIcon.png" height="15px" width="15px" style="position:absolute;" />
<span style="font-size:12px; margin-left:23px;"><a target="_blank" href="http://<%# DataBinder.Eval(Container.DataItem, "act_info") %>"><%# DataBinder.Eval(Container.DataItem, "act_info") %></a>
</span>
</div>
</ItemTemplate>
</asp:Repeater>
I don't have anything in the the Timer_tick-event, as i've tried several things and the deleted it after it failed. Hope it's enough.
I would set a special CSS class to the highlighted item by using something like this in the tick event handler by looping over the Repeater.Items collection.
Something like this (untested):
int currentHighlight = -1;
int nextHighlight = -1;
for (int i = 0; i < Repeater1.Items.Count; i++)
{
HtmlControl htmlDiv = (HtmlControl)Repeater1.Controls[i].Controls[1];
if (htmlDiv.Attributes["class"] != null)
{
if (htmlDiv.Attributes["class"].Contains("highlighted")) // this is the currently highlighted item
{
// record currently highlighted item index
currentHighlight = i;
// remove highlight
htmlDiv.Attributes["class"].Replace("highlighted", "");
htmlDiv.Attributes["class"].Trim();
}
}
}
if ((currentHighlight == -1) || (currentHighlight == Repeater1.Items.Count))
{
// handle first Item highlighting
nextHighlight = 1;
}
else
{
// handle standard case
nextHighlight = currentHighlight + 1;
}
// highlight next item
((HtmlControl)Repeater1.Controls[nextHighlight].Controls[1]).Attributes["class"] += " highlighted";
Then you can use the highlighted CSS class to handle styling (the bold font you mention) as well as some special behavior in JavaScript or jQuery for example.
BTW, you can do any other operation on the RepeaterItem object once you get which one is currently highlighted.
I am displaying 3 jobs by a repeater. Each job has a JOB application ID. In my Data access layer you can delete a job in the database by the ID. So, in any class i can call and delete an entry.
JobOpeningDAL app = new JobOpeningDAL();
app.RemoveJobOpening(64);
I would like to generate a button for each of the 3+ jobs.
i would like to put this code in the button and not hard code the ID.
Trick is each button would need to get that jobs ID.
Is this possible?
Is there a way to bind a different button to each repeater item based on a item like: JOB ID? That way when the user clicks the delete button underneath a job it automatically deletes it.
Here is my repeater code
<br />
Job ID
<%# DataBinder.Eval(Container.DataItem, "JOBAPPLICATIONID")%>
<br />
Job Description:
<%# DataBinder.Eval(Container.DataItem, "JobDescription") %>
<br />
Wage:
<%# DataBinder.Eval(Container.DataItem, "Wage") %>
<br />
Department ID:
<%# DataBinder.Eval(Container.DataItem, "DepartmentID") %>
<br />
Has been filled:
<%# DataBinder.Eval(Container.DataItem, "HasBeenFilled") %>
<br />
Location:
<%# DataBinder.Eval(Container.DataItem, "Location") %>
<br />
Job Name:
<%# DataBinder.Eval(Container.DataItem, "JobName") %>
<br />
if I understand what you are trying to do you could try the following:
add a button to your repeater like this:
<asp:Button ID="yourButton" runat="server" CommandName="delete" CommandArgument='<%# Eval("id")' />
then in your repeater's itemcommand handler, you can do the following:
protected void YourRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
object idToDelete = e.CommandArgument;
int ID = Convert.ToInt32(idToDelete );
//do stuff with the ID here
}
}
I'm trying to create a pager in ASP.net and am running into an error.
Trying to do this in my markup:
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) { %>
<% if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"><%= pageNumber %></asp:LinkButton>
<% } %>
<% } %>
I am getting the following error:
Compiler Error Message: CS0103: The name 'pageNumber' does not exist in the current context
The error is happening on the LinkButton line. It works fine on the first if case...but for some reason my variable does not exist on the else case.
Does anyone have any idea why this is not compiling or how I could do the same thing differently.
It's been a while since I have done regular ASP.net and I am used to the MVC way.
You can't place <%= pageNumber %> inside a LinkButton.
Alternatively, use the Repeater control and the OnItemDataBound event to add logic such as pageNumber == this.PageIndex in the code behind.
<asp:Repeater ID="Pager" runat="server">
<ItemTemplate>
<asp:Literal ID="ltlGoToPage" runat="server" Visible="false"></asp:Literal>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
You can toggle the visibilty of the controls in the OnItemDataBound event.
In the code behind, reference the controls and apply any logic:
var ltlGoToPage = (Literal)e.Item.FindControl("ltlGoToPage");
var lnkGoToPage = (Literal)e.Item.FindControl("lnkGoToPage");
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) {
if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber.ToString() %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"><%= pageNumber.ToString() %></asp:LinkButton>
<% }
} %>
This will help eliminate some of the markup. Also, called ToString() on page number as that's just common practice for me, but you might not want it that way.
This is not how you do things in asp.net web forms, you should do the loop in the code behind.
The error is caused by the fact that the problem <%= pageNumber %> is in the middle of a control.
Sorry about my previos post. I wanted to say the way I see it is to use Text property of the control.
This should work:
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) { %>
<% if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"></asp:LinkButton>
<%
lnkGoToPage.Text = pageNumber.ToString();
} %>
<% } %>
Also, you should think about giving sdifferent IDs to the LinkButtons to make sure they are different. But, conceptually, the code above should work