asp.net calling a method from front end - c#

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. :)

Related

Ordered list using repeater in ASP.net

The code below keeps giving me an error:
Repeater code in .ASPX file
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate><ol class="DescriptionRepeater"></HeaderTemplate>
<ItemTemplate>
<li>
<%= this %>
</li>
</ItemTemplate>
<FooterTemplate><ol/></FooterTemplate>
</asp:Repeater>
Code to populate the repeater in .ASPX.CS file
im using visual studio 2017 the code is causing the ordered list to be filled with the path to the .aspx file
List<string> lstDescription = new List<string>();
lstDescription.Add("this is the first description");
lstDescription.Add("this is the second description");
rptDescription.DataSource = lstDescription;
rptDescription.DataBind();
It is not a practical scenario. Anyhow, you just use Container.DataItem, since DataItem itself is a string. Please make sure you use binding syntax <%# %> instead of <%= %>.
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate>
<ol class="DescriptionRepeater">
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
<ol />
</FooterTemplate>
</asp:Repeater

asp tags wrapping codebehind variables

I'm trying to understand why or how to code around a problem using an if statement buried in a .aspx page using a Boolean variable that is in the page behind.
The first method works fine if I keep all everything in simple strings:
<%# (startBlock ? "<tr><td>This works</td></tr>" : "") %>
However, the second method does not work.
<% if (startBlock)
{ %>
<tr><td>This does not work</td></tr>
<td>
<asp:Label ID="Label9" Text='<%# Eval("some_variable") %>' runat="server" /></td>
</td>
<%}%>
I understand that my variable startBlock needs to be bound but adding the # symbol causes an error. How can I accomplish the second method where I need to jump in and out of code that is in the code behind the .ASPX page?
Thank you in advance.

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

How to create a list of links in 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.

How to 'bind' Text property of a label in markup

Basically I would like to find a way to ddo something like:
<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label>
I know I could set it from code behind (writing lId.Text = MyProperty), but I'd prefer doing it in the markup and I just can't seem to find the solution.
(MyProperty is a string property)
cheers
You can do
<asp:Label runat="server" Text='<%# MyProperty %>' />
And then a Page.DataBind() in the codebehind.
Code expressions are an option as well. These can be used inside of quotes in ASP tags, unlike standard <%= %> tags.
The general syntax is:
<%$ resources: ResourceKey %>
There is a built-in expression for appSettings:
<%$ appSettings: AppSettingsKey %>
More info on this here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
Leave the markup as is and make a call to Page.DataBind(); in your code behind.
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>
since asp.net tags do not allow <% %> constructs, you cannot use Text="<%= MyProperty %>".
<div> <%=MyProperty"%></div>
Call lID.Databind() from code-behind
When you use <%# MyProperty %> declaration you need to databind it, but when using <%= MyProperty %> you don't (which is similar to just writing Response.Write(MyProperty).
You can do this:
<asp:Label ID="lblCurrentTime" runat="server">
Last update: <%=DateTime.Now.ToString()%>
</asp:Label>

Categories

Resources