I'm debugging some weird behavior in a ASP.NET webforms app. I've narrowed it down to something I find peculiar: A control which implements IPostBackDataHandler has it's RaisePostDataChangedEvent called even if it's a callback from another control. I've reproduced this in a dummy test page.
Information on how it SHOULD be is scarce. I've found only one page which seems to imply that it's only called on postbacks, and not on callbacks.
Is this how it should be? Has it always been so?
After some more digging around in ASP.NET source code I think I can say with certainty - that's how it's always been.
Awesome. I shudder to think how many places in the application have subtle flaws because of this. It's a good thing we're on the path of replacing it all with MVC.
Related
Folks,
I have some personalized properties on an ASP.Net Web Part that I would like to set via Ajax (for example, the size to which the user has expanded the WebPart using the jQuery Resizable plugin.)
I've read this question and answer, but I don't think it will work. Personalized properties are instance properties. (If they were static, they couldn't be user-scoped, could they?) A WebMethod, which must be static, can't access them.
You might be thinking that I could just use a hidden field and send my values back that way, but that assumes that a postback is done at least once after the values are set. In this case I can't guarantee that: the page in question displays a lot of data but doesn't take any user input other than for configuration.
I seem to recall that some Ajax techniques involve remotely instantiating a page on the server and going through at least part of the page life cycle, but the last time I messed with that was 2006 and I never could get it to work very well. I have the impression that modern Ajax techniques, even for ASP.Net, work in other ways.
So, does anybody have an idea of how this could be managed?
Thanks very much,
Ann L.
Webmethods only have to be static when they are page-based. Create a webservice in your project and stick non-static webmethods in there. You can even enable session state.
I have an ASP.net page with two web methods. Everything worked fine until I tried to add an ASP.net UpdatePanel. The UpdatePanel works, but now I can't call my web methods. They fail and return the following error: "Message":" is not a valid valid for Int16..."
I can place a breakpoint on the first line of my web method and it won't be hit; so that tells me something about where the error is coming from.
Anyhow, is it practical to use web methods and UpdatePanels on the same page?
Okay, I must apologize to those who have read my question so far. I discovered a subtle bug that I introduced into my code at the same time I added the ASP.net UpdatePanel.
Normally, I would just delete my question, but since the question has not been previously asked, I'm going to answer my own question:
Yes it's possible to use both UpdatePanel and WebMethod within the same page
... for anyone else who wants to know.
Is it possible to get design mode to work for templated user controls? I have tried following the How to: Create Templated ASP.NET User Controls on MSDN, and also tried the various tips at the bottom of page for version 2.0 of the framework, but alas, I still get the dreaded "Error creating user control" error, when switching to design view.
Should I just give up, and switch to a custom server control?
There is a Connect bug filed on this as well as several comments on the VS2005 version of the MSDN page. Apparently this is a long-running defect that hasn't been fixed and hasn't got a solution. In ScottGu's post about this feature, he acknowledges this doesn't work (in the comments) and points the commenters to the CompositeControl base class if they want designer support.
Probably not the answer you were hoping for, but it sounds like there's no real solution for the issue except moving to server controls.
I was never able to get it to work either. It can be done easily with a regular server control, though. I posted a quick example as an answer to this question:
ASP.NET: User control with access to the controls that it wraps
On my journey into the depths of custom ASP.NET control development I am obviously getting my head around the ASP.NET PostBack model and how it affects control development.
I understand that controls have no "lifetime" in ASP.NET, and therefore must be re-initialized on each and every page load. We overcome this by persisting the objects values/parameters to the ViewState.
Many articles I read therefore suggest not using PostBack since this can add considerable overhead to the Page. I am not looking for how to disable it, I know that.
What I am looking for is:
What alternatives to we have to using the PostBack model to initialize controls?
I know we could use the QueryString, but that seems awfully messy, and obviously unreliable.
Ideally you could give me an overview of the architecture/design of a different approach and the pro's/con's of it..
Many thanks ^_^
Well, Session State is a server-side solution, with its own pile of cruft to deal with if you want to avoid ViewState altogether. Really though, using ViewState in a custom control is all fine and good - just be picky about what you store - only store deltas from the declared control state, don't store anything you're going to get on postback anyway (e.g. from a DB call), etc.
You have to store the values somewhere, so you are limited to the query string and hidden form fields. If you relate that to HTTP, basically it's either GET or POST parameters.
I suppose you could use cookies, but that would be really messy.
Store your object state in the session context: this will shift the burden of keeping state from the client to the server, which may be acceptable for small-scale intranet apps. For sites on the capital-I Internet, this won't work;
AJAX-enable your control: in this case, only state changes need to be posted back. Picking the right framework is key here; see http://www.asp.net/ajax/ajaxcontroltoolkit/samples/ for the official MS approach; many others are possible.
If you're truly looking for alternatives to the PostBack model altogether, then I would suggest researching the ASP.NET MVC Framework. I would love to kick WebForms to the curb and do all my stuff in MVC, but alas, legacy code is a tarbaby and rewriting is almost never the answer, so I plug onwards...
I think you still mis-understand controls somewhat. Controls only have the problem you describe when you add them to the page dynamically. If you declare your controls upfront in the aspx code then they build along with the page.
OK, I am not sure if the title it completely accurate, open to suggestions!
I am in the process of creating an ASP.NET custom control, this is something that is still relatively new to me, so please bear with me.
I am thinking about the event model. Since we are not using Web Controls there are no events being fired from buttons, rather I am manually calling __doPostBack with the appropriate arguments. However this can obviously mean that there are a lot of postbacks occuring when say, selecting options (which render differently when selected).
In time, I will need to make this more Ajax-y and responsive, so I will need to change the event binding to call local Javascript.
So, I was thinking I should be able to toggle the "mode" of the control, it can either use postback and handlle itself, or you can specify the Javascript function names to call instead of the doPostBack.
What are your thoughts on this?
Am I approaching the raising of the events from the control in the wrong way? (totally open to suggestions here!)
How would you approach a similar problem?
Edit - To Clarify
I am creating a custom rendered control (i.e. inherits from WebControl).
We are not using existnig Web Controls since we want complete control over the rendered output.
AFAIK the only way to get a server side event to occur from a custom rendered control is to call doPostBack from the rendered elements (please correct if wrong!).
ASP.NET MVC is not an option.
Very odd. You're using ASP.NET server controls and custom controls, but you're not using web controls? And you're calling __doPostBack manually?
Do you like to do things the hard way?
If I was still using the server control model rather than MVC, I would slap ASP.NET Ajax controls on that sucker and call it a day. What you're doing is like putting a blower on a model T. It may be fun and interesting, but after you're done with all the hard work, what do you really have?
I have been doing some more digging on this, and came across how to inject Javascript in to the client when required. This will obviously play a huge part in making the controls more responsive and less round-trips to the server.
For example: RegisterClientScriptBlock.
Look forward to playing with this more, feel free to get invovled people!