Client Rendering Problem after 'page.' calls - c#

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

Related

WebBrowser does not change

I'm making an application in C# which, depending of a variable, shows one web page or another.
When I push a button, the program load the userName and the webBrowser should show a different web page. Here is my source code:
private void button1_Click(object sender, EventArgs e) {
string url = "http://www.url.com/" + userName;
webBrowser1.Navigate(url);
webBrowser1.Refresh();
}
The problem is that, when I push the button a second time with a different variable, the web browser reloads the same web page.
I think it's because of the webBrowser1.Refresh(); you don't need it and i think you are pushing the button 2 consecutive times with different values and it gives you the impression that it's loading another page but it isn't. try to remove that line and add an event to the Navigated method of your WebBrowser object in order to obtain a feedback when the browser is done loading the page.
I have solved my problem. I had set the property AllowNavigation to false, so, when I tried to change the web page, it didn't allow me to did it. Anyway, I needed to remove the Refreshcall to make it work.

Adding a function to page load event from pre render event

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.

Where are delegate instances added to events when generating automatic event handlers?

When I double click on a button (myButton) in Design view of a .aspx web form, an event handler is automatically generated in the code behind: protected void myButton_Click(object sender, EventArgs e)
Now if I understand correctly, in order to associate that method with the Button.Click event, somewhere there has to be something like: myButton.Click += new EventHandler(this.myButton_Click);
However, I can't seem to find that anywhere. I've used Ctrl+F for the entire solution and I've checked the mywebform.aspx.designer.cs.
At first I thought it was because the .aspx page's AutoEventWireup was set to true. However, even after making AutoEventWireup false, the button still responds to being clicked by running the code in protected void myButton_Click(object sender, EventArgs e)
I understand that you shouldn't mess with generated code, and I don't intend to, I just want to know more about how this is working under the hood.
The assignment of the event handler is actually done in the asp markup. Here's a link to a bunch of different properties that can be declaratively assigned to your button.
Here's another MSDN link about using the OnClick attribute.
I'm guessing that your ASP markup for the button has the following property assigned:
OnClick="myButton_Click"
As for how it gets translated into an assignment, the page gets compiled at runtime upon the first time it is requested (ASP.NET Compilation Overview).

Calling a function before Page_Load

I have a button that calls function A()
When I click on it I want the calls to be made in that order:
A()
Page_Load()
Right now it's doing:
Page_Load()
A()
Is there a way around that or is it just by design and there's nothing I can do about it?
The easiest way to do this would be to use a HTML Submit button and check to see if it is in the Form on every postback in Page_Init
public void Page_Init(object o, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.Form["MyButtonName"]))
{
A();
}
}
And in your ASP.NET code:
<Button Type="Submit" Name="MyButtonName" Value="Press Here To Do Stuff Early!" />
I think that will work.
Control events (such as the click events of buttons) are called after page_load. The controls are not guarenteed to be fully initialized prior to page_load. If you really need to call a function before page_load has been called based on whether a button has been pressed you'll have to examine the request to check if the button has been pressed (basically old school ASP)
You need to call your function in the Page_Init. Page_Init will happen before Page_Load.
Here's an Overview of the ASP.NET Page Lifecycle.
Not exactly: ASP.NET will always call Page_Load before handling postback events like Button_Click.
However, you can accomplish what you want by redirecting to your page after handling the postback event. (Using the Post-Redirect-Get pattern.)
Inside your Page_Load method, you can avoid running any relevant code twice by checking to see if it's a postback first:
if (!this.IsPostBack) {
// Do something resource-intensive that you only want to do on GETs
}
As Jeff Sternal answered, The Post-Redirect-Get pattern is a good way of solving a problem like this.
In my circumstances i had a calendar and if you clicked a date it would add that to a scheduler. The scheduler would have buttons on each new date that needed to have onclick functions tied to them.
Because the new row was being added with a linkbutton(on the calendar), in the code the new scheduler date was being added at the Postback event handling meaning that the new set of buttons wouldn't have a command tied to them.
The page life Cycle
Post Get Redirect
I don't think it's possible, at least, not in the way described by your question. When you click a button it will send a request to the server which in turn will start processing it, and follow the ASP.NET Page Lifecycle as posted by Joseph.
Alternatively you could try making an AJAX call to a page without reloading the current one you're on and do whatever processing you require.
This is what you want to do for Page Init is called before Page Load.
Take a look at the ASP.net Page Life Cycle
public void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//CALL YOU FUNCTION A()
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
If your actual goal here is to have your "page loading code" happen after your event handler runs -- for example, if clicking your button changes something in your database, and you want the updated data to be reflected on the page when it loads -- then you could have your "page loading code" get called from a method that gets called later in the ASP.NET page life cycle than your event handler, such as Page_PreRender, instead of calling it from Page_Load.
For example, here's a simplified excerpt from an .aspx.cs page class that has a button event handler that runs before the page population logic, and a confirmation message that is visible on the page only after the button was clicked:
// Runs *before* the button event handler
protected void Page_Load() {
_myConfirmationMessage.Visible = false;
}
// Runs *after* the button event handler
protected void Page_PreRender() {
// (...Code to populate page controls with database data goes here...)
}
// Event handler for an asp:Button on the page
protected void myButton_Click(object sender, EventArgs e) {
// (...Code to update database data goes here...)
_myConfirmationMessage.Visible = true;
}

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.

Categories

Resources