I have a search button in a asp.net page that show a pop-up and by closing that some controls are be added to a panel in my form using AjaxManager of Telerik.
I've used the Telerik:AjaxUpdatedControl to say render the panel everytime a search is done.
But my proble is when search is done for second time the controls that had been adde for first time will be removed.
how can I overcome this problem?
Dynamic controls are not created after a postback. You have to recreate them every single time. (Their viewstate is still maintained you just have to create them with the same ID and in the same place within the control hierarchy).
The DynamicControlsPlaceholder control does this all for you. It is available at - http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
However I would suggest you use it for as little as possible. Heavy usage of it can cause performance to suffer.
Related
I've created a ASP.NET website and on page load I have to dynamically add button controls(80 to 500). When I add more than 40 buttons it takes a little bit of time for the page to load. So my question is: What is the best way to dynamically add button controls to ASP.NET web form without losing performance?
Thank you.
The most efficient way would be Controls.Add(..). However, its likely rendering time here is your bottleneck - which is not strictly and ASP.net issue. Adding controls to an ASP.net control collection is super fast.
Albeit, 500 buttons on a given page seems excessive. You should probably look at a paging solution to get better performance.
In my webpage, I am creating HTML table/div/controls at run-time. But, on Page refresh, it disappears.. What is the best way to retain/restore all the dynamically created HTML controls with their values?
ASP.NET and code behind is C#. Mostly I am using HTML controls, JQuery/JavaScript
All controls must be rebuilt in Page_Load so that event handlers and value binding can occur. So, if you create the controls initially in an event handler then you need to cache enough data in Session, or ViewState, so that you can rebuild those controls on Page_Load.
A lot of times a basic Tuple will work to provide enough information for those controls to be recreated. A little note though. You're going to need to ensure you set the ID property the first time they are built and when they are rebuilt. That's how value binding occurs.
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
We have an application where we have a single level navigation menu with some heavy-duty pages on each link. The user can switch back and forth between these pages frequently to obtain information that he needs.
Once the page gets generated, it wouldn't change for the session. However, the page is specific to the user, hence we cant cache it.
I was trying to come up with a solution where we generate the page once, and keep it hidden in the background until its link is clicked, but haven't been able to get my head around this.
One of the ways I thought was to have multiple div tags (one for each page) on one page and keep toggling the visibility as the links are pressed, but that would end up making this single page very heavy. Someone also suggested using iFrames, but I am not really comfortable using the iFrames much and I'm not even sure, if it would be any helpful either.
Can you guys please suggest a few approaches to tackle the issue?
update: Just to clarify, we are fine with keeping the pages separate and navigate across using a standard menu bar. We were just looking for ways to optimize the performance as we know that the pages once generated wouldn't change and there should be some way to tap that benefit.
You can use Ajax tab control for this purpose
Try taking a look at this MSDN article which specifically tackles the issue of how to user-level cache. Also, it might be more manageable to break each tab into a user control. That way your ASP.NET page has just the tab control and 1 user control for each section under the tab. It makes managing tabs much easier.
EDIT:
What I would do in your case, since you say the data won't change for the user, is I would grab the static data from the database and then I would store that data in the Session cache. THe session cache is specific per user and you can try to retrieve the static data from there instead of repetitively calling the database.
Check out the ASP Multiview control. Just remember that even though the different views are hidden when not active, their viewstate is still being sent back and forth. Can be a benefit if you need to check control values across views though.
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.