Making Elements not possible to manipulate Visibility - c#

I have a ASP-WebForm-Application with several User-Rights.
In CodeBehind I am hiding some Elements if the specific Rights aren't given. But with F12 the User could manipulate the Code the get some Functionallity he isn't allowed to.
Are there any possibilities to hide Elements from CodeBehind, that they aren't make visible via Code-Manipulation? Something like destroying them completely in CodeBehind?
For example a Navigation based an List-Element, where I want to hide some Links:
<ul>
<li>
<a>link 1<a/>
</li>
<li>
<a>link 2<a/>
</li>
<li>
<a>link 3<a/> // Has to be hidden by some conditions
</li>
</ul>
Hope someone could help me!

You can use the asp:PlaceHolder or the asp:Panel and warp your content and make it visible or not.
Alternative you can add on the element the runat="server" Visible="false" and manipulate the visible.
Examples:
<ul>
<li>
<a>link 1<a/>
</li>
<li>
<a>link 2<a/>
</li>
<li runat="server" id="pnlToHide">
<a>link 3<a/> // Has to be hidden by some conditions
</li>
</ul>
or
<ul>
<li>
<a>link 1<a/>
</li>
<li>
<a>link 2<a/>
</li>
<asp:PlaceHolder runat="server" ID="pnlToHide2">
<li>
<a>link 3<a/> // Has to be hidden by some conditions
</li>
</asp:PlaceHolder>
</ul>
and on code behind
pnlToHide.Visible = false;

Related

How to click on a tab with specific text?

I need to click on tab "Pis/Cofins" in my application
<ul class="nav nav-tabs" id="tabs">
<li class="">
Dados Gerais
</li>
<li class="active">
Pis/Cofins
</li>
<li>
Combustíveis
</li>
<li>
Modo de Servir
</li>
<li>
Imagens
</li>
<li>
Informações
</li>
</ul>
You may simply use the following xpath:
//a[text()='Pis/Cofins']
You may use extension like Firebug in firefox to easily find locators and use them in your test scripts
As per the HTML you have shared to click on tab with text as Pis/Cofins you can use either of the following solutions:
LinkText:
driver.FindElement(By.LinkText("Pis/Cofins")).Click();
CssSelector:
driver.FindElement(By.CssSelector("ul.nav.nav-tabs#tabs a[href$='pisCofins']")).Click();
XPath:
driver.FindElement(By.XPath("//a[contains(#href,'pisCofins') and contains(.,'Pis/Cofins')]")).Click();

Cannot use a leading .. to exit above the top directory (ASP.NET webforms)

I have an ASP.NET webforms project with a master page using adminlte UI.
I created a web site with default project in Visual Studio and then change the project to apply adminlte dashboard, copied all scripts and bootstrap then change master page and default form.
My problem: in menu sidebar when I tried to add from to root folder and do this
<li>
<a runat="server" href="Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
everything is ok and form loads without any problem.
But when I created a folder Track and created a new form inside it and try to do this
<li>
<a runat="server" href="~/Track/Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
I get this exception:
exception
What I tried
First attempt:
<li>
<a runat="server" href="../../Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
Second attempt:
<li>
<a runat="server" href="../Track/Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
Third attempt:
<li>
<a runat="server" href="~/../Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
Final attempt:
<li>
<a runat="server" href=~/Track/Shipment.aspx">
<i class="fa fa-th"></i><span>Shipment</span>
</a>
</li>
I've fixed some deprecated and superfluous markup - adjust any related CSS selectors.
Try this:
<li>
<a runat="server" href="~/Shipment.aspx">
<em class="fa fa-th">Shipment</em>
</a>
</li>
The "~/" should resolve anything from the main project root down, presuming there are no page name conflicts. Some of your attempts should have worked so I'm thinking maybe something else is causing a problem. Presuming your menu is in a User Control, maybe it's the path to this control in your Master Page(s) that is causing the problem?

How to get all <li> elements from html with C#

I have html code, which looks like that:
<p>Some text</p>
<p>Some text</p>
<h2>title</h2>
<ul>
<li> element </li>
<li> element </li>
<li> element </li>
...
</ul>
<h2>title 2</h2>
<ul>
<li> element </li>
<li> element </li>
<li> element </li>
...
</ul>
etc.
My question is: how can i get all < li> elements into a List? I don't need titles, i just want elements from lists.
I think HTML Agility Pack could be a solution, but i have no idea how to do that. Thanks for help!

Dropdown master page didn't work after change page

Didn't work dropdownlist Risk Content in master page after change page to the setting page. The dropdown just work after go back to home page again
MasterPage.master
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav custom_nav">
<li class="">Home
</li>
<li class="dropdown">Risk Context
<ul class="dropdown-menu" role="menu">
<li>Objective (KPI)
</li>
<li>Risk Limit & Expected Residual Risk
</li>
<li>Impact Scale Tables
</li>
<li>Probability Scale Tables
</li>
</ul>
</li>
<li>Settings
</li>
<ul>
</div>
What I miss from my code?? help me. thank before..

How to hide Login and Register button after login by user?

I'm working on a school project. I'm able to login user successfully who registered. But when he/she loged-in how can I hide Login and Register page link. I'm using css and <ul> for them. And after sign-out how can I show them again.
Here is the coding of my links on MasterPage
<td id="tabs2" style="width:190px">
<ul id="logreg">
<li><a href="Login.aspx" ><span>LogIn</span></a></li>
<li><span>Register</span>
</li>
</ul>
</td>
And how can I use lable like a button to SignOut.
Wrap the elements in a placeholder control, and set the Visible attribute depending on session state.
<td id="tabs2" style="width:190px">
<ul id="logreg">
<asp:placeholder id="NotLoggedInPH" runat="server">
<li><a href="Login.aspx" ><span>LogIn</span></a></li>
<li><span>Register</span></li>
</asp:placeholder>
<asp:placeholder id="LoggedInPH" runat="server" visible="false">
<li><asp:HyperLink id="btnLogout" runat="server"><span>Logout</span></asp:HyperLink></li>
</asp:placeholder>
</ul>
</td>
Then in your Page_Load:
if (!(session["sessionName"])){
NotLoggedInPH.visible = false;
LoggedInPH.visible= true;
}
Another option is to just check Request.IsAuthenticated
<% if(!HttpContext.Current.Request.IsAuthenticated) { %>
<ul id="logreg">
<li><a href="Login.aspx" ><span>LogIn</span></a></li>
<li><span>Register</span></li>
</ul>
<% } else { %>
// Show a logout button
<% } %>
I recommend using the LoginView control and Forms Auth, but here are some ways to get your current code to work:
Assuming Forms Auth:
<li id="liLogin" runat="server" visible='<%= !User.Identity.IsAuthenticated %>' ><span>LogIn</span></li>
<li id="liReg" runat="server" visible='<%= !User.Identity.IsAuthenticated %>' ><span>Register</span></li>
<li id="liSignout" runat="server" visible='<%= User.Identity.IsAuthenticated %>' ><span>Signout</span></li>
Using Session var:
<li id="liLogin" runat="server" visible='<%= (Session["bla"] == null) %>' ><span>LogIn</span></li>
<li id="liReg" runat="server" visible='<%= (Session["bla"] == null) %>' ><span>Register</span></li>
<li id="liSignout" runat="server" visible='<%= (Session["bla"] != null) %>' ><span>Signout</span></li>
adding runat="server" attribute, though its simple trick.
<td id="tabs2" style="width:190px" runat="server">
<ul id="logreg">
<li><a href="Login.aspx" ><span>LogIn</span></a></li>
<li><span>Register</span>
</li>
</ul>
</td>
after Login Successfull, you can do like this
tabs2.Visible=false
you can simply create a user session and check whether sign-in user exist or not if exist show SignOut otherwise show login panel.
Since you are not using Forms Authentication and your teacher does not allow you to use LoginView Control, I will give you an other option... Here is what I will do..
I would have kept the common functionality like the header in a master page. This way the
user login check would have been centralised.
I would have converted the to asp:hyperlink control. OnLoad method of master page class check if the session variable which holds the id and if it is not null do the following..
hyperlinkObj.Attributes.Add("style","display:none");
I Just put User.Identity.IsAuthenticated == false and it hides the login
<ul class="nav navbar-nav ml-auto">
#if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Login" asp-action="Logout">Logout</a>
</li>
}
#if (User.Identity.IsAuthenticated == false )
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Login" asp-action="UserLogin">Login</a>
</li>
}
</ul>

Categories