Hide controls in ascx without access to code behind - asp.net

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 %>

Related

Webforms: how to specify different content for a given masterpage

Given this markup in an ascx file :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<%#Eval("Content2").ToString%>
</div>
Is there a syntax I can use to chose the display of "Content1" and "Content2" depending of which masterpage is calling?. I.e. :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<If masterpage1>
<%#Eval("Content2").ToString%>
</endIf>
<If masterpage2>
<%#Eval("Content3").ToString%>
</endIf>
</div>
Thanks for your help.
Page's masterpage can be accessed via Master property, so to check for a specific masterpage in use you can do something like
if (this.Master is Master1Type)
The if/else syntax is also possible, and will look like this:
<% if (this.Master is Master1Type) { %>
<%#Eval("Content2").ToString%>
<% }
else { %>
<%#Eval("Content3").ToString%>
<% } %>
However that looks dirty to me, and having conditionals likes this inside the page markup is not a common practice. I would suggest defining a function in code behind to deal with masterpages logic, and output necessary value from the data item:
<%# GetContent(Container.DataItem) %>
protected string GetContent(object dataItem)
{
if (this.Master is Master1Type)
{
return Eval("Content2");
}
// etc
}

Dynamic values for asp control

I'd like have the following type of code fragment in a .aspx page:
<% foreach( int i in <some-expression> ) { %>
<asp:linkbutton CommandArgument="<value-of-i>" OnClick="<some-handler>" runat="server" />
<$ } %>
Basically, I want to generate the page sans data binding, but supply the value of CommandArgument to the linkbutton from a variable in the code behind, so that when the click handler activates, I can retrieve the value of the variable from the CommandArgument, that existed when the code ran.
I do not want to use data binding, for various reasons. Is this possible? I understand that the <%: ... %> syntax is equivalent to Response.Write, and that the syntax <%# %> retrieves values which are databound. I also know that I can't use the <%: %> syntax here to solve my problem. And because I am not using data binding, I can't use the <%# %> syntax.
It seems to me that something like this should be very basic.
So, to answer my own question, there seems to be no way to mix server side controls and state generated from the code around them. The reason being that server side controls are generated before the code runs, and the code, for the most part, runs to emit the response (think Response.Write).
However, one can "manually" cause the post back that server side controls, like asp:LinkButton, perform with a java script call to __doPostBack. So the solution to my problem is something like the following:
<% for( int i = 0; i < 10; i++ ) { %>
<a href="" onclick='javascript:__doPostBack( "MeClicked", "<%: i %>" ); return false; '>Click Me <%: i %></a>
<% } %>
Where Page_Load does something like::
if ("MeClicked".Equals( Request.Form[ "__EVENTTARGET" ] )) {
string arg = Request.Form[ "__EVENTARGUMENT" ];
Response.ClearContent();
Response.Write( arg + " Clicked" );
Response.End();
return;
}

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 making an AJAX call with Jquery

I know how to do this with classic asp.
How do I do it with ASP.net?
Can I just make a code page like verifyLogin.cs
And call that page from Jquery?
Or would I have to make a page like verifyLogin.aspx with a code behind page?
There are lots of different ways to do it, depending on your requirements. Here's a very simple way to start though. Create a page called "AjaxData.aspx" with no code-behind:
<%# Page Language="C#" %>
<%
Response.ContentType = "text/plain";
string data = "some data";
Response.Write(data);
%>
Then use your AJAX call as normal:
$.get("AjaxData.aspx", function(data) {
alert(data);
});

Is there a way to insert text into a page without "Response.Write"?

I 'm trying to make a "login information" on the top panel, like "Welcome Back XXX", so I use
<% Response.Write(Session["username"]); %>
inside the aspx page.
It works, but is there anyway to use the variable directly without Response.Write here? It seems unnecessary.
There is a simple "shortcut" in the ASP.NET page syntax to Response.Write.
<%= Session["username"] %>
is functionally equivalent to
<% Response.Write(Session["username"]); %>
Typically you want to encode your session variables as HTML using Html.Encode, in case they contain characters which are not in the accepted HTML range. If you're using ASP.NET 4, you can use <%: %>, which is equivalent to Response.Write(Html.Encode(string)).
You can do it like this:
<%= Session["username"] %>
And if you use ASP.NET 4.0 you can automatically HTML encode the value by using this syntax:
<%: Session["username"] %>
put a asp.net label on your page, like
<asp:Label id=lblUserName runat="server" />
and on your codebehind page, on page_load event or on proper event
lblUserName.Text = String.Format("welcome back {0}",Session["username"]);
use a label and assign user name to it
In aspx (html code)
<asp:Label id=lblUserName runat="server" />
In aspx.cs (Code behind)
lblUserName .Text = "Welcome back"+Session["username"].ToString();
The correct way.
First is to check if the value is null
Second because you write it on a page, use the HTMLEncode to be sure that you avoid any type of injection, or problems.
now, if you like to use a Literal or a Label, or just direct write it, is up to you. If you going to place it inside an UpdatePanel you must use a Literal.
Now, if you use Literal avoid to set the ViewState to gain space from it, ether way you need to set it on PageLoad. And it will be
<asp:Literal runat="server" id="txtUserName" EnableViewState="false" />
and on page load.
if(Session["username"] != null)
{
Debug.Assert(Session["username"].ToString.Length > 0 , "Check out why I have zero user name");
txtUserName.Text = Server.HTMLEncode(Session["username"].ToString);
}

Resources