Adding a function to page load event from pre render event - c#

I am working in a asp.net project(ASP.NET 4.0,c#,WebForms).
I need to add a function to the Page Load event based on some conditions in Page Prerender event.How can i achieve this.
I tried the following code,
Page_Load += new EventHandler(delegate(object sender, EventArgs e) { ShowForm(); });
But it doesn't work.Can someone provide some insight on this?

You can't, in the pre-render event you're too late, the Load event has already happened!
See http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

Not possible - Page_Load event happens before Page_Prerender event.
I also suspect that perhaps you are mixing between server side events and client side events - you may want to perform some logic when page gets loaded at the client side (which is different than page_load event - which happens on the server side).
Perhaps you can try to explain what exactly you want to achieve by the code that you have sighted and you may get better answers that would solve your actual problem.

Related

Event handling for dynamically generated controls

I am working on a module where I am basically generating all the controls on the page dynamically using XSLT. They are being rendered and added to the mark up right. Here the problem is that I want to write event handling for this dynamically generated controls and I am not sure how to achieve that because in perfect development environment, we normally double click on our control on aspx page and .NET creates a related event for you in the back on the aspx.cs page.
Any ideas?
Dynamically-added controls generally do not survive postback.
No amount of double-clicking, or even typing the expected names of the controls with _selectedIndexChanged is going to get you what you want.
This is ASSUMING (please let us know if I'm right or wrong) that you are adding HTML controls, not ASP.NET controls dynamically.
You'll need to create a method with the appropriate handler and wire it up to your dynamically created controls when you create them.
i.e.
protected void MyHandler(object sender, EventArgs e)
{
//Do some stuff
}
when you create your controls
LinkButton lb = new LinkButton();
lb.ID = "lbexample";
lb.Click += MyHandler;
Page.Form.Controls.Add(lb);
But it's very important that on your postback, you rebuild these controls as they were or the event won't fire. You'll need to recreate the controls first so that the event can be raised, so any data that you'll need to create the controls will have to be available on the post back using whatever state mechanism you're comfortable with.
If you want to execute client-side events, you can specify the name of the method to fire (or the code itself) by adding the appropriate attribute.
For example, if you want to fire myCheckBox_OnClick when the user clicks your dynamically generated checkbox, you can do the following:
myDynamicallyGeneratedControl.Attributes.Add("onclick", "myCheckBox_OnClick");
You could also generate the javascript code that is to be executed and add it to the page through the page's ClientScript.RegisterStartupScript method.

adding an event handler to a dynamically created checkbox (aspx, c#)

So my problem is that I want to add an event handler to a dynamically created CheckBox. I have already looked at other ways to do this, and decided that creating a dynamic table which contains my CheckBoxes is the best option for me. I have not added these CheckBoxes to the Control Tree because I need to manage the ViewState manually. Either way, my code works in every way except that my CheckBox's CheckChanged Event does not fire. I am adding this eventhandler to my CheckBox in my pageLoad event, however, any page event I try seems to give me the same results:
CheckBox chbxLv1 = new CheckBox();
chbxLv1.ID = "DymanicallyCreatedIDForIdentification";
chbxLv1.AutoPostBack = true;
chbxLv1.CheckedChanged += new EventHandler(this.checkChanged);
/* Way lower in my code */
protected void checkChanged(object sender, EventArgs e)
{
//Some code goes here which never seems to execute... grrr
}
I thought that this may be a problem with the ViewState at first and did quite a bit of research on that. I'm now thinking I am doing something dumb with adding an event handler. I'm not sure why this event never fires, but I'm a little new at adding events to a control. Do I need a delegate here?
--Roman
In order for dynamically loaded controls to be handled properly during the ASP.NET Page Lifecycle, they need to be added to the page during OnInit (or prior to LoadViewState, really) otherwise their state information will not be maintained and you can, in fact, corrupt the viewstate depending on how/where things are added in the page's control graph.

How to trigger SelectIndexChanged event to VListBox?

Somebody know the VListBox?
It is fast to load item to the listbox.
But I can't get the SelectIndexChanged Event be triggered by set SelectIndex.
Somebody know how to trigger that event by winapi or something else?
VlistBox source code is in the page above.
does the event get fired when you click on the item with a mouse?
if so then it may be by design. also if you are selecting it via code then a better way is not "how to manually trigger the event". consider refactoring the event handling code into a new DoSelectionChanged() method and have the code that calls SelectIndex() call that. in this way the "meaty" stuff that normally would be in an event callback can be used by other methods, not just the callback.
hope that helps
Wy don't you call the EventHandler manually?
vListBox1_selectedIndexChanged(null,null);

Help Me Understand This Comment Re Page Lifecycle

I encountered this comment recently:
protected void Page_PreRender(object sender, EventArgs e) {
// doing this at PreRender so we don't have to worry about when/if
// we should bind based on if it's a postback or callback and what not.
OrderList.DataSource = OrderItems;
OrderList.DataBind(); }
I was under the impression that PreRender fired every time Load fires, as part of normal page lifecycle, so what's the advantage of doing databinding here?
if you use PreRender, you can manage any control at this point, just before page goes for display. PreRender occurs in the very end, after all events have occured
This article explains all from start to finish, in
Control Execution Life Cycle
hope this helps
I see no advantage of doing that in response to that event. As far as the specific PostBack comment, he's referring to code like this:
if(!IsPostback)
{
...
}
Wild guess: Lots of code in Page_Load is dependent on whether or not the processing is being done as a result of a postback, and there are some things in that code that must be decided before the binding is made. The commenter therefore believes that maintenance will be easier if the .databind() call is made in some later part of the lifecycle, which makes more clear the fact that the binding is independent of the codepaths in Page_Load.

Client Rendering Problem after 'page.' calls

I have tried, 'PreviousPage', 'PreviousPage.IsCrossPagePostBack' 'Page.previousPage', page.title
It causes the client to stop rendering the page after this line.
simple example
protected void Page_Load(object sender, EventArgs e)
{
response.write("I can see this");
string test = PreviousPage.IsCrossPagePostBack.toString(); //Any page call Causes client rendering to freeze
response.write("But i cant see this");
System.Windows.Forms.MessageBox.Show("However i can see this,proving that the server is still running the code");
}
Anybody Please, any ideas?
ANSWER
Well it ended up it was something stupid. code smell over.
The button i was using to fire the PostBack had a handler that fired to redirect, i just deleted the handler, keeping the PostBackUrl setting and magic.
Have you checked PreviousPage for null?
From msdn:
The PreviousPage property is a null
reference (Nothing in Visual Basic)
when the user requests that page
directly from the server.
Also - MessageBox in a web form, not a great idea... perhaps use the inbuilt trace.axd

Categories

Resources