Dynamically load specific User Control from postback - c#

I have a menu on a .aspx page, which will load a specific user control depending on which menu option is clicked. This part is working fine.
The problem I'm running into is when I instantiate a post back from the dynamically loaded user control, the .aspx page is reloaded. Now after researching a few of the other questions on here, I gathered that I have to recreate the User Control each time the post back is instantiated. However, the question I have might be quite simple, but how do I reload the specific User Control (and fire an event such as OnClick) based on what is posted back to the server?
So my question is: What exactly gets passed back to the server on post back, and is there something in the post back request that will allow me to load the specific control? If there is, how would I get to it?

To do it the manual way, you would have to recreate the control(s) before ViewState is reloaded, like during OnInit for example.
There is a control called the DynamicControlsPlaceHolder that takes care of persisting dynamic controls for you. It's a definite time saver, and it makes persisting dynamic content easy.
Here is a link to the control:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
To better understand what postbacks do, you need to understand the ASP.NET page lifecycle. Here is an MSDN article that explains it in detail:
http://msdn.microsoft.com/en-us/library/ms178472.aspx

Related

Preventing duplicate postback processing when refreshing with update panels

basically what I am trying to achieve is this (asp.net 4) :
If a user does a postback on an ImageButton or similar control it should cause a page Post, and go through the entire asp life cycle.
If this has just happened, and the user presses F5 or similar to refresh, it should ignore all events from the previous post and just do a regular Get.
If the user clicks Save multiple times it should register only 1 Post and not cause duplicates being created etc.
If there are update panels on the page, a Post should only update the panels data, whereas a Get(refresh) should reload the entire page.
I have had a look around and am currently using the Response.Redirect method (once processing is completed in a post it does a response redirect to the same page to replace the Post with a Get). This is unsatisfactory for a number of reasons
It causes unnecessary overhead doing two page Posts every save
I would like to have regions of the page in Update Panels, and at the moment if you change something and save it in one region, it reloads the entire page.
I found this similar SO question here which highlights a few methods, none of which looks like they will solve my needs. I also found this which is an interesting method, but I was wondering if anyone could tell me if it will solve all my above needs before I try implement it? Also, is the onsubmit event firing the only difference between a Post or a refresh mimicking a Post?
Regarding the 3rd bullet above, I have read about the jQuery .one() function, but I am looking for an application wide solution, as most of the app has been developed without this in mind.
Thanks in advance!

ASP.Net Only 1 Form Restriction: Should I be using a webpage for everything?

I am forced to use Web Forms in my project, and sadly, Web Forms only allow - If I may - "Strict" Websites to be created.
Whenever you need a Button you need to put it in a form, and then you need another button which has nothing to do with the previous button, and you can't have 2 forms,
And the idea of putting a DIV that fires a server side (C#) method is kind of difficult, okay it may be easy but all I have found are "tricks", not an "official" clean way.
So I have this idea of making a webpage for each action in my websites.
For Example:
Let's say I wanna click on the ARROW that raises the rating of this question, I would put something like this.
HTML
Rate Up
And Some CSS Codes to make it look like a beautiful button...
Okay now this will take me to a page called Rating.aspx with 2 parameters, the first parameter is the ID of the question that I would like to raise its rating, and the second parameter is either UP (+rating) or DOWN(-rating).
On the Page Load method of Rating.aspx, I would update the database, then redirect to the question page.
This will work perfectly, BUT, is it a good approach? is it professional? (put in mind that there will be many actions to preform like that...)
With ASP.NET you better use server controls. Better way of implementing that is using or , that actually renders your anchor tag. But you can attach OnClick event handler to this control (link button) so after clicking there would be automatic POST to server. The same page cycle for the current page will take place (this is called PostBack) and your attached event handler will fire, where you can actually make changes to the database. So you don't even need to create any other pages for tasks like this. Every server control has specific set of events like OnClick for buttons or OnSelectedIndexChanged for dropdown lists. You can even create your own controls or derive from existing ones and create your own events.
Take a look on following links for more information:
Button Click
Event Handling in ASP.NET
ASP.NET Page Life Cycle

In ASP.NET, what's a good strategy for maintaining dynamically generated controls across Postbacks?

Although this is a great article on handling dynamic controls in ASP.NET, I have an application where I cannot generate controls on PageLoad or PageInit. They are generated much later, after the user has input data. Is there a good strategy for maintaining dynamically generated controls when I cannot generate them during PageLoad or PageInit?
Page_Load will still fire "after the user has has input data". ASP.Net is stateless and the page is regenerated every time there is a postback. In order for controls to keep their states properly you need to make sure you recreate them again.
Your Page_Load event is still firing. You can assign the controls an ID so they can be properly re-created on postback. If you do not assign controls an ID, they will get an automatically generated name. This will cause the input values to be lost and also the viewstate will not work properly.
I asked a similar question that might be of help to you.
Dynamic User Controls get and maintain values after postbacks

Loading My UserControl dynamically and asynchronously on each button click

In my page I have a 'Button' when I click it, it loads an instance of my 'UserControl' to a 'PlaceHolder' .. I've used the 'UpdatePanel' but it was a hazard! .. I think it's because the Asp.Net 'Ajax' sends the whole page to the server and behind the scenes it actually causes a full PostBack. and it will help me if I could implement a partial PostBack so I won't need to save or access my database each time I reload or click the button to re-assign the values to my UserControl.
So I was hopping for some samples implementing a partial PostBack to load only one instance of my UserControl.
I can't post the code in my page here because it's a REAL MESS! .. but if you really need this to help me then maybe I could send this and post a clean solution here on the question.
Thanks for your time .. I hope there's some simple answers to my question because I'm really hopeless here!
The UpdatePanel always causes a postback to the server for the entire page, and only updates the UI's within the update panel. That's just how it will always work. If you only want to load a user control, consider using a JQuery approach to pull the user control from the server and render it via client-side JavaScript.
http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery
http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/
HTH.

Dynamically creating asp.net with c# pages

I am struggling with finding clear answers to dynamically creating the same page over and over. The questions and samples I have found seem to be all over the board on this topic. I have studied the life cycle and still seem to not have a clear answer as to where code should go.
I have a master page and a content page. All the content in the content area needs to be dynamically created (text boxes, ddl's, page tabs, buttons/onclick etc.).
After a user fills in data and clicks a submit button, I need to read the values off the form and rebuild the page completely again (not add/remove controls to current content).
My question is then.
Where do I put my code to build the page?
Will this area allow me to use IsPostBack so I can rebuild content with Request.Form values?
Will my buttons _Click events work?
Are there any working samples out there you could direct me to?
Thank you very much for the feedback...
I don't know all the answers to your questions, but I hope this may get you started. When dynamically generating the UI through code, this happens in Init. Controls dynamically loaded on Init is key because between init and load, on postback, viewstate is loaded for these controls.
This means you need, on every postback, recreate the page as is to match the previous control tree, then deconstruct it after init and recreate the new UI, if something is supposed to change UI wise. This is because it validates the tree structure to determine its the same UI. Now, if you don't need viewstate, this may not be as much of an issue. I haven't verified this without viewstate to see if it behaves different.
It depends how dynamic you need it, whether you need viewstate (is a big factor).
HTH.
Try creating the controls in the page's PreInit method. "IsPostBack" should work and the click event handlers should work as well.
What you need is a web user control, see ASP.NET User Controls
Brian's advices are good and you should follow them.
This might not really answer your question but still I add it as an advice. I'm professionally creating ASP.net web applications at quite a large scale and from my experience I can say that too much "dynamics" is usually bad and should be avoided because it just introduces complexity. Normally you might want to expose UI parts into ASP.net UserControls or if you want to make them even more reusable (if that's a factor) then into ASP.net Server controls. Then you replace different of them dynamically rather than creating everything from scratch.

Categories