Why won't VS 2008 SP1 let me edit global.asax? - asp.net

Normally I create web application projects and use code-behind, but I have a requirement to create an small throwaway demo app using code-inline.
I added a global.asax file to the app, but for some odd reason, Visual Studio 2008 SP1 won't let me edit any of the code between the script tags i.e. add code to the event handlers such as Application_Start, Session_Start. VS does however let me edit outside the script tags.
This is just a simple file based web app using the built in web server.
Any ideas what's going on?
This is the code-inline global.asax VS creates:
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when
// the sessionstate mode
// is set to InProc in the Web.config file.
// If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>

Ok, so here is the answer:
You can see part of the explanation here and here.
Basically the global.asax file doesn't actually get compiled, so VS2008 put in a fix to prevent you from modifying it, since your modifications will have no effect.

I don't think VS allows inline script in Global.asax. You need to put the code in Global.asax.cs instead.

The only time I ever saw this kind of of thing is when I am in debug mode, and I didn't realize it. Seems pretty basic, but have you checked that?

tried right-clicking -> view code?

Related

ASP.NET code source updating

I built a website and I want to redirect a site built in asp.net
( similar example www.mysite.net/Front/ContactUs.aspx?Page=ContactUs&mn=ContactUs) to my new site.
the problem is that I have not found where I can make changes in aspx code, I looked into the source code but in vain.
The project contains a lot of files of code, I do not really know what part of the code I have to show you.
Is ASP's projects have a syntax to define the site URLs?
Please I need help, how to make this redirection , and where I can make changes.
Thanks.
Whether you want to redirect all requests or just select individual files, partially or fully, they all normally have a function called Page_Load.
For every file you want to be redirected, you can use this code piece.
private void Page_Load(object sender, EventArgs e)
{
// Check whether the browser remains
// connected to the server.
if (Response.IsClientConnected)
{
// Redirect
Response.Redirect("http://new.website.com/", false);
}
else
{
// Browser is not connected, stop all response processing
Response.End();
}
}
If you want the full site to be redirected, you can use the this function, in global.asax file
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Response.Redirect("http://new.website.com/", false);
Response.End();
}
</script>

What event would I use to intercept and handle all runtime errors in a webform page?

What event would I use to intercept and handle all runtime errors in a webform page?
Also how would I know what exception was intercepted if I used the property e. ?
Thank you!!! <3
If you just want to do it in a single page, you would override the Page_Error method, just like you do Page_Load.
If you want to do it for your entire application, and I think you might, you'll need to change your Global.asax:
protected void Application_Error(Object sender, EventArgs e)
{
var ex = HttpContext.Current.Server.GetLastError();
//Handle your error as you wish
}

ASP.net control Postback Problem (Can't read the value the user entered!)

I've written a custom widget to allow a user to select a location from a database of locations. This is my first ASP.net custom control. It seemed like everything was working fine, but now there's a problem.
My control implements the RaisePostBackEvent function as follows:
public void RaisePostBackEvent(string eventArgument)
{
SelectedLocationId = eventArgument.Split('|')[0];
SelectedLocationDescription = eventArgument.Split('|')[1];
}
I wrote a test page and included the following in my ASP code:
<%= locationSelector.SelectedLocationId %>
That worked fine.
However, in my web application, the following code does not work:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
Response.Write(locationSelector.SelectedLocationId);
// SelectedLocationId is null here!!!
}
When I run this code in the debugger, I see that my Page Load event fires before the Post Back event! Therefore, the data is not yet read from the postback. I know that using the MS provided text field control, the text is available at Page Load, so I think I must be doing something wrong.
How can I read the location that the user selected when the Page Load event fires? To clarify, I'm refering to the Page Load of a web application page.
You're setting SelectedLocationId on a postback event and at the same time you are trying to retrieve its value on the first load. SelectedLocationId will be null all right.
Try:
protected void Page_Load(object sender, EventArgs e)
{
if (locationSelector != null)
Response.Write(locationSelector.SelectedLocationId);
}

Asp.net page routing not working

I am trying to add page routing (I use regular asp.net 4.0, not mvc), so that when a user goes to:
http://sitename.com/public/member/view/andrey
they would get to:
http://sitename.com/public/memberprofile.aspx?userName=andrey
I added following in Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/View/Member/{username}",
"~/Public/MemberProfile.aspx");
}
But when I try going to http://sitename.com/public/member/view/andrey in my browser, I get 404
Is there anything else that needs to be done for this routing to work other than adding a page route map?
Thanks!
Your route says Public/View/Member/{username}
But your link is /public/member/view/andrey
This would definitely 404
Why not try and change your route to
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/Member/View/{username}",
"~/Public/MemberProfile.aspx");
}
and see what happens
I actually found this great article that helped me fix my problem: http://blogs.msdn.com/b/rjacobs/archive/2010/06/30/system-web-routing-routetable-not-working-with-iis.aspx

When and where does a UserControl compile

I have a page, Default.aspx, and a UserControl, HelloControl.ascx. In the page, I dynamically instantiate the control as follows:
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
c.Greet();
}
This works fine, and the user control writes "Hello from a control" to the response. I have no #Register directive in Default.aspx, but when I try a similar dynamic control creation on a client's machine, I get an error that the "Type or Namespace does not exist".
I have even gotten feedback, on forums, from MS, that I need the #Register directive, but I obviously don't. Can anyone help me out with info on how and when the user control is compiled if no #Register directive references it?
EDIT: I have tried a different direction of investigation, losing my initial call to LoadControl, and I still can't reproduce the problem. The following code also works fine on my dev machine, without any #Register directive.
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = new HelloControl();
Response.Write(c.Greet());
}
use full path to control :
this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));

Resources