I know this question has been answered a few times but it just hasn't been the way I've need it.
Basically I have this code:
<Engine:WidgetSQLDataSource ID="DS_Hotel" runat="server" SelectCommand="site.GetHotelList" />
<asp:Repeater ID="rp_GuestHotelInfo" runat="server" DataSourceID="DS_Hotel">
<ItemTemplate>
<% if (Convert.ToInt32(Eval("TreeID")) == PrimaryNavigation1.ParentID) { %>
<img width="226" height="68" src="<%# Eval("Logo") %>" alt="<%# Eval("HotelName") %>" />
<% } %>
</ItemTemplate>
</asp:Repeater>
What I am trying to do is change the logo based on the currents pages parent id.
However I'm getting an error saying that:
Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control.
Any ideas how I can tackle this issue?
Thanks,
T.J.
Repeater, in webform at least, does not support the "if" statement, instead you can use the "ternary" operator, for example: string src = (a==b) ? "equal":"diferent";
So your code may be replaced by this:
<ItemTemplate>
<a href="#" class="logo">
<img width="226" height="68"
src='<%# (Convert.ToInt32(Eval("TreeID")) == PrimaryNavigation1.ParentID) ? Eval("Logo") : "" %>'
alt="<%# Eval("HotelName") %>"
/>
</a>
</ItemTemplate>
Related
I have been trying to get a progressBar into my GridView for a while now. Unfortunately without success. I currently have the following:
<asp:GridView ID="gvShow" runat="server" AutoGenerateColumns="False" DataKeyNames="Progress" Width="100%">
<Columns>
<asp:BoundField DataField="Progress" HeaderText="Progress" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Literal ID="lbProgress" runat="server" Text="<div class='progress'><div class='progress-bar' role='progressbar' aria-valuenow='<%#Eval("Progress") %>' aria-valuemin='0' aria-valuemax='100' style='width: 60%;'><span class='sr-only'>60% Complete</span></div></div>"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
which results in a parser error (The server tag is not well formed.). If I replace the eval with any value it works without any issues. I think I am missing something here but I really can't find it.
For some reason all of your markup is as text of Literal control. So you have the problem with quotes, as you need double quotes for Text="" and of Eval.
Personally I see no reason to use Literal here, you can just use the markup as is, and the problem will go away:
<ItemTemplate>
<div class='progress'>
<div class='progress-bar' role='progressbar' aria-valuenow='<%#Eval("Progress") %>' aria-valuemin='0' aria-valuemax='100' style='width: 60%;'>
<span class='sr-only'>60% Complete</span>
</div>
</div>
</ItemTemplate>
You have to use Eval method instead of Text Method.
<ItemTemplate>
<div class='progress'>
<div class='progress-bar' role='progressbar' aria-valuenow='<%#Eval("Progress") %>' aria-valuemin='0' aria-valuemax='100' style='width: 60%;'>
<span class='sr-only'>60% Complete</span>
</div>
</div>
I have a gridview. its data source is a datatable that is loaded from the database. In this gridview, i have a template column.
<asp:TemplateField HeaderText="Product Type" SortExpression="ProductID">
<ItemStyle CssClass="MP-table-tb-display-item" />
<ItemTemplate>
<div class="MP-table-tb-display-main">
<asp:LinkButton ID="lnkview" CommandArgument='<%# Eval("ProductID") %>' CommandName="Viewproduct"
runat="server" CausesValidation="False" OnClick="lnkview_Click"><h4>
<%# Eval("Name") %>
</h4>
</asp:LinkButton>
</div>
<br />
<div class="MP-table-tb-display">
<p>
<span>KEY</span><%# Eval("[product_type_key]") %></p>
<br />
<a target="_blank" href='<%# Eval("SourceURL") %>'>Source</a>
</div>
</ItemTemplate>
</asp:TemplateField>
In this I want Source hyperlink only show when data available into <%# Eval("SourceURL") %>. If I am not able to get the SourceURL value into RowDatabound Event . Please Guide me.
I plan for this too but this is not working properly.
<a target="_blank" href=' <%= Eval("SourceURL")!=null ? Eval("SourceURL") : "style='display: none'" %> />'> Source</a>
use this instead
<asp:hyperlink Target="_blank" NavigateUrl='<%# Eval("SourceURL") %>' Visible = '<%# Eval("SourceURL") == null ? false : true %>' >
Similarly you could use the <a> tag to control its visiblity. The if condition would go in Style attribue and not in href attribute. Something like this
Style=display:Eval('some_val') == null ? none : block
Try this :
<a target="_blank" href='<%#(String.IsNullOrEmpty(Eval("SourceURL").ToString()) ? "" : Eval("SourceURL"))'%> style='display: none'>Source</a>
Thanks
I need to get a label during runtime so that i can compare to the Session["userName"] and choose to show buttons after the comparison.
I have look around and this seems to be the solution but i've run onto trouble as i only get null during runtime.
UserName:
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
<br />
So, this is the label inside a "ListView1" i need what Eval finds during each iteration of pumping out of the attached database.
<% Label lbl = (Label) ListView1.FindControl("userName");
string userName = lbl.Text;
if (Session["userName"].ToString() == userName)
{%>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<% }%>
and this is my attempt at retrieving the label.Text. i know it is something similar to this but i might be doing it wrong... well i am doing it wrong.
Thanks you very much for any information you can provide as i have just spent about 10 hours changing the ways i could achieve this result.
I would do something like:
Visible='<%# Session["userName"].ToString() == Eval("UserName").ToString() %>'
on each of the Button controls.
In my project the master page contains a repeater that's used as a menu with an Xml file as the data source for the repeater.
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate><div id="navmenu"><ul></HeaderTemplate>
<FooterTemplate>|</ul></div></FooterTemplate>
<ItemTemplate>
|<li>
<a href="<%# DataBinder.Eval(Container.DataItem, "url")%>"
class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
urls in the xml file is as
<menuitems>
<item id="1" url="Employee.aspx" title="Employee" description="Employee" />
<item id="2" url="Location.aspx" title="Location" description="Location" />
</menuitems>
Here I want to change the CSS style of the current page in the menu.
One solution you can opt for is to handle the ItemCreated event of the <asp:Repeater> control. To do this you need to add an event handler:
In the .master markup:
<asp:Repeater ID="Admin_menus" runat="server" OnItemCreated="Admin_menus_ItemCreated">
<HeaderTemplate>
<div id="navmenu">
<ul>
</HeaderTemplate>
<FooterTemplate>
|</ul></div></FooterTemplate>
<ItemTemplate>
|<li runat="server" id="hyperlink"><a href="<%# DataBinder.Eval(Container.DataItem, "url")%>" class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%></strong> </a></li>
</ItemTemplate>
</asp:Repeater>
In the .master.cs codebehind:
protected void Admin_menus_ItemCreated(object sender, RepeaterItemEventArgs e)
{
// Ensure that the ItemCreated is not null, the first one (header?) gets
// returned null
if (e.Item.DataItem != null)
{
// Extract the "url" attribute from the Xml that's being used for
// databinding for this particular row, via casting it down to
// IXPathNavigable as the concrete type of e.Item.DataItem isn't available
// to us.
var currentUrl = ((IXPathNavigable)e.Item.DataItem).CreateNavigator().GetAttribute("url", "");
if (Request.Url.PathAndQuery.Contains(currentUrl))
{
// This just adds a background color of "red" to the selected
// menu item. What you actually do is entirely up to you
var hyperlink = (HtmlGenericControl) e.Item.FindControl("hyperlink");
hyperlink.Style.Add(HtmlTextWriterStyle.BackgroundColor, "red");
}
}
}
Note that I've added a runat="server" as well as an id="hyperlink" to the <li> tag in your ItemTemplate so that the code in the ItemCreated handler can find it easily to style it.
Perhaps one solution is to check the current page in your inline Eval code and add the "currentpage" class to the anchor
For simplicity I'm using Eval() instead of DataBinder.Eval()
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate>
<div id="navmenu"><ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# Eval("url") %>' class='link6<%# Request.RawUrl.EndsWith(Eval("url").ToString()) ? " currentpage" : "" %>' id='<%# Eval("id")%>'><strong><%# Eval("title")%></strong></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></div>
</FooterTemplate>
</asp:Repeater>
Instead of binding directly from your xml data to repeater, try to get the repeater databing event, and compare with the current page url and the each binding item to determine if the current item should be hi-lighted
<asp:Repeater> is driving me mad..
I need to do
<ItemTemplate>
<% if (Container.DataItem("property") == "test") {%>
I show this HTML
<% } else { %>
I show this other HTML
<% } %>
</ItemTemplate>
But I can't for the life of me find any way to make that happen. Ternary isnt any good, because the amount of HTML is quite large, setting labels via a DataBind event is not very good either, as I'd have to have large blocks of HTML in the code-behind.
Surely there's a way to do this....
You could use server side visibility:
<ItemTemplate>
<div runat="server" visible='<% (Container.DataItem("property") == "test") %>'>
I show this HTML
</div>
<div runat="server" visible='<% (Container.DataItem("property") != "test") %>'>
I show this other HTML
</div>
</ItemTemplate>
You could try creating a kind of ViewModel class, do the decision on your code-behind, and then be happy with your repeater, simply displaying the data that it is being given.
This is a way to separate logic from UI. You can then have a dumb-UI that simply displays data, without having to decide on what/how to show.
You could do this with user controls:
<ItemTemplate>
<uc:Content1 runat='server' id='content1' visible='<%# Container.DataItem("property") == "test" %>'/>
<uc:Content2 runat='server' id='content2' visible='<%# Container.DataItem("property") != "test" %>'/>
</ItemTemplate>
Looks like I got this mixed up with the actual databinding
You can do it like so:
<asp:Repeater runat="server">
<ItemTemplate>
<% if (((Product)Container.DataItem).Enabled) { %>
buy it now!
<% } else {%>
come back later!
<% } %>
</ItemTemplate>
</asp:Repeater>
I had a similar problem and stumbled across this page. Thanks for the great answers, Gavin and Keltex got me on the right track but I had a bit of a tricky time getting this to work on my page. Ultimately I was able to get it to work with this boolean, so I wanted to share for posterity:
Show Checkbox if false
<asp:CheckBox ID="chk_FollowUp" Visible='<%# (DataBinder.Eval(Container.DataItem, "FollowUp").ToString() == "False") %>' runat="server" />
Show Flag img if true
<asp:Image ID="img_FollowUp" AlternateText="Flagged" ImageUrl="Images/flag.gif" runat="server"
Visible='<%# DataBinder.Eval(Container.DataItem, "FollowUp") %>' Height="30" Width="30" />
First You Have to Defind a Count variable in your Page.cs file
<%if (Count == 0)
{
%>
<div style="background-color:#cfe9ed" class="wid_100 left special_text"><%# Eval("CompanyName") %></div>
<%}
else if (Count == TotalCount - 1)
{
%>
<div style="background-color:#f2f1aa" class="wid_100 left special_text"><%# Eval("CompanyName") %></div>
<%}
else
{
%>
<div class="wid_100 left special_text"><%# Eval("CompanyName") %></div><% } %>
<%Count++; %>