How to enable href to appear with certain condition. ASP.NET C# - c#

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;

Related

How to access a legend tag from code

I have the following data-bound repeater code :
<%--categories--%>
<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<div class="groupbox">
<fieldset>
<legend><%# Container.DataItem %></legend>
<table>
<asp:Repeater ID="ItemRepeater" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox id="chkItem" runat="server" Text='<%# Eval("Text")%>' />
<asp:HiddenField id="pgNos" runat="server" Value='<%# Eval("PGNos")%>' />
<asp:Button ID="btnXRefs" Text="x-refs" runat="server" CssClass="xRefButton" OnClick="btnSelectXRefs_Click" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</fieldset>
</div>
</ItemTemplate>
</asp:Repeater>
There is a repeater inside a repeater. How do I access the text inside the legend (<legend><%# Container.DataItem %></legend>) from code?
I tried :
foreach (RepeaterItem cr in CategoryRepeater.Items)
{
string heading = (string) cr.DataItem; // returns null
}
Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list. For a Repeater which displays 10 rows of data, this is one row from the datasource...Basically, it's a specific row and at runtime you can get the Property Values from this row
I saw your above Mark Up...Seems like you are missing to mention the property of Data-Bound type Class like below.
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
There is a repeater inside a repeater. How do I access the text inside
the legend (<%# Container.DataItem %>) from code?
As told by phemt.latd, you can change the Legend tag into server side control like below.
<legend id="lg" runat="server">
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
</legend>
Now, in the Item-Bound Data Event, Find the Legend Control.
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlGenericControl ctl = (HtmlGenericControl)e.Item.FindControl("lg");
ctl.InnerText //This is what will give you the result.
}
}
The legend tag that you use is not visible on server side. Is a client control and not a server one.
Try with this:
<legend id="myLegend" runat="server"><%# Container.DataItem %></legend>
Then in codebehind:
protected void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem == null) return;
HtmlGenerics body = (HtmlGenerics)e.Item.FindControl("myLegend");
body.InnerText = "Foo";
}

Highlight (bold font) row in asp:Repeater with timer

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.

How to Change Visible Property of ASP.NET Control at RunTime in .NET 3.5

A Default.aspx page has some controls. Some controls visibility depends upon conditions. Here, what is tend to accomplish is change the visible property at runtime depending upon the conditional value.
Sampel Markup (Default.aspx in Static Mode)
<div id="DivBtnImgCopy" runat="server" Visible = "True">
<asp:ImageButton ID="BtnImgCopy" CssClass="image" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" OnClientClick="CopyImage(); SelectButton(this,true);return false;" />
</div>
What I tried is write a method in code behind file and tried to get value from that method to set visible property to true or false.
CodeBehindFile (Default.aspx.cs)
protected bool ShowHideButton()
{
bool bStatus = false;
try
{
if (sCondition == "false")
{
bStatus = false;
}
else if (sCondition == "true")
{
bStatus = true;
}
return bStatus;
}
catch { }
}
Sample Markup (Default.aspx in Dynamic Mode)
<div id="DivBtnImgCopy" runat="server" visible = "<% =ShowHideButton() %>">
<asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
runat="server" />
</div>
But, Getting below error:
Cannot create an object of type 'System.Boolean' from its string representation '<%=ShowHideButton() %>' for the 'Visible' property.
Any solution or work-around to accomplish this task.
Need Help.
Fastest way to do it is having your ShowHideButton return a bool instead of a string ; then :
<%
DivBtnImgCopy.Visible = ShowHideButton();
%>
<div id="DivBtnImgCopy" runat="server" >
<asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
runat="server" />
</div>
Cleanest way would be to include DivBtnImgCopy.Visible = ShowHideButton(); in the prerender event handler of your page
I am not sure of what visible does.
If you want not to render the div at all, you could wrap your in a <% if %> :
<% if(ShowHideButton()) { %>
<div id="DivBtnImgCopy" runat="server">
<asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
runat="server" />
</div>
<% } %>

Repeater - bind a button to a field

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

Using Directives

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.

Categories