asp.net web forms constructor? - asp.net

I want to load a dictionary with some data and use it in the button event handler of a web form. Where is the appropriate place to put this code? There is a Page_Load method, but I don't want it to run every time the page loads.

in a Page Load event handler, call the method only once when the page first loads.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// your code to load dictionary here, or a method call.
}
}

Related

ASP.NET: Page.Init woudln't fire

I have a custom ASP.NET control. In its Init handler I add a delegate to the Page's Init like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(someCondition())
{
this.Page.Init += delegate(object sender, EventArgs ee)
{
//some stuff
};
}
}
Now, if I add this custom control to the HTML of the page declaratively, every thing works fine, the Page's Init delegate gets called. But if I add this control to the page programmatically like:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MyControl myControl = new MyControl { ID = "myControl" };
this.Page.Form.Controls.Add(myControl);
}
The Init if the control get's called but the delegate that I attached to the Page.Init does not. What am I doing wrong here?
IT's because when adding the controls in OnLoad the Page's Init has already executed
Move the declaration of your custom control to page's OnInit instead of OnLoad. Instantiate your control and add it to the form before calling base.OnInit(e). This will give the page chance to load your control and actually attach your delegate to page's Init event before the Init gets called by the ASP.NET run time. Your problem is that page's Init was already called when your control's Init gets executed.
Instead of adding control in PageLoad, add it in Page_PreInit event handler.

.NET Public Events not wiring up in nested web user controls

I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.

asp.net page's preinit event

I am new to asp.net. I have an aspx page and i have to write some code in its PreInit event.
From where i find PreInit event on the page.
As we double click on button to get button click event(or selecting button and select event from property pane)
Plz reply me ASAP.
Man, why do you need the mouse?
If you need to write some code into PreInit just write the code:
protected virtual void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//your code
}
or in class constructor add a event handler for it:
...
PreInit += new EventHandler(SomeMethodName)
...
and define the event handler method
private void SomeMethodName(object sender, EventArgs e)
{
//your code
}
And by the way, check a .Net Framework book and a Visual Studio manual.
You need to do some reading:
http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

How to subscribe to page events in Web Application Page

I am migrating from ASP.NET 1.1 and I used to subscribe to page events using designer which adds subscriptions to InitializeComponent method on the page.
In the ASP.NET 2.0, how can I subscribe to Page events from the designer?
Not using designer this seems to work.
protected void Page_Init(object sender, EventArgs e) {
// The code, no safety here, just convention
}
// OR
protected override void OnInit(EventArgs e) {
base.OnInit(e);
// The code
}
What is the recommended way of subscribing to page events? Same question applies to the MasterPage.
Additionally what is the best way to subscribe to events on the UserControl? When I declare the Page_XXX on the UserControl the event gets called many times.
Pages support auto event-wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireUp attribute of the Page directive is set to true (bydefault it is true - asp.net 2.0), page events are automatically bound to methods that use the naming convention of Page_Event, such as Page_Load, Page_PreInit, Page_Init etc.
void Page_PreInit(){
}
void Page_Init(){
}
EDIT:
#Dmitriy : So only the method name matters?
Yes
#Dmitriy : Also how do you subscribe to events from designer?
You can't subscribe page events through designer.
See this example: How to add event handler for page events if AutoEventWireUp is false?
public _Default() //Constructor
{
this.Load += new EventHandler(_Default_Load);
}
void _Default_Load(object sender, EventArgs e)
{
}

How do I override page load lifecycle in ASP.NET to prevent ASPX form from loading?

I need to test a condition in several ASPX code-behind files and, in some cases, would like to completely bypass the normal page load process so that the corresponding ASPX page is not loaded. Intead, I'd like to send a custom response to the browser that's written from a code-behind method.
Does anyone know where to start- what method(s) in the page lifecycle to override and the best technique to ensure that my custom Response.Write is sent to the browser while the normal ASPX page content is suppressed?
Thanks.
Probably the easiest way to do it - use Page_Load().
protected void Page_Load(object sender, EventArgs e)
{
bool customResponse = true;
if (customResponse)
{
Response.Write("I am sending a custom response");
Response.End(); //this is what keeps it from continuing on...
}
}
The "easy" way to do it, with Response.End() is terrible for performance, throwing an exception which terminates the thread.
http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx
I had the same question and solved it this way. It's a two-step process: First call HttpApplication.CompleteRequest() and exit your processing. Next override Render() so that the base method is not called. The example code then becomes:
bool customResponse = true;
protected void Page_Load(object sender, EventArgs e)
{
if (customResponse)
{
Response.Write("I am sending a custom response");
this.Context.ApplicationInstance.CompleteRequest();
return; // Bypass normal processing.
}
// Normal processing...
}
protected override void Render(HtmlTextWriter writer)
{
if (!customResponse)
base.Render(writer); // Then write the page as usual.
}
It really depends what you're responding to, is it a posted Form field, authentication info etc...?
The method shown using Page_Load will work, but anything before that point in the page lifecycle will also execute.

Resources