Dynamically add web user controls - asp.net

I am pretty new to ASP.NET programming. Now I try to add several web user controls to my page dependent on checked boxes in a tree view that I create from data of my database.
I was thinking about using iframes - one for the tree view and the other one for the controls, but this seems quite complicated. Is there an easier way to do this?
The tree view should always remain on the page an the web user controls should appear or disappear in a scrollable part of the page. Could you tell me what would be good practice to resolve this issue?

iframes would not be the easiest way to go about this (as you have two different server side pages that cannot communicate with each other).
If the number of controls that you are controlling visibilty for is not large, I would suggest that you have your user controls in a panel on the page, all invisible, and the treeview and this panel all on the page, within an update panel.
On check of the treeview, in the partial postback, show/hide the appropriate web user control.
As noted above, dynamically adding controls is problematic, as they have to be re-added every postback and you run into state issues.

Dynamic controls added to the page need to be added back to the page on every postback. I would not use iframes for this; instead, you can have the tree view on the left in a floating div, and another floating div for the right content. If the right content is always checkboxes, use a ListView, Repeater, or some other data bound control that builds the control tree for you. It's much easier to work with these controls as long as the UI is consistent. If not, you can build the right side dynamically. You'd have to readd the checkboxes to the page on all future postbacks.

Related

.net page design with complicated Ajax relationship

I am creating a .net website in c#.
The master page contains a fixed footer bar from which you can display 'bookmarks'. I also have a remove function for each bookmark.
Some aspx pages may have a add bookmark/remove bookmark function (a button control) on them.
I use update panels throughout so button controls are always encapsulated by one.
If I add/remove a bookmark from an aspx page, I can trigger an update to the fixed footer panel. A delegate event is triggered in the aspx page which calls a method on the master page to update the fixed footer panel, so everything is sync'd dynamically.
It doesn't seem possible to do the same the other way around. If i remove a bookmark from the fixed footer, I would need to know if the current aspx page was displaying a bookmark control. Pretty much impossible I would have thought, but I'm open to suggestions.
With that in mind the question really is does anyone have a technique that they use to deal with these scenarios, such as calling a full page update or something similar, or is it a case of doing nothing until the next page load/postback?
Thanks in advance.

Dynamically add controls in Page_Init which loads every time (how to Avoid every controls reloading?)

I am adding RadDock control and adding its Item Command events which require to be added in Pag_Init. And adding user controls to RadDock.
My problem is that when I have some post back for a specific control page_Init calls which reloads the controls and every control is re-binded every time. I want to avoid control creation every time. And want the specific control's post back should happen.
If I apply (!IsPostBack) condition in Page_Init then controls are not loaded and page gets empty.
I am stuck.
Any best practice or work around is acceptable.
Thanks in advance.
I don't know the specifics of Telerik's tools, but if they work like regular ASP.NET dynamic controls, you have to add the controls to the control tree on every page load. Populating the controls with data is distinct from adding them to the control tree. If the controls are added correctly so that they are placed in the same way in the control tree as on the previous page visit, and implement ViewState correctly (if needed) the runtime will populate them with data from the posted data and ViewState when a postback occurs.
I added IFrame to RadDock and then gave user control source to IFrame. Now it is working fine, Only the specific control's post back occurs.
Any way, Thanks Jonas

How to avoid re-binding data controls with ASP.Net Dynamically Loading a User Control

The overall problem is that I have a web application that has many hidden controls that are shown and hidden based on user rights or based on particular actions within the main page. Because there are so many hidden controls and the combination of each of these control's databinds paired with the sheer size of the resulting rendered HTML, the page is too slow to load. We can of course move the databinds to only occur on the show of the controls but this only addresses half of the problem. We are attempting to address both by Dynamically loading the usercontrols. As we all know the problem with this approach is that when dynamically loading the control, it must be re-added to the page on every subsequent request. So anytime say a dropdown in selected on the dynamic child control that causes an action (like OnClick event), it has to reload the child control, which by itself isn't bad but you also have to rebind all the controls on the page or your databound controls will all be empty. So is there anyway to get around having to rebind all of the controls with every load of the child control? I know that for simple things like a textbox I could save the viewstate and the text would persist between requests and reloading the control, but say the child control has a grid with several hundred rows of data after it is bound, how do I persist that when reloading the child control? I can post a small example if needed.
use Update Panel, i think i will call to server only for updated data not whole page or control.

persisting links on an asp.net page - is this a job for viewstate?

I've got a master page with a section for subnavigation links on it:
<div id="sub_nav" runat="server"></div>
I programatically populate this with Hyperlinks when my main asp:Menu data is bound depending on the address of the page I'm on.
This works fine and all my correct submenu stuff shows up on each page. The problem is that when one of these pages does a postback, I lose all the links that were in my sub_nav div.
Now, I could just populate the div with links every time regardless of whether the master page load is a postback or not, but I figured there is a better way of doing this. I was thinking enabling the viewstate on the div and links inside it might persist them through postbacks, but apparently that is not how viewstate works.
What is the correct way of doing this?
Viewstate only stores the current state of a control and not the controls by themselves. If you are dynamically adding controls make sure to add them on page init method irrespective of postback
This MSDN sample should help you.
According to the excellent article TRULY Understanding ViewState, that's not really the purpose of ViewState. Furthermore, ViewState costs additional bandwidth so in general we want to avoid it if possible. It sounds like this data should be "cheap" to obtain (cacheable or whatnot), so I'd definitely populate it on every request and disable ViewState on those controls.
To understand the main purpose of ViewState consider a page with two buttons, btnA and btnB and two labels lblA and lblB.
When the user clicks btnA , the page posts back and sets lblA to "You clicked A!".
When the user clicks btnB, the page posts back and sets lblB to "You clicked B!".
With ViewState, the page remembers that lblA.Text was set to "You clicked A!" previously and restores that value. Without ViewState, if the user clicked A and then B, the page would only display "You clicked B!" because there's nothing to store the previous value of lblA.

Template control in ASP.NET hiding content from page

I'm writing my own Modal Popup as a template control in ASP.NET. I got two template containers - one for the heading of the window and one for the actual control i want to display in the modal window (let's call it the form control). The form controls can contain server controls like buttons, textbox'es etc.
It works well, except when i want to access the form control in my web page. My codebehind won't recognize the content in the template control - just like it won't in, say a Repeater. So i figured a Panel control works just like what i need, except the Panel control only has "one container" and i'd really like to be able to set both a header and the content (form control).
I figured i could overwrite the Panel control to add my own html but that would limit my header to be something encodeable in an attribute. So is my best bet really to expose the Heading as just a property, instead of a template and thus being limited in what i can write for heading?
Just as you can with a repeater, try using the ParentControl.FindControl(...) method to get the control you desire.
I ended up overriding the Panel control, accepting that i can only "pass" one set of controls as its children. Also i had to make the heading a property i set as an attribute on the modalpopup control.
Even though i couldn't solve it the way i wanted to, i think the solution is good enough.

Resources