Application object cannot be used in ASP.NET web page - asp.net

How to use Application object in the web page?
I thought it should be something like Session object.
But when I use Application, it shows the Reference like
System.Net.Mime.MediaTypeNames.Application
Obviously, it's not the one I'm looking for.
Has it been discarded in .NET 4?
If yes, what should I use to replace the Application object.

Are you referring to this one
Page.Application Property
Gets the HttpApplicationState object for the current Web request.

<%
this.Application["test"] = "some value";
%>
inside a WebForm should work. And in the code behind it's the same story:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Application["test"] = "some value";
}
}

The Application (HttpApplicationState) property is very much there.
Seems you have some references that are causing the confusion.
In your CS code on a Page you should be able to use it
this.Application["key"] = myObject;

It should work if you try to access it from an ASP.NET page. Application is a property of Page that returns the HttpApplicationState.
protected void Page_Load(object sender, EventArgs e)
{
if(Page.Application["Foo"] != null)
{
// ...
}
}
If you want to access it from a static context, you can use HttpContext.Current:
if (HttpContext.Current.Application["Foo"] != null){ }

Related

How to make a ASP.NET Webforms application testable?

I am looking at a legacy enterprise application, which written using ASP.NET. No controls or web forms. This is how it works:
EmployeeList.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeList.aspx.cs" Inherits="EmployeeList" %>
EmployeeList.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// Security Check
// Load Template, get version 1.4 of active employee list
StringBuilder template = GetTemplate("Employee", "ActiveList", "1.4", true);
// Get list from database
using(SqlDataReader objReader = GetListFromDB())
{
while(objReader.Read())
{
//fills data
TUtils.Replace(template, ROW, "%name%", objReader[0]);
}
}
// return response
Response.Write(template.ToString());
}
private StringBuilder GetTemplate(string x, string y, string v, bool z);
{
// returns template
}
private SqlDataReader GetListFromDB() {
// returns data reader
}
My question is, since we are not using web forms, is there a way to introduce NUnit in this event driven model (as shown above)?
Also, please avoid suggestions to move to ASP.NET MVC or other patterns, which we are considering, but wondering is there any way to convert this enterprise application testable.
This is absolutely possible. You should have a look on implementing MVP pattern with ASP.NET Webforms. There are several open source implementations but you can do a smaller specialized on your your own.
The basics are to move your code behind logic to a presenterclass. The presenter class has a reference to the page implementing an interface. The trick in your case will be to Mock the Page.Response object for your test. Thats why it´s hard to unit test it right way. The PageResponse Property contains a object deriving from HttpResponseBase and that´s the baseclass you should Mock in your tests and do your asserts on with your example. You could start with that and then extend your presenter with functionalty like Session, Request etc.
If you don´t have any markup at all probably you can just create the presenter in the view constructor and don´t bother of having and reference to the view.
To clarify: The big trick is to get the code out of the aspx.cs file. That beast is not testable.
Sample base class for Presenters:
public class Presenter<T> where T : class, IView
{
protected readonly T View;
protected Presenter(T view, ILogger logger)
{
View = view;
}
public virtual void page_PreRender(object sender, EventArgs e)
{
}
public virtual void page_Init(object sender, EventArgs e)
{
}
public virtual void page_Load(object sender, EventArgs eventArgs)
{
}
public HttpContextBase HttpContext { protected get; set; }
protected HttpRequestBase Request
{
get { return HttpContext.Request; }
}
}
Since most of your code is in the code-behind, I dont think that the usual testing approach with NUnit / Visual Studio testing framework will work well.
However, I think one possible approach is to use UI Testing frameworks like WATIN / Selenium. This will enable you to still write tests for the various functionality.
I have done something similar in the past by basically writing a test case for every UI action that results in a server postback. May not be ideal but it does allow you to automate your testing.

3rd party Slimee DatePicker: how to handle parse error?

I need to use the mentioned 3rd party datepicker and it throws an exception when an invalid date it entered. The author only exposes one event, which is fired when a successful parse takes place. How, in ASP.NET could I catch this error and do something about it, like set a label's text?
There are a couple of approaches you can take here, personally I would replace the default event handler for the TextChanged event via inheritance.
The code assigns one via during the setup and unfortunately textbox is a private member
textBox.TextChanged += new EventHandler(OnSelectedDateChanged);
which is declared as
protected virtual void OnSelectedDateChanged(object sender, EventArgs e)
So we can inherit SlimeeLibrary.DatePicker
public class EnhancedDatePicker : SlimeeLibrary.DatePicker
and then override the EventHandler raising a new parse error event.
public event EventHandler OnDateParseError;
protected override void OnSelectedDateChanged(object sender, EventArgs e)
{
try
{
base.OnSelectedDateChanged(sender, e);
}
catch (FormatException fe)
{
OnDateParseError(sender, e);
}
}
Hope that helps. I haven't checked it but have examined the code for slimees control, but don't want to setup a code project account to download it sorry. You'll obviously need to change your ASP.NET usercontrol references to use the new class.

page event when using page methods?

I have the following that I'm using in every page:
public partial class Pages_MyPage : System.Web.UI.Page
{
ViewUserPreferencesModel TheUserPreferences;
Protected void Page_Load(object sender, EventArgs e)
{
TheUserPreferences = (ViewUserPreferencesModel)Session["SessionUserPreferences"];
And then I use a Page Method like this:
[WebMethod]
public static string GetAppointements(string DateInput)
{
ViewUserPreferencesModel TheUserPreferences = (ViewUserPreferencesModel)HttpContext.Current.Session["SessionUserPreferences"];
My question is this: Do I need to include the statement that loads user preferences when I run the page method or are the statements in the Page_Load event triggered when the page method is called, and if they are, will the variable be populated?
Thanks.
No, Page Methods do not follow the ASP.NET page lifecycle. However, even if they did, your TheUserPreferences variable won't be accessible in the static context.

What am I doing wrong in ASP.NET

Here is my master page code behind:
namespace mysite.MasterPages
{
public partial class Main : System.Web.UI.MasterPage
{
public bool isLoggedIn;
protected void Page_Load(object sender, EventArgs e)
{
isLoggedIn = Request.IsAuthenticated; // Is the user currently logged in
}
}
}
Here is my register page code behind:
namespace mysite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (((MasterPage)Page.Master).isLoggedIn)
{
Response.Redirect("default.aspx");
}
}
}
}
I'm trying to make the isloggedIn accessible to all pages using that as a master page! I get errors like:
Error 2 The name 'isLoggedIn' does not exist in the current context
Error 3 'System.Web.UI.MasterPage' does not contain a definition for 'isLoggedIn' and no extension method 'isLoggedIn' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)
Any help appreciated.
add <%# MasterType VirtualPath="~/Main.master" %> to your page markup.
and your this.Master's type becomes AlphaPack.MasterPages.Main instead of System.Web.UI.MasterPage. So you will be able to access it without cast:
this.Master.IsLoggednIn
Currently you need do next:
((AlphaPack.MasterPages.Main)this.Master).isLoggednIn
And better - create a property. And hold data not in variable but in ViewState (read Control State vs. View State):
namespace AlphaPack.MasterPages
{
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
this.IsLoggedIn = Request.IsAuthenticated;
}
public bool IsLoggedIn
{
get { return this.ViewState["isLoggedIn"] as bool? ?? false; }
set { this.ViewState["isLoggedIn"] = value; }
}
}
}
And what about code-behind. I recommend to use Web App project, not Web Site project (which is out-of-date)!
Next markup syntax is used. Web app:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyNamespace.MyPage" MasterPageFile="~/MyMaster.master" Title="MyTitile" %>
and web site:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" MasterPageFile="~/MyMaster.master" Title="MyTitile" %>
if (((MasterPage)Page.Master).isLoggedIn)
Should be
if (((mysite.MasterPages.Main)Page.Master).isLoggedIn)
You cast Master to a MasterPage type (which is useless as it is already a MasterPage). But MasterPage doesn't contain the property isLoggedIn. That's why you can't access it.
So, just cast the Master property to the right type, in your case mysite.MasterPages.Main
The problem here is that your are declaring isLoggedIn within an inline code-block, so it will only be scoped to within that block.
You'll need to add it as a variable within the code-behind class or within <script runat="server"></script> tags, if adding the server-side code inline, i.e. (trimmed down for brevity):
<script runat="server">
public bool IsLoggedIn;
protected void Page_Load(object sender, EventArgs e)
{
IsLoggedIn = Request.IsAuthenticated;
}
</script>
You could then access the master page in subpages like so:
<%
if (((MasterPage)Page.Master).IsLoggedIn)
{
}
%>
However, I'm not sure that this is the best way to achieve what you want. I would probably take that logic out of the master page and stick it in a purpose built authentication class or service.
Usually one would store such Global 'states' inside a session variable. Are passing it around as a query string parameter?
And why are you not putting the code in the code behind?
Edit 1:
Just move this logic:
Response.Redirect("default.aspx");
directly into your masterpage:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.IsAuthenticated) // Is the user currently logged in
{
Response.Redirect("default.aspx");
}
}
If this is the default behaviour you want for all pages that will be using this Master Page.
Edit 2:
you said in a comment :
and how would i go about accessing the
variable on other pages?
There are 3 ways I can think of:
Query String (Per Page Request)
ViewState (Per Page)
Session Variable (Available Globally)

Update links on master page with data from child page

I have a menu of report links in my master page. I need to append an ID to the end of each whenever the user changes a value on the child page. What's a good way to accomplish this?
UPDATE: I should have mentioned that the child update is happening inside an UpdatePanel, meaning the master page is not reloaded when the change happens.
A MasterPage is really a child control of the page which it controls. You can control a MasterPage like any other control on your page (almost). All you need to do is get a reference to it.
You add a property to the code of your MasterPage, so its code may look something like this:
public partial class _default : System.Web.UI.MasterPage
{
protected string m_myString = string.Empty;
public string myString
{
get { return m_myString; }
set { m_myString = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Then you have to cast the this.Master property to your MasterPage
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Cast here to get access to your MasterPage
_default x = (_default)this.Master;
x.myString = "foo";
}
}
In response to your UPDATE:
The updated panel could write the ID to a hidden field and the menu events could look for that hidden fields in Request.Form["fieldName"].
Note that you shouldn't fieldName.Text because ASP.NET does a bad job of returning the right value for fields that have been AJAXed.

Resources