DNN ASP Only show on homepage - asp.net

Wondering if I can do something like this in my DNN skin to only show some html on the homepage and vice versa on the inner-pages with an else statement.
<% if ((url = "/" | url = "")) { %> Doesn't work… <% } %>
Also would be nice to know where the documentation is for this kinda stuff.

There are syntax errors. If you are using classic-asp then
<% if (url = "/" or url = "") then
your code
end if
%>

Related

Hide controls in ascx without access to code behind

I have a website which is written in VB.NET. I do not have access to code behind and only have access aspx/ascx pages. I have some controls like this in my ascx file.
<img src="../css/icons/login.svg" />
I want to make them visible based on if user is logged in or not logged in. I remember it is possible to put the code in design files but I do not remember the syntax in VB.NET and make it visible/unvisible for these controls.
If you do not need to access that link in code behind, which would seem to be the case, you can construct the html as a ternary operator. That way you avoid binding expressions.
<%= Request.IsAuthenticated ? "Login" : "" %>
Or as an inline if statement
<% if (Request.IsAuthenticated == false) { %>
Login
<% } %>
VB Example
<% Dim IsAuth As String = IIf(Request.IsAuthenticated = False, "Login", "") %>
<%= IsAuth %>

Server-side code working with JavaScript using ASP.NET

I am trying to get a server side code working with JavaScript. What am I doing wrong?
function openUp(name)
{
document.getElementById(name).style.display = <%if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))Then %>
response.write('table-row'); <%else%> response.write('block');
}
Your Response.Write statement (which is not properly cased) should be inside the <% %> tags. You also seem to have an odd mixture of C# and VB.NET in your code. Which of the following are you using?
C#
document.getElementById(name).style.display =
<% if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))
Response.Write("'table-row'");
else
Response.Write("'block'"); %>; // Semi-colon outside of %> is for JavaScript
VB.NET
document.getElementById(name).style.display =
<% If (Request.ServerVariables("HTTP_USER_AGENT").Contains("FireFox")) Then
Response.Write("'table-row'")
Else
Response.Write("'block'")
End If %>; // Semi-colon outside of %> is for JavaScript
To answer your question, you can update your code to;
C#
document.getElementById(name).style.display = '<%Response.Write((Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox") ? "table-row": "block")); %>';
VB
document.getElementById(name).style.display = '<%Response.Write(IIF(Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"), "table-row", "block")) %>';

Print code behind function return value in aspx page

I'm sure this is trivial, but why isn't the Windows Authentication user printing in my ASP.NET page inline?
Code behind function:
public string GetCurrentUserWindowsLogin()
{
string windowsLogin = Page.User.Identity.Name;
int hasDomain = windowsLogin.IndexOf(#"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
}
return windowsLogin;
}
Inline code:
<div class="loginDisplay">[ <%#GetCurrentUserWindowsLogin() %> ]</div>
The <%#... %> is used for Binding Expressions like Eval and Bind.
So if you call Page.DataBind() in page_load it should work.
Another way that should work is to use code render blocks which run normal code:
<% GetCurrentUserWindowsLogin() %>
or the <%= %> construct used for small chunks of information:
<%= GetCurrentUserWindowsLogin() %>
Just a follow up on the above answer, the <%= is like response.write.
http://msdn.microsoft.com/en-us/library/vstudio/6dwsdcf5(v=vs.100).aspx

ASP.NET Aspx View - How to Remove Unused Variable Warnings

My company has a zero warning tolerance policy and I have a warning that I just can't get to go away. It is showing on the declaration of the string because
Warning 1 The variable 'message' is assigned but its value is never used
This is the code snippet:
<%
string message = "Message to be displayed";
if (Model.Count == 0)
{
%>
No records to display. <%= message %>
<%
}
else
{
%>
The rest of the code is correct, so it's not a syntax error.
Why would this kind of warning appear in the first place when I am clearly referencing it?
Thx in advance!
It's strange that your company has 0 tolerance to errors and still lets you write C# code in your views which turn them into spaghetti. Also you are hardcoding urls instead of using URL helpers which is very bad.
So you could start by slightly improving the code:
<% if (Model.Count == 0) { %>
No records to display.
<%= Html.ActionLink("Click here to add.", "Details", "ObjectDefinition") %>
<% else { %>
...
<% } %>
Change the declare as below, this issue will be fixed.
When you see this kind of warning, just review the declare. It will OK then.
<%
if (Model.Count == 0)
{
string message = "Message to be displayed"; // declare message here :)
%>
No records to display. <%= message %>
<%
}
else
{
%>
Best regards.

asp.net mvc user control problem foreach loop

Previous post
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
MvcApplication1.Models.FeaturesRepository _model = new MvcApplication1.Models.FeaturesRepository();
%>
<% foreach (var md in _model.GetAllFeatures())
{ %>
<li><%= md.vcr_FeaturesName %></li>
<% } %>
It is with reference to the previous post above.Is there something wrong with the foreach loop(The result is correct but it is displaying the series of Add,Add,Add,Add,Add,Add...,which is the last record of the getallfeatures.
#mazhar, instead of creating your model like you are, in MVC you should return the model to the view.
In your controller you would say something like return View(MyModel);
I don't see anything wrong, per-sey, with your foreach but if you are going to replicate a control over and over you may want to consider a PartialView and rendering that by passing the appropriate model to it.
<% foreach ( var md in model.features ) Html.RenderPartial(md); %>
The above is untested but close I think.
I haven't looked at the previous post because I think you need to get this into the MVC way first. I don't think there is technically anything incorrect in your code and suspect it's your controller and model code.
I've edited your post to remove the commented out code. Very confusing leaving it in.

Resources