I bet this question was asked before, but I couldn't find the right keywords to search for.
I am trying to call my variable (currentUser.FirstName) in the middle of the HTML, but it won't recognize the variable if I try. It does work if I don't put HTML tags around the code.
Here's my code:
<% var currentUser = ProjectGalaxy.Website.GetCurrentUser(); %>
<% if (currentUser != null)
{ %>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%=
currentUser.FirstName %></a></li>
</ul>
<% } %>
Thank you,
Thomas
The variable you are creating within the first server block <%...%> is likely no longer in scope so when you exit the first server block and are writing the html you would have lost the context.
There are a couple of options depending if you are limited to addressing this in the display page only or are able to edit both the display and code behind.
Option 1. Remove the variable and refer to the underlying value from the server method directly.
There is a duplication of the call but it removes any scoping issues and keeps it localised if you are unable to edit the code behind.
<% if (ProjectGalaxy.Website.GetCurrentUser() != null) {%>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:ProjectGalaxy.Website.GetCurrentUser().FirstName%></a></li>
</ul>
<%}%>
Create a property in the code behind and assign the method value to that property. (Requires both display and code behind file changes)
// aspx.cs code behind
protected bool CurrentUser
{
get { return ProjectGalaxy.Website.GetCurrentUser(); }
}
// aspx display page
<% if (CurrentUser != null) {%>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:CurrentUser.FirstName%></a></li>
</ul>
<%}%>
Move control to the code behind and create an asp:panel server control with the code and toggle the visibility of the panel during the page lifecycle.
// aspx.cs code behind
pnlWelcomeMessage.Visible = true; // during page load if condition is true
// aspx display page
<asp:Panel ID="pnlWelcomeMessage" runat="server" CssClass="nav navbar-nav navbar-right" Visible="false">
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:ProjectGalaxy.Website.GetCurrentUser().FirstName%></a></li>
</ul>
</asp:Panel>
Hope it helps.
Related
Issue: Using roles that I have created in my database, I want to be able to limit use for specific users depending on that users role.
Information: I am not using an MVC approach, only empty .aspx pages that I have wired up through to my database to show information etc. Provided are images of tables that are holding the role/user information in my database Roles , Users , Role/User. Ideally I want to remove items from my menu depending on which role the user is in. So lets say the user is in the role "Worker" they will only see 2 menu "controls" or "buttons" on the aspx page menu.
Attempted solution: On the landing page after a user has logged in, the Page_Load method checks to see which role the user is in and sends them to the appropriate page. The issue with this solution is that I have to create duplicates for every page depending on which role the user is in.
Question: How can I edit the HTML in a aspx webform using the c# code behind.
Question 2: Is there a simple solution to limit use depending on a user's role without have a ton of duplicate aspx pages.
Please let me know if there is more information needed.
The menu is created in the HTML on the aspx form like this:
<ul id="centered">
<li><a href='Welcome.aspx'><span>Home</span></a></li>
<li class='active has-sub'>
<a href='#'><span>Sales</span></a>
<ul>
<li class="active has-sub">
<a href='#'><span>Sales</span></a>
<ul>
<li><a href='Sales.aspx'><span>Create</span></a></li>
<li><a href='Sales.aspx'><span>View</span></a></li>
</ul>
</li>
</ul>
</li>
You need to mark you menu ul to runat="server" and give it a unique id.
<ul id="centered">
<li><a href='Welcome.aspx'><span>Home</span></a></li>
<li class='active has-sub'>
<a href='#'><span>Sales</span></a>
<ul>
<li class="active has-sub">
<a href='#'><span>Sales</span></a>
<ul runat="server" id="ul_menu">
<li><a href='Sales.aspx'><span>Create</span></a></li>
<li><a href='Sales.aspx'><span>View</span></a></li>
</ul>
</li>
</ul>
</li>
</ul>
Here I have change to <ul runat="server" id="ul_menu">
Now you can use this id ul_menu and add item to this from code behind on the basis of role of the user. for example --
protected void Page_Load(object sender, EventArgs e)
{
PopulateMenu();
}
private void PopulateMenu()
{
string role = "B";//You can get the role of logged in user from membeship
StringBuilder txt = new StringBuilder();
if (role == "A")
{
txt.Append("<li><a href='Sales.aspx'><span>Create</span></a></li>");
}
if (role == "B")
{
txt.Append("<li><a href='Sales.aspx'><span>View</span></a></li>");
}
ul_menu.InnerHtml = txt.ToString();
}
I have a Confirm-user.aspx in asp.net bound to master page. The Confirm-user page is in folder(Manage_pages). I have the sidebar in master page and use this code for list all page that user can access:
<div id="sidebar">
<ul>
<li>
<h2>title</h2>
<ul>
<% foreach (DAL.Page page in pageList)
{%>
<% Response.Write("<li>"+page.PageName+"</li>"); %>
<% }%>
</ul>
</li>
</ul>
</div>
The problem is that when I click on Confirm-user page link I go to this page successfully:
http://localhost:2112/Manage_pages/ConfirmUser.aspx
But when I click on link again it does not work and go to:
http://localhost:2112/Manage_pages/Manage_pages/ConfirmUser.aspx
Please help me!
use
<a href='<%=ResolveClientUrl("~/ConfirmUser.aspx")%>'>text</a>
the "~/ConfirmUser.aspx" could be "~/Manage_pages/ConfirmUser.aspx" if Manage_pages is your intended sub folder but not your application name
i want to know if it is possible to transfer a value from a master page to anotehr master page? i want to transfer my "Name" value to the next masterpage. images shown below
the name "Jedi Ablaza" will be transffered into the next master page
and my code for my first master page
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a runat="server" href="~/" style="color: white">Home</a></li>
<li id="forHR" runat="server" class="dropdown">
<a id="hr" class="dropdown-toggle" runat="server" data-toggle="dropdown" href="#" style="color: white">HR</a>
<ul class="dropdown-menu">
<li>Core Values</li>
<li>Rater & Employee Relation</li>
</ul>
</li>
<li>
<a id="toMosef" runat="server" href="MOSEF/WebForm1.aspx" style="color: white">MOSEF</a>
</li>
to the highlighted portion of the new masterpage.
how can I do that? thanks!
Use Sessions.
Assign to the session:
Session["username"] = txtusername.text;
retrieve from the session:
string username = Session["username"].toString();
basically I have a button in my navbar which is defined in master file. And I want to call a c# method when that button**(bt)** is clicked.
But I can not access the control in code-behind. I also need to control visibility, so I believe it has to stay in one of those Views.
<asp:GridView runat="server" ID="lw3">
<EmptyDataTemplate>
<ul class="nav navbar-nav navbar-right" style="color:#ffffff !important">
<li><a runat="server" href=""></a></li>
<li><a runat="server" href="" title=""></a></li>
<li><a runat="server" id="btout">bt</a></li>
</ul>
</EmptyDataTemplate>
</asp:GridView>
Put the <a> link outside of Gridview to directly access in code behind.
If you still want to put inside the grid then you will be able to access these <a> link through grid by doing lw3.FindControl.
After you get access to those control then you will be able to control visibility as well by doing:
btout.Visible = true; or
btout.Visible = false;
I Hope this will help you.
Having a strange problem with the RenderControl method.
I have UserControl (an ASCX file) with this mark-up:
<ul>
<asp:Repeater ID="rptImages" runat="server">
<ItemTemplate>
<li>
<a href="<%# ((Image)Container.DataItem).Url %>">
<img src="<%# ((Image)Container.DataItem).Url %>?mw=80&mh=50" title="<%# ((Image)Container.DataItem).Title %>" alt="<%# ((Image)Container.DataItem).Alt %>" />
<p><%# ((Image)Container.DataItem).Description %></p>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
When this code executes in the normal page lifecycle (eg. when it is added to a page), it renders valid XHTML as mark-up:
<ul>
<li>
<a data-fullscreen="/someimage.jpg" href="/another-image.jpg">
<img src="/myimage?mw=80&mh=50" title="Image Title" alt="Alt Text" />
<p></p>
</a>
</li>
</ul>
Note how the p tag has a closing tag (even though it is empty), and the image tag also has a closing tag.
When I instantiate this control on the server and try to parse it to a string using the RenderControl() method like this:
StringBuilder builder = new StringBuilder();
using (StringWriter writer = new StringWriter(builder))
{
using (XhtmlTextWriter htmlWriter = new XhtmlTextWriter(writer))
{
var control = (GalleryControl)LoadControl("~/layouts/Controls/Gallery/GalleryControl.ascx");
control.Images = m_images;
control.RenderControl(htmlWriter);
}
}
return builder.ToString();
Then the XHTML that is returned looks like this:
<ul>
<li>
<a data-fullscreen="/someimage.jpg" href="/another-image.jpg">
<img src="/myimage?mw=80&mh=50" title="Image Title" alt="Alt Text">
<p>
</a>
</li>
</ul>
Note how the image tag is missing its closing tag, and the p tag is also not closing, making this XHTML no longer valid.
I've spent the entire day on this. I've tried the XhtmlTextWriter instead of the HtmlTextWriter to pass into RenderControl, but this didn't make any difference.
Has anyone else ever come across this problem? It's quite bizarre and has a lot of us in the team stumped at the moment! Any help or ideas would be appreciated.
EDIT:
I probably should have mentioned that this code is being executed in the Sitecore processor stack. It runs in the "renderField" processor stack just before the ExpandLinks processor.
I suspect this line doesn't execute or an exception is thrown and swallowed not allowing it to finish:
<%# ((Image)Container.DataItem).Description %>