Using user controls with Ajax within asp.net application - c#

I have an asp.net application in which i have nested master pages. The problem is that i have many difficulties to use an update panel to refresh only a part of the page in a child master page or a web form which inherits from a master page different of the parent one. I think this problem is caused by my use of update panel in the parent master page . So , because I worked with the user controls in WPF application and i'm using to put a container which its contents( which is a user control) changes dynamically when i need to, i wonder if i have two user controls ( login and inscription for example ) and i have this code in my view :
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
//putting a user control
</ContentTemplate>
</asp:UpdatePanel>
How can i change dynamically the user control used in the update panel?
Is it a good approach to do like this?
Thanks,

The documentation on MSDN states:
To add items to the UpdatePanel control dynamically, use the
ContentTemplateContainer property, which enables you to add child
controls without having to create a custom template that implements
the ITemplate interface.
I found that anytime I try to use the UpdatePanel, it ends up causing tons of problems. I've found using AJAX (with jQuery to make it easier) to be a much better solution.

Related

Multiple web controls to trigger one Update Panel

I have three web controls that has to update a gridview inside an updatepanel.
The markup is like this:
<asp:DropDown></asp:DropDown>
<asp:Button></asp:Button>
<asp:DropDown></asp:DropDown>
<asp:UpdatePanel>
<asp:Gridview></asp:Gridview>
</asp:UpdatePanel>
I cannot put the updatepanel before the first dropdown because I have used the bootstrap datepicker and that does not behave properly if I put the updatepanel in the start.

asp.net panel loses viewstate on postback

I have a web page with an update panel in it.
Inside the update panel I have a panel with a user control like this:
<asp:UpdatePanel ID="updatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlFiles" runat="server" EnableViewState="true">
<files:FilesControl runat="server" ID="filesControl" />
</asp:Panel>
</ContentTemplate>
I have a list of checkboxes that whenever the user checks one of them I want to add another FilesControl to that panel
so I am doing it like this:
FilesControl files = (FilesControl)LoadControl("~/UserControls/FilesControl.ascx");
files.ID = XXX;
pnlFiles.Controls.Add(files);
But on every postback (every checkbox checked) the panel loses the last state and the controls added are wiped out, so the panel actually returns to its initial state every time and I can't ad more and more controls to it.
I enabled view state and it didn't help.
What am I missing out here ?
As requested, my comments as answer:
You need to add the control(s) to the page on every postback in Page_Init or Page_Load(at the latest) with the same ID as before.
The ID is unique, I generate it. I add the controls on the checkbox
checked change event chkCompare_CheckedChanged
While it is perfectly fine to add a control dynamically in an event, it is necessary to re-create this control on the subsequent postbacks. That must be done in Page_Load at the latest (better: Page_Init). So you need to store somewhere which controls you have already added to be able to re-create them. Maybe it is sufficient to store the control-count in the ViewState.
So I should store the controls I added in the ViewState object and
then re-add them to the panel on Page_Load ?
No, you shouldn't store the controls in the ViewState but the information that is necessary to re-create them. How do you know what you need to add? Of course you could also use a webdatabound control like Repeater, DataList or GridView where you only need to assign a DataSource and the persisting stuff is done automatically for you.
That is what is done with all controls even if you add them declaratively(on aspx). But those controls are re-created automatically by ASP.NET. All variables are disposed at the end of the page's life-cycle since the communication between the client and the server is stateless and disconnected.

Add server controls to a page that have no form

I have A single page that has the following structure
<form runat="server"><placeholder></placeholder></form>
I have a control call 'feed' that gets added to the placeholder. The control that gets added, holds multiple other controls called 'product'.
I load the products into the feed dynamically with ajax.
The problem is, that the mark-up in the product control contains server side buttons that require a form tag with runat=server. Otherwise the page wont compile.
The page compiles fine once I add the necessary form tags to the 'product' control. This is not satisfactory as I don't want multiple form tags on the page at once.
Is there any way around this?
You can either add a single <form runat=server> that wraps all server controls or don't use server controls (normal html tags).
Or, use a more flexible framework like MVC that gives you full control of your markup.

How can we use validation controls in a master page in asp.net

How can we use validation controls in a master page. The master page has some server controls and some html controls. Now if we place the validations controls on master page would it effect the child page as well. I read about validation controls and master pages from my own blog.
(Why do you want the validation in the master page?)
A better design choice is to put the validation controls together with the controls you want to validate.
If the controls are on the master page, then you can put validation controls in the master page. If they are on a page using the master page, put them there.
I suggest to make use of user controls. Group the actual control together with the validation control. This would give you much better control over your layout and validation. And as a bonus it's reusable.

How can i create a ModalPopupExtender control dynamically from a server control?

I have a composite server control that does quiet a number of things; one of which is to display a ModalPopup OnClick of a dynamically generated HtmlAnchor control.
I need to create this ModalPopupExtender control dynamically in my server control and trigger it from within.
I have been able to create it and trigger it from a button created at design time but not at runtime. This is as a result of the ID assign to the link is always not found by the ModalPopupExtender control.
I have tried assigning a static ID but no success. Can anyone help?
I figured it out. All i needed to do was recreate the HtmlAnchor control in the overridden CreateChildControls method on postback.
Thanks David for you concern.

Categories