ASP.NET WebApp How to change nav bar "Login" to "Logged in" - asp.net

Hey guys so I've been looking online and i just cant figure this out..
I've used the Visual studio base Web app essentially as a skeleton to start my web app and i have this in "Site.Master" which i think is the class holding everything about the Navigation page:
"<li><a runat="server" id= "liLogin" href="~/Account/Login">Log in</a>
</li>"
I want to change this name "Log in" To Logged in as a result of an if statement
But: i don't know how to call it...
thanks!

<li><a runat="server" id= "liLogin" href="~/Account/Login">
#if(User.IsAuthenticated)
{
<text>Logged In</text>
}
else
{
<text>Log In</text>
}
</a>
</li>

Related

Checking if the user has email confirmed from a partial razor view

I have a partial named _Username which displays the currently logged in users menu and allows the user to change settings etc...
I need to check whether the current user has a confirmed email address in the database from this partial but for some reason can't get any information from the database in relation to the confirmed email column. I can get the username as you can see from the code below.
This is my partial code:
<div class="username">
<ul class="username__user">
<li>
<a asp-page="/Account/Manage/Index">#UserManager.GetUserName(User)</a>
<ul class="username__menu">
<li>Account</li>
<li>Settings</li>
<li>Messages</li>
<li>
<hr />
<form id="logoutForm" asp-page="/Account/Logout">
<button id="logout" class="btn btn--green" type="submit">Logout</button>
</form>
</li>
</ul>
</li>
</ul>
</div>
This partial is embedded inside the _Layout partial as well.
Any information regarding getting access to the UserManager would be appreciated.
I do have the #inject UserManager UserManager declaration inside my _ViewImports as well.
So after some digging around I did come across a solution. By adding the following at the beginning of the partial I was able to set a bool variable to pull whether the current logged in user has a confirmed email or not.
AppUser usr = await UserManager.GetUserAsync(User);
bool confirmed = await UserManager.IsEmailConfirmedAsync(usr);
This allows me to check what I needed to check so I can notify the user in my partial view when he does not have a confirmed email address.

ASP.NET WebForms - Issues disabling list item

I am running (ASP.NET Web Forms) VB.Net code and in the aspx page, i have something like this:
<ul id="tabsdefault" class="tab-menu">
<li id="accessingIndividuals" runat="server"><a>Accessing Individuals</a></li>
</ul>
and in the code behind file, i have this code:
accessingIndividuals.Disabled = true
However, before upgrading to .NET 4.6, this code worked and now after the upgrade, it does not disable the list item.
I have also manually changed the aspx page item with this:
<ul id="tabsdefault" class="tab-menu">
<li id="accessingIndividuals" runat="server" disabled="disabled"><a>Accessing Individuals</a></li>
</ul>
but that did not work and nor did the following:
<ul id="tabsdefault" class="tab-menu">
<li id="accessingIndividuals" runat="server" disabled="true"><a>Accessing Individuals</a></li>
</ul>
Any idea what has changed in .NET Framework or how can disable this item?
Looking at the page source using Developer Tools, the output is like this:
<ul id="tabsdefault" class="tab-menu">
<li id="accessingIndividuals" enabled="false" disabled="disabled" class="selected"><a>Accessing Individuals</a></li>
</ul>
Looks like all new Browsers ignore the disable attribute on a list item. One possible solution is:
li[disabled]
{
pointer-events: none;
opacity: 0.6;
}

IE Form Submit Empty yet works with type=checkbox

I've looked at dozens of similar questions already but most solutions implement hidden inputs or extensive javascript and none have answered why one type of input works and the other doesn't.
This problem is specific to Internet Explorer, works on both Chrome and FF.
Here's my code to create the form:
#foreach (hello.Data.Models.Button btn in Model.Content.Buttons.OrderBy(b => b.Action))
{
if (btn.Action == "Close Message" && (Model.SurveyAuditExists == true || Model.SurveyContent == null))
{
<li class="response-button-item">
<input type="submit" class="response-button" name="#btn.Text" id="#btn.Text" onclick="submitForm.submit();" value="#hello.Biz.Helpers.ExtensionMethods.TrimString(btn.Text, new char[] { '&' })" />
</li>
}
And here's an example of the resulting HTML:
<form id="submitForm" action="/Go/Here/39354" method="post">
<ul id="response-button-list">
<li class="response-button-item">
<input name="&Agree" class="response-button" id="&Agree" onclick="submitForm.submit();" type="submit" value="Agree">
</li>
<li class="response-button-item">
<input name="&Disagree" class="response-button" id="&Disagree" onclick="submitForm.submit();" type="submit" value="Disagree">
</li>
</ul>
So when I click either Agree or Disagree on Chrome/FF it returns whether the user has Agreed or Disagreed but in IE it returns blank and results in an error as FormCollection and AllKeys are empty in my controller.
Changing the input type to checkbox and clicking on the box works and submits correctly in IE.
I haven't got nested forms in the page.
Any help would be appreciated!
From #stephenmuecke 's comment I tested the buttons without the JavaScript
onclick="submitForm.submit();"
As obviously this was competing with the input type="submit", removing the onclick attribute solved any issues.
So in the end the error was all my fault, hopefully this info helps someone else notice this quicker than I did.

show/hide css menu item depending user role using asp.net

I have the css layout: One column fixed width layout, from maxdesign.com
I have two menu items defined like the following:
<div id="navigation">
<ul>
<li>Data Entry</li>
<li>Reports</li>
</ul>
</div>
Now, let's say that I have two roles: guest and operator, and I want that for example if a user with role guest is logged in, then just the Report item from the menu appear, and in case of a user operator is logged in, then both options appear.
How can I accomplish that?
EDIT:
Based on your responses, I'll go with the server side logic to deal with this:
<div id="navigation">
<ul>
<li><asp:LinkButton ID="lkbDataEntry" runat="server">Data Entry</asp:LinkButton></li>
<li><asp:LinkButton ID="lkbReports" runat="server">Reports</asp:LinkButton></li>
</ul>
</div>
Thanks!
You can put this in the Page_Load..
Dim cs As ClientScriptManager = Page.ClientScript
If Not cs.IsClientScriptBlockRegistered(Me.GetType(), "RoleVariable") Then
Dim js As New String
js = "var _role = " & role & ";"
cs.RegisterStartupScript(Me.GetType(), "RoleVariable", js, True)
End If
And from there, you will have the role in the Javascript realm, where you can manipulate the visibility of the items you want.
So...
<script type="text/javascript">
function hideStuff() {
if (_role === "operator") {
// hide/show your elements here
}
else if (_role === "guest") {
// hide/show your elements here
}
}
</script>
Keep in mind that this approach is all client-side and is therefore easy for another developer to manipulate if they really wanted to. But on the other hand, it's the simplest. Don't use this approach for high-security situations.
You could give your menu elements an ID attribute and then in your codebehind either use RegisterClientSideScriptBlock or use Response.Write to send JavaScript to the client to hide (or show) elements based on some condition.
how about something simple like?
<% if(Page.User.IsInRole("operator") || Page.User.IsInRole("guest")) { %>
<div id="navigation">
<ul>
<% if(Page.User.IsInRole("operator")) { %>
<li>Data Entry</li>
<% } %>
<li>Reports</li>
</ul>
</div>
<% } %>
I don't 100% think you can (or should) be doing logic operations with stylesheets. You may need to have some javascript and then decide based on guest or operator which style to display

change master page <a href link from content page

i have this on my master.page
<ul class="menu">
<li class="first" runat="server" id="Li2">
<a runat="server" id="A1" href="../NewEntry.aspx">Create a New Entry</a>
</li>
</ul>
when i go to content page ("NewEntry.aspx") i want the link name to be changed to "Update Entry"
<ul class="menu">
<li class="first" runat="server" id="Li2">
<a runat="server" id="A1" href="../UpdateEntry.aspx">Update Entry</a>
</li>
</ul>
any feedback?
Make the link an asp:Hyperlink. Then have the master page expose a function or property:
public void SetLink(string href, string text)
{
A1.NavigateURL = href;
A1.Text = text;
}
Call the function from the main page.
You can use a hyperlink control <asp:hyperlink> and set the url as well as the text values.
I would recommend handling this as a HyperLink control as others have mentioned. If for some reason you must handle this as a server-side HTML anchor, you can access it using the following code from your webform code-behind:
HtmlAnchor link = (HtmlAnchor)(this.Master).FindControl("A1");
link.InnerText = "Update Entry";
You can also define a content place holder where you have "Create a New Entry". Leave that as the default inside that place holder, and only in the content page set content for it to Update Entry.

Resources