Anchor Link in DataGrid ItemTemplate - asp.net

I have the following markup in a DataGrid:
<itemtemplate>
<a href='~/File.aspx?item=<%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>'
runat='server'><%# DataBinder.Eval(Container.DataItem, "Title").ToString() %>
</a>
</itemtemplate>
But it's rendering the following HTML:
<a href="../File.aspx?item=<%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>">
My Link
</a>
What am I doing wrong?

Try this.
<asp:TemplateColumn>
<ItemTemplate>
<a id="A1" runat='server' href='<%# "~/File.aspx?item=" + Eval("ID")%>'>
<%# Eval("Job") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>

You have to append it
<a href='~/File.aspx?item=' + <%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>
Otherwise if you use hyperlink, It will be very easy. e.g.
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%#Eval("ItemID", "~/File.aspx?item={0}")%>'
Text='<%# DataBinder.Eval(Container.DataItem, "Title").ToString() %>'></asp:HyperLink>
</ItemTemplate>

Related

Use the 'If condition' in Item Template [ASP]

I have two elements; <img> and <video> in an ItemTemplate, I want to test the value <% # Eval ("Url")%> to choose an element to display.
<itemtemplate>
<li>
<video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
<source src='<%# Eval("Url") %>' type='video/mp4'>
</video>
</li>
<li>
<img src="<%# Eval("url") %>">
</li>
</itemtemplate>
You can use a ternary operator in the template and based on that outcome show/hide the correct element.
<ItemTemplate>
<li>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Eval("SomeValue").ToString() == "video" ? true : false %>'>
<video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
<source src='<%# Eval("Url") %>' type='video/mp4'>
</video>
</asp:PlaceHolder>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Url") %>' Visible='<%# Eval("SomeValue").ToString() == "video" ? false : true %>' />
</li>
</ItemTemplate>

hide repeater href when databiner.eval is null

I have a repeater column like this
<asp:Label id="lbl1" runat="server"><a href='mailto:<%# DataBinder.Eval(Container.DataItem, "EMAIL1_GPD") %>'
target='_blank'> <%# DataBinder.Eval(Container.DataItem, "FIRSTNAME1_GPD") %>
<%# DataBinder.Eval(Container.DataItem, "LASTNAME1_GPD") %></a> </asp:Label>
<br/>
<asp:Label ID="lblPhone" runat="server" Text='<%# Eval("PHONE1_GPD") %>' Visible='<%# Eval("PHONE1_GPD") == null ? false: true %>'
ForeColor="#000000" Font-Size="Small"></asp:Label>
<br/>
<asp:Label id="Label12" runat="server"><a href='mailto:<%# DataBinder.Eval(Container.DataItem, "EMAIL2_GPD") %>'
target='_blank'><%# DataBinder.Eval(Container.DataItem, "FIRSTNAME2_GPD")%>
<%# DataBinder.Eval(Container.DataItem, "LASTNAME2_GPD")%></a> </asp:Label>
<br/>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PHONE2_GPD") %>' Visible='<%# Eval("PHONE2_GPD") == null ? false: true %>'
ForeColor="#000000" Font-Size="Small"></asp:Label>
<br/>
<asp:Label id="Label13" runat="server"><a href='mailto:<%# DataBinder.Eval(Container.DataItem, "EMAIL3_GPD") %>'
target='_blank'><%# DataBinder.Eval(Container.DataItem, "FIRSTNAME3_GPD")%>
<%# DataBinder.Eval(Container.DataItem, "LASTNAME3_GPD")%></a> </asp:Label>
<br/>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("PHONE3_GPD") %>' Visible='<%# Eval("PHONE3_GPD") == null ? false: true %>'
ForeColor="#000000" Font-Size="Small"></asp:Label>
</td>
I would like to hide the labels when dataBinder.Eval is empty. I have done it for the labels where it displays phone number. I am stuck with hiding the hrefs. Though it does not display anything when the data is null. The page on the browser does not display properly.
Set visibility of the label like Visible='<%# String.IsNullOrEmpty(Eval("EMAIL3_GPD")) ? false : true %>'. Your label should look like:
<asp:Label id="Label13" runat="server" Visible='<%# String.IsNullOrEmpty(Eval("EMAIL3_GPD")) ? false : true%>'><a href='mailto:<%# DataBinder.Eval(Container.DataItem, "EMAIL3_GPD") %>'
target='_blank'><%# DataBinder.Eval(Container.DataItem, "FIRSTNAME3_GPD")%>
<%# DataBinder.Eval(Container.DataItem, "LASTNAME3_GPD")%></a> </asp:Label>

How to find control bounded in item template on master page from content page?

I am having a nested repeater on the master page. And a hiddenfield in its item template.
i want the value of hiddenfield on content page.Like this
<ul class="categories">
<li>
<div id='cssmenu'>
<h4>Categories</h4>
<asp:Repeater ID="repcategory" runat="server" OnItemDataBound="repcategory_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf1" Value='<%# Eval("CategoryID") %>' runat="server" />
<li class="active has-sub">
<a href='#'><span>
<%#Eval("CategoryName") %></span></a>
<asp:Repeater ID="repsubcategory" OnItemDataBound="repsubcategory_ItemDataBound" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf2" Value='<%# Eval("SubCategoryID") %>' runat="server" />
<li class="has-sub">
<a href='#'><span>
<%#Eval("SubCategoryName") %></span></a>
<asp:Repeater ID="repsubcategory2" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf3" Value='<%# Eval("SubCategory2ID") %>' runat="server" />
<li>
<a href="ClientProductSubCategory2.aspx"><span>
<%#Eval("SubCategory2Name") %></span></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</div>
I want the value of subcategory2id on the content page and codded like this.
HiddenField hiddensubcategory2id = (HiddenField)Master.FindControl("hf3");
DataSet ds = new ClientProductView().GetAllProductSubCategory2(hiddensubcategory2id.Value);
repContent.DataSource = ds;
repContent.DataBind();
But this is returning a null value. Please help me to solve this problem
Try finding the control within the repeater object itself, like this:
var hiddensubcategory2id = repsubcategory2.FindControl("hf3") as HiddenField;
Note: You should always check to see if the result of a FindControl() is null or not, like this:
if(hiddensubcategory2id != null)
{
// Do something with the control you found
}

General idea how to highlight the selected Parent item in a Menu (using ListView)

So I have a Hierarchical Datasource that contains the information for a menu... I databind it to a ListView and use CSS to format it into a menu.. everything is good.. Now the next step I can't seem to understand..
How do I highlight (set the class of the ) the menu item that was just selected.. They are just links .. Should I somehow identify the ID of what was clicked and then use that to somehow loop through..?
Here is what I got so far so any tips in the right direction are good.
<asp:ListView ID="lvMainOuter" runat="server">
<LayoutTemplate>
<ul id="menunav" runat="server">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li <%# (Container.DataItem as FigLeafMenuItem).ItemSelected == "true" ? #" class=""MainMenuSelected""" : #" class=""""" %>>
<asp:HyperLink ID="nodeLink" runat="server" Text='<%# (Container.DataItem as FigLeafMenuItem).Name %>' NavigateUrl='<%# (Container.DataItem as FigLeafMenuItem).Url %>' />
<asp:ListView ID="lvMainInner" DataSource="<%# (Container.DataItem as FigLeafMenuItem).Children %>" runat="server">
<LayoutTemplate>
<ul id="menunav" runat="server">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="nodeLink" runat="server" Text='<%# (Container.DataItem as FigLeafMenuItem).Name %>' NavigateUrl='<%# (Container.DataItem as FigLeafMenuItem).Url %>' />
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
public int SelectedMenuId { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lvMainOuter.ItemDataBound += lvMainOuter_ItemDataBound;
lvMainOuter.DataSource = new FigLeafMenu().GetFigLeafTopMenu();
lvMainOuter.DataBind();
}
void lvMainOuter_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
//WHAT DO I DO NOW TO GET THE SELECTED ITEM AND ASSIGN IT A CLASS?
}
}
Try this
<asp:ListView ID="lvMainInner" DataSource="<%# (Container.DataItem as FigLeafMenuItem).Children %>"
runat="server">
<LayoutTemplate>
<ul id="menunav" runat="server">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="nodeLink" runat="server" Text='<%# (Container.DataItem as FigLeafMenuItem).Name %>'
NavigateUrl='<%# (Container.DataItem as FigLeafMenuItem).Url %>' />
</li>
</ItemTemplate>
<SelectedItemTemplate>
<li>
<asp:HyperLink ID="nodeLink" CssClass="MyHighlightClass" runat="server" Text='<%# (Container.DataItem as FigLeafMenuItem).Name %>'
NavigateUrl='<%# (Container.DataItem as FigLeafMenuItem).Url %>' />
</li>
</SelectedItemTemplate>
</asp:ListView>
or you could use codebehind ...
lvMainInner.SelectedIndexChanged += (o, args) =>
{
//You can do whatever here
};
In your hyperlink markup you can set the CssClass to a conditional statement based on some statement right?

Accessing SiteMapNode Container.DataItem from outside of Parent Repeater

I am trying to access the current 'active' top level node from a sitemap repeater from outside of the ASP.NET repeater used to render it, this is for CSS styling purposes as I want to place the child nodes on the subsequent row with different styling horizontally. At present I have the following code which I can't get to display correctly using CSS.
<asp:SiteMapDataSource ID="topNavLevel" runat="server" ShowStartingNode="false" />
<asp:Repeater runat="server" ID="rptParent" DataSourceID="topNavLevel">
<HeaderTemplate><ul id="lawMenu" class="topMenu"></HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" ID="parentLink" NavigateUrl='<%# Eval("Url") %>'><span class="t"><%# Eval("Title") %></span><span class="l"></span><span class="r"></span></asp:HyperLink>
<asp:Repeater ID="rptChild" runat="server" DataSource='<%# ((SiteMapNode) Container.DataItem).ChildNodes %>'>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="childLink" runat="server" NavigateUrl='<%# Eval("Url") %>'><span class="t"><%# Eval("Title") %></span><span class="l"></span><span class="r"></span></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></FooterTemplate>
</asp:Repeater>
I would like to display the child nodes on the next light blue element which I can do perfectly well from a seperate div if this was not rendered using a child repeater. In the image below Blog and Services are top level nodes and their subseqent nodes (2 for each) should be displayed on the light-blue row below. Is this possible?
To get the parent repeaters DataItem as if you weren't inside your child repeater:
<%# DataBinder.Eval(((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Parent).DataItem, "Property") %>
I have solved this now. For anyone else coming across this post here's the solution:
<asp:SiteMapDataSource ID="topNavLevel" runat="server" ShowStartingNode="false" />
<asp:Repeater runat="server" ID="rptParent" DataSourceID="topNavLevel">
<HeaderTemplate>
<ul id="lawmenu" class="law-menu">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" ID="parentLink" NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
<div class="law-nav_nav2">
<asp:SiteMapDataSource ID="secondNavLevel" runat="server" ShowStartingNode="false" StartingNodeOffset="1" />
<asp:Repeater ID="rptChild" runat="server" DataSourceID="secondNavLevel">
<HeaderTemplate>
<ul class="law-menu_nav2"style="z-index:100">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="childLink" runat="server" NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
The HeaderTemplate takes care of the list container styling then the repeater items are listed out one at a time with an offset of 1 for the current node. This looks easy based on what I have seen around the net, I'm just fairly new to some elements of ASP.NET :) Thanks.

Resources