ID for page is always __Page - c#

I am registering a callback that I want to access asynchronously. This callback will be executed for validation before form submit. Problem is, since the form action goes to a different page, making a reference to Page results in a reference to "__Page", which gets evaluated as a reference to the target page, and the asynchronous calls go there instead of the current page. (The callback is registered in a usercontrol on the page instead of the page itself.)
I can change the form action on the client-side after I make the calls, but I'd rather take care of it all in the callback registration. So how can I register the callback to something other than __Page?

tldr: change the action of the form client-side. DoCallback uses the action of the form to make the async request.

Related

More detailed explanation about GetPostBackEventReference method

I understand that this will causes a page reload (partial or full, depending on how your UpdatePanels are set up)
But,
where in the code I should put it (client or server side)?
which control should I send to the method? Is it must be inside the UpdatePanel?
does this method work only for controls inside update panels?
must the control have a postback capability?
what is the engine behind this? How does this method work, so I could use it properly.
Thanks.
The function call returns a string of executable JavaScript, which you need to write to the client somewhere in your response.
Typically, you're sending your Page (this/Me) unless you have a control that you specifically want to handle the postback (ie, that implements IPostBackEventHandler)
GetPostBackEventReference is not related to UpdatePanels; if you have one, it will handle the postback.
No (see #2)
This makes a postback to the page. If you want it to raise an event when it posts back, you need to implement IPostBackEventHandler, either on your page or on one of your controls.
http://msdn.microsoft.com/en-us/library/ms153112.aspx

Changing displayed data from a static method in ASP.NET

I have two user controls that sit on a page which determines which should be displayed. The user controls generate some html that is passed into an asp:literal and subsequently manipulated via javascript. It is done this way due to the lack of a suitable control that I am allowed to use on the project.
When a user clicks a view change button, a WebMethod is called on the main page (the one that holds the controls) from the control's javascript. From here, a static method on the control is called. The control then needs to regenerate the html and place it into the asp:literal for the view change to be complete.
My problem is that I am in a static method on the control's page, and have no access to the non-static genorateHtml function. I have tried a singleton pattern with no success (this could have been due to improper implementation). Any ideas on how to make this call? THANKS!
I used to hit similar issues at with one of the projects i worked on. The solution we ended up adopting was implementation of System.Web.UI.ICallbackEventHandler with partial rendering to return just the needed content depending on arguments. ICallbackEventHandler runs in the page lifecycle.
The only trouble we had then was performance issues relative to implementation which posts back the whole form instead of just the arguments you want.
Maybe the best way for you would be through this method in which they render the control from a static method. That would probably suit your needs.
Hope this helps!

Reflecting over the Page_Load method returns null

My question is related to retrieving attributes over a method which is called as part of a delegate _[e.g. Page.OnLoad or a Button_Click]_
I have a method attribute [MyMethodAttribute(PropertyOne, PropertyTwo)] over the _[Page_Load]_ or _[AnyWebControl_Event]_. This method attribute needs to be queried at runtime over the method on which it was placed. The method resides in the code-behind of any web page. All web pages derive from BaseWebPage. The event can be either a page load or a post back event.
Based on whether the event was triggered by Page Load or a postback, I get a handle to the control [a page, or the postback] and add my method [MyMethod] to be executed.
_eventControl = GetPostBackControl(page); //__EVENTTARGET or any control
if (_eventControl != null) // this is a postback control for any page
{
_eventControl.Load += new EventHandler(MyMethod);
}
else // this is PageLoad method for any page
{
_eventControl = page;
page.Load += new EventHandler(MyMethod);
}
After which, I am trying to find the event [e.g. Page_Load] which triggered the page load. This is where I am not able to get a handle to the method and tried all different ways around it.
One of the ways was to query which event delegate triggered the load or postback. But the event cannot be accessed from outside of the class, so I am not able to use MyDelegate.GetInvocationList() as this returns null.
Any help is appreciated.
After some more effort, I have found that __EVENTARGUMENT will provide me the method name.
I have a different issue now. When I reflect over the ASPX page for the Page_Load method, it returns null.
Any ideas?
Page_load is a protected method and I was using Public BindingFlags. So after using NonPublic | Instance flag I am able to get to the Page_Load method with relfection
After some more effort, I have found that __EVENTARGUMENT will provide me the method name.
Page_load is a protected method and I was using Public BindingFlags. So after using NonPublic | Instance flag I am able to get to the Page_Load method with relfection
What about Page.IsPostback? If IsPostback is false then the the page load was not triggered by a postback and is a fresh load.

How do you inject a header using the Navigating event in WebBrowser

I'm trying to solve a problem where I'm intercepting a request in Navigating and want to inject an additional request parameter into that request before passing it off.
Here are the steps:
First I make a request via WebBrowser.Navigate(). I have an event handler to capture the Navigating event.
The request hits the Navigating event (before the actual request is made). I, at this point, want to either cancel the request and issue a new request with the added parameters, or somehow alter the current request to have the correct parameter.
Unfortunately I cannot simply call the Cancel event, and re-issue a Navigate call because of one problem. This request is from an iframe, so its one level deep, and the navigate call would clobber the end result of the document contents (as it would show only the iframe contents and not its parent's).
Any suggestions?
It seems impossible to inject a new parameter into a request on low level.
A recommended way is to alter html source of a page sends a request. For instance, add a hidden input field inside a form just after page is loaded. IMHO.

Updating ASP.NET label while processing

I have a method thats run on a button click, with 4 labels. What I need to do is update a label after each block of code executes. I've tried threading, updatepanels, etc and can't seem to get anything to work. I thought the timer would work, but it only ticks when you're not inside a method. The code looks something like this:
private void SomeMethod()
{
label1.text = "Processing...";
AnotherMethod();
label1.text = "Done.";
label2.text = "Processing...";
AnotherAnotherMethod();
label2.text = "Done.";
//etc...
}
You have a misunderstanding of how asp.net works. Your server code runs in response to a request from a browser for a complete html page. This is true even when all you really want to do is run some button click code. The entire page must be rebuilt from scratch anyway, even on postbacks. It's just the way web forms are designed.
As soon as the page is rendered to the browser, that instance of your page class is destroyed. On the next postback you'll start from scratch again, with the notable exceptions of the session, viewstate, and the application cache. Even the page's previous DOM instance in the browser is replaced.
So when you set the text property of the label you aren't directly updating anything visually in the browser. All you are doing is updating some temporary storage in your page class. As the last stage of executing your server code, all those temporary variables are used to render the completed html and the response is finally sent to the browser and shown to the user.
That should be enough information to give you an understanding of why your code doesn't behave as expected. It's running all of the code in the method before any of your property changes make their way to the browser. Therefore, the only thing the user sees is the final state of the operation.
Now ajax can complicate things a bit. When using an ajax control you might not be updating the entire page anymore, but the concept still applies: one request is made, and one response is received and used to update the entire context of the request. You can further muddle things if you have a lot of javascript in place to handle the result of the ajax request.
Unfortunately, there's no quick fix for the code you posted. You'll need to think about how this really works and decide how you want your page to flow.
Can you expose the 3 methods to client script and then call them sequentially from your client side code, when method1 finishes the client script would update the ui then call method2, and so on...

Categories

Resources