using PreSendRequestHeaders Event in global.asax - c#

I tried to assign the PreSendRequestHeaders Event in the global.asax file in the "Application_Start" method. But this does not work.
private void Application_Start()
{
PreSendRequestHeaders += OnPreSendRequestHeaders;
}
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
// this is not called
}
The OnPreSendRequestHeaders is not called, why?
Is it possible to assign the PreSendRequestHeaders method in the global.asax?

Just use:
protected void Application_PreSendRequestHeaders(Object source, EventArgs e)
{
}
Or instantiate the handler:
protected void Application_Start()
{
PreSendRequestHeaders += new EventHandler(OnPreSendRequestHeaders);
}
protected void OnPreSendRequestHeaders(object sender, EventArgs e)
{
// should work now
}

Related

i++ keep on incrementing during page load

i++ keep on increasing whenever I reload the page.It should only increment when I trigger the button but I found out that during page reload it also increments.
I did the !IsPostBack but I still encounter the problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number();
}
}
private static int i;
private void cart_number()
{
lbl_cart_number.Text = i++.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number();
}
When you reload a page, it means that it's not a IsPostBack. You should remove cart_number(); from your Page_Load. The Page_Load will be triggered each time there is an interaction between the browser and the webserver.
Remove cart_number() method call from your 'Page_Load'. No need to call that method on Page_Load. Any specific reason why you call from Page_Load()?
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number("1");
}
}
private static int i;
private void cart_number(string flag)
{
int lbl=0;
lbl =int.Parse(lbl_cart_number.Text);
if(flag!="1"){
i=lbl;
if(i>=0){
lbl_cart_number.Text =( i+1).ToString();
}
}
else
{
lbl_cart_number.Text ="0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number("2");
}

Retrieving Session in Global.asax

This is the error I get
"Session state is not available in this context."
Just an amateur programmer, how can I retrieve Session in Global.asax
this is my code in Global.asax
public class Global : System.Web.HttpApplication
{
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
string Email = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
string Roles = LoginVerification.GetUserRoles(Email);
string DisplayName = (string)Session["DisplayName"];
e.User = new System.Security.Principal.GenericPrincipal(
new System.Security.Principal.GenericIdentity(DisplayName, "Forms"), Roles.Split(';'));
}
catch (Exception)
{
}
}
}
}
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
Thank you in advance for answering my question! :)
I guess this code should be in a AcquireRequestState event handler instead of AuthenticateRequest event handler.
Session should be available at this stage
see https://stackoverflow.com/a/9501213/1236044

Handle Application.Exit(CancelEventArgs) in C#

Simply put, how can I subscribe to and handle the following event?
System.Windows.Forms.Application.Exit(new System.ComponentModel.CancelEventArgs(true));
because apparently this is not going to give me my CancelEventArgs:
Application.ApplicationExit += new EventHandler(ApplicationExitHandler);
private void ApplicationExitHandler(Object sender, EventArgs e)
{
...
}
Try to put Event inside the Form1.Designer.cs
private void InitializeComponent()
{
//.......... UI iniatilization
System.Windows.Forms.Application.ApplicationExit +=new System.EventHandler(ApplicationExitHandler);
}
In Form1.cs
private void ApplicationExitHandler(Object sender, EventArgs e)
{
//.............
}

Calling code of Button from another one in C#

I need to know if it's possible to call the Click of a button from another one.
private void myAction_Click(object sender, EventArgs e)
{
// int x;
// ...
}
private void Go_Click(object sender, EventArgs e)
{
// call the myAction_Click button
}
Thanks.
You want:
private void Go_Click(object sender, EventArgs e)
{
myAction_Click(sender, e);
}
But a better design is to pull the code out:
private void myAction_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Go_Click(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// Your code here
}
The button has a PerformClick method.
http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx

BreakPoint not working in Init, InitComplete, PreLoad events in ASP.NET page with C# in VS2008

BreakPoint not working in Init, InitComplate, PreLoad events in ASP.NET page with C# in VS2008. But it is working for Page_Load event.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void InitializeComponent()
{
this.PreLoad += new System.EventHandler(this._Default_PreLoad);
this.InitComplete += new System.EventHandler(this._Default_InitComplete);
this.Init += new System.EventHandler(this._Default_Init);
this.PreRender += new System.EventHandler(this._Default_PreRender);
this.PreInit += new System.EventHandler(this._Default_PreInit);
this.SaveStateComplete += new System.EventHandler(this._Default_SaveStateComplete);
}
protected void _Default_InitComplete(object sender, EventArgs e)
{
........
}
protected void _Default_Init(object sender, EventArgs e)
{
.........
}
protected void _Default_PreLoad(object sender, EventArgs e)
{
..........
}
}
EDIT: move your handlers adding into the OnInit instead of InitializeComponent method:
override protected void OnInit(EventArgs e)
{
// move your initializers here
}
But actually you don't need these initializers at all because of all these handlers could be hooked automatically with AutoEventWireUp=true e.g.:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreLoad(object sender, EventArgs e)
{
.........
}
protected void Page_InitComplete(object sender, EventArgs e)
{
........
}
protected void Page_Init(object sender, EventArgs e)
{
.........
}
protected void Page_PreRender(object sender, EventArgs e)
{
.........
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
.........
}
}
EDIT II: As far as I remember, InitializeComponent is for VS 2003, .NET v1.1. Back then, the InitializeComponent was a place where the IDE serialized construction of the WebForm.
Now this method get never called from your code, so there are no event handlers you expected (and supposed to be added). Now there are 2 options to add hanlers: with AutoEventWireUp=true for general Page events and e.g. in overrided OnInit method.
Try this to help solve your problem of some breakpoints working, some not:
In the Debug menu, select "Delete All Breakpoints"
Save your solution and close Visual Studio
Open your solution, and re-establish your breakpoints in your events.
This should ensure that your breakpoints are set and hit properly.

Categories