How to create a list of links in c#? - c#

I'm trying to create a simple hyperlink list with a RepeaterItem (I'm not particular to a RepeaterItem, so if there are better ways...).
I'm pretty much using the code from the MSDN documentation linked above, but I have a simple problem, I'm using the <% %> control wrong:
Parser Error Message: The server tag is not well formed.
<li><asp:HyperLink id="navListItem" runat="server"
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "Url") %>">
<%# DataBinder.Eval(Container.DataItem, "Text") %></asp:HyperLink></li>
Apparently I cannot use <% within another asp.net tag.
What would be the "correct" way to create a list such as:
<ul>
<li>Link Text 1</li>
<li>Link Text 2</li>
<li>Link Text 3</li>
</ul>
The Url & Link text I get from a resource file.

You could either remove the double quotes for the NavigateUrl property or use a single quotes there instead:
NavigateUrl=<%# DataBinder.Eval(Container.DataItem, "Url") %>
or
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Url") %>'
and this should work.

Related

Access a control in Codebehind that's in a Template

In my ASP.NET WebForms application using Scafolding I have many pages where I need to restrict certain links based on user's role.
For instance, in my Site.Master in my <LoggedTemplate> along with other <li>, I have a <li> for Admin page also. By default, that is not visible, but if the user is logged as an Admin, then I want it to make visible. Which I am not able to do. Here's the code for it :
<LoggedInTemplate>
<ul class="nav navbar-nav">
<li><a runat="server" id="adminLink" visible="false" href="~/Admin/Admin_Page">Admin</a></li>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
In my Codebehind, in Page_Load I am not able to access adminLink only.
Simialrly, in one of the Default page of a Model, the list has links to View, Insert & Delete. If the user is admin, then only I want to show Insert & Delete links. Here's the code for it :
<td>
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Details", Item.ChannelId) %>' Text="View" /> |
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
</td>
</tr>
</ItemTemplate>
I tried adding
<% if (CommonUtilities.IsUserAdmin) { %>
hyperlinks for Insert & delete & finally
<% } %>
but this was giving error. I added ID to editLink, but again cannot access it in Page_Load method.
I am sure, their must be some method to work out with this which I am not able to find yet.
How to deal with this problem ?? Please help me, I have several pages & links to hide & show based on admin role.
Any help is highly appreciated.
Thanks
I think you are looking for FindControl. For example:
Label adminLabel = LoggedInTemplate.FindControl("adminLink") as Label;
adminLabel.visible = true;
Works for me in a few templates, dont know about LoggedInTemplate tho, but can't see why not.
edit: didn't realize ur using <a>. Not sure why you mix asp hyperlink and html but anyway, logic is still the same.
Thanks WEDEBE for pointing out to mix <a> and .
That point gave me a way out. In my design, I change <a> to <asp:HyperLink> & removed code from Codebehind. In design only I tried checking hte role of user & then adding full <li>. Like this :
<LoggedInTemplate>
<ul class="nav navbar-nav">
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<li> <asp:HyperLink runat="server" id="adminLink" NavigateUrl="~/Admin/Admin_Page.aspx">Admin</asp:HyperLink> </li>
<% } %>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
With the other 2 HyperLink's also I did the same way :
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
<% } %>
And this worked. But I realized one thing, when If I add
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin) { %>
like this, in a single line, it's not working. But on adding the curly braces in new line, it works. I know this sounds very strange, I also can't make out why it happens so. But it is fact, that what I have faced & learned.
I know this is quiet simple thing, but just in case my code helps anyone, have shared here.
Thanks

Complex binding expression with IF on asp.net

I have a repeater with a column that I needs to have the following expression, but not sure of the syntax.
If value="DispForm.aspx" then
Show "No Document"
Else
Show the real value.
I tried to use all expresion in one line, but not sure what am I missing here
<a href=" <%# DataBinder.Eval(Container.DataItem, "Path") %>">
<%#
if (DataBinder.Eval(Container.DataItem, "FileName") == "DispForm.aspx")
{
"No document";
}
else
{
DataBinder.Eval(Container.DataItem, "FileName");}%>
</a>
Error :
http://screencast.com/t/ZERZjzZxST
It would be much simpler to use the ItemDataBound event, add an id and runat="server" to the anchor element, then you can use e.Item.FindControl("anchorname") to get the htmlanchor element. You can then set it's text, href, visibility etc. in codebehind far easier than trying to get some of the more difficult databinding logic to work effectively.
I solved it my self like this: item databound events will be fired on the server side for each row, and this report returns 2000 rows. Not a good thing.
<a href="<%# DataBinder.Eval(Container.DataItem, "fileNameUrl") %>">
<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() == "DispForm.aspx" ? "No document" : DataBinder.Eval(Container.DataItem, "FileName").ToString() %>
</a>

How Add Anchor Tags by repeater and To Jump To Specific Location On A Page?

I want to use repeater to populate anchors and links to that anchors dynamically.
I know for statistic case we can do this:
Link Text
and the anchor :
<a name="anchor"></a>
Now, to create the link dynamically I use:
<asp:Repeater ID="LinkRepeater" runat="server">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" class="TopMenuBarLink" NavigateUrl='<%# Eval("Link")%>'>
<%# Eval("Title")%>
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
But when I run the program, on the website, use inspect element I get this for the links:
<a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="Mypath/MyAnchor">My title text</a>
How do I get it in the right form:
<a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="#MyAnchor">My title text</a>
Do you have a specific reason that you're using a server control for your hyperlink? why not use a normal anchor element.
<asp:Repeater ID="LinkRepeater" runat="server">
<ItemTemplate>
<a class="TopMenuBarLink" href='#<%# Eval("Link")%>'>
<%# Eval("Title")%>
</a>
</ItemTemplate>
</asp:Repeater>
I would try this (add the # symbol before the ASP.Net code bracket):
NavigateUrl='#<%# Eval("Link")%>'

asp.net calling a method from front end

i have a method in my back end that i would like to call from my front end, but can't seem to get it working. here is my code:
<% foreach(string item in Plants){ %>
<li>
<span class="folder">
<asp:label ID="lblPlantName" runat="server" Text='<% GetPlantName(item) %>'></asp:label>
</span>
</li>
<%} %>
the getplantName method should return a string and fill the text in. But this is not getting called for some reason.
Anyone have any ideas or suggestions?
Please use <%= GetPlantName(item) %> instead of <% GetPlantName(item) %> and method should be Public or Protected.
To return a string you need Response.Write which is written in shorthand as <%=%> so:
<%= GetPlantName(item) %>
While your code might be working (with the fix suggested by others) it's not good practice. It's the classic ASP way while you're using ASP.NET - it's like driving 10 MPH with sport car on the highway.
One good practice can be to use the Repeater Control - it's still simple and it's much more elegant.
The .aspx will now look like this:
<asp:Repeater ID="rptPlants" runat="server">
<HeaderTemplate><ol></HeaderTemplate>
<FooterTemplate></ol></FooterTemplate>
<ItemTemplate>
<li>
<span class="folder">
<%# Container.DataItem %>
</span>
</li>
</ItemTemplate>
</asp:Repeater>
And to bind the data have such code in the Page_Load function in your code behind:
string[] arrPlants = new string[] { "Sacred Datura", "Kambroo", "Wallflower", "Beech 'Retroflexa'", "Zephyr Flower" };
rptPlants.DataSource = arrPlants;
rptPlants.DataBind();
In your case just replace arrPlants with your real array, Plants.
Feel free to ask for further details or explanations. :)

ASP.net putting a dataitem in a repeater

I've got a:
Data repeater
Control in the repeater
And I'd like to put some values into the controls. At the moment I have a control in it as:
<asp:Repeater runat="server" ID="ArchiveEntryRepeater">
snip
<asp:HyperLink ID="HyperLink1" ToolTip="Comment on Some Entry Title Here" runat="server" NavigateUrl="~/Blog/7/How-to-know-when-this/Comments">
<strong><%# DataBinder.Eval(Container.DataItem, "Comments")%></strong> Comments
</asp:HyperLink>
snip
</asp:Repeater>
Which works fine, but I want to put
<%# DataBinder.Eval(Container.DataItem, "Title")%>
Into the NavigateURL property of the Hyperlink. Just putting it in doesn't seem to work!
Try enclosing your NavigateURL in apostrophes instead of double quotes to not confuse the ASP.NET parser:
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Title")%>' [...]>[...]</asp:HyperLink>
This might be your issue.
You can always bind on the back end using the event itemdatabound. It has some advantages especially where the data is not a neat view or needs some type of major manipulation.
Another option would be to loose the control and replace it with an html anchor control like this:
<asp:Repeater>
<ItemTemplate>
<a id="HyperLink1" title="Comment on Some Entry Title Here" href='<%# DataBinder.Eval(Container.DataItem, "Title")%>'
style="font-weight: bold">'<%# DataBinder.Eval(Container.DataItem, "Comments")%>'</a>
</ItemTemplate>
</asp:Repeater>
Once the server renders a hyperlink to the client there is generally no reason to have it "runat='server'" . The purpose is to navigate to another page so the overhead of using an asp:HyperLink vs. an nchor is wasted.

Categories

Resources