Control Reuse and UpdatePanel - asp.net

I have a user control that is pretty basic. It contains several TextBox controls, a few DropDownList controls, a save Button and a cancel Button. I would like to use this control in two different modes. The first mode is in the normal postback mode to do the save and cancel actions. The second mode would use AJAX to do the save and cancel actions.
Is it possible to wrap the contents of the control in an UpdatePanel and then be able to turn on/off whether or not the UpdatePanel does AJAX or PostBack for the control events? Or would I be better served by just creating two new controls (1 with UpdatePanel, 1 without) to house the one old control?

Have a look at the Refreshing UpdatePanel Content section here: UpdatePanel Class.
But what I would do, personally, is just create the UserControl without the UpdatePanel, and then enclose it in one when you need to. Less confusing.

The easiest way to just 'turn off' your UpdatePanel is to set EnablePartialRendering to false in the ScriptManager. I'm not sure that this is the best solution. I would recommend adding your controls to an update panel in your codebehind Page_Load event handler based on a boolean flag.
void Page_Load() {
if(IsAjaxy) {
upAnUpdatePanel.Controls.Add(tbSomeTextBox);
}
else {
this.Controls.Add(tbSomeTextBox);
}
Something along those lines should work just fine.

Related

How to load user controls dynamically in ASP.NET without postback

I have a ASP.Net(C#) page. When a user selects a value in dropdownlist, some new controls will be visible to him.
I am able to that now, but the page has to reload every time and state has to be maintained between postbacks.
Is there any way to do the same without reloading of the page in a easiest way possible without using ajax control toolkit?
Additional info: the data for the dropdownlist is coming from the database, and some places it has to be all server side so i can't use javascript .
You can try this idea: http://msdn.microsoft.com/en-us/library/ms178208(v=vs.100).aspx. I have not try it before but it seems logical.
you should go through updatepanel. Put all the controls inside a updatepanel which you want to show on selectedindexchanged event of the dropdownlist and write code behind on selectedindexchanged event of the dropdownlist accordingly. Hope this will help you...

Textbox autopostbacks

I have a custom asp.net user control which has an update panel in it. In this update panel i have all the controls and content that are shown to the user. Amongst these controls there are two textboxes, which have AutoPostback = true. This is because when their value is changed, the structure of the page changes accordingly. This works as required, but when I modify the two textboxes in quick succession, the first autopostback works while the second one doesn't fire. It seems that while it is doing the first postback, any other attempted postbacks will be ignored. How can I work around this?
This behavior is by design. The usual approach is to use UpdateProgress control that disables the user input on the page while the postback is in process.
Alternatively you could add your own onchange event handlers that call __doPostBack() more intelligently (by using timers etc.) to avoid this problem for your specific scenario. You could also try aborting any postback is process before submitting a new one.
A resource that might be useful: http://www.dotnetcurry.com/ShowArticle.aspx?ID=176

UserControl, PlaceHolder and UpdatePanel On PostBack

I dynamically load a User Control, with an Update Panel inside a Place Holder.
When I click a button from the User Control, should refresh the Update Panel contents, but it refresh the entire page instead, and the User Control is disappearing from the page, because the page's Page_Load does not load anything if it's a PostBack.
How I can fix it?
Whenever a partial or full postback happen , Automatically all update() method of all updatepanels fire . For preventing such this behavior you need to set UpdateMode="Conditional" property . In this situation you need to specify asynchronous trigger Or ChildrenAsTriggers=true .
for preventing a dynamically loaded-usercontrol to be disappear ,It's good to save it in ViewState , Here is a tutorial and sample application
I think you'll need to reinject the control in page_load or pre_render. Dynamically created controls don't live through postback.
Make sure you are creating the control EVERY page request, regardless of GET/POST. Also, make sure you are giving it the same ID.
I like to override the CreateChildControls method.
You need to add the control page to the page in the page_init method. It has to be added on each post back. The control will retain all the values even after adding it back.
There is a full working example at this link.

Controls are empty after submit

I have a checkboxlist and textbox controls on my asp.net page and the are dynamically created and added to the page. When I populate the values and submit the form, the values are empty by the time it hits the server. Any help?
They are empty because they are being re-created too late in the page lifecycle.
Without knowing the precise point in the ASP.NET Page Lifecycle you're adding your controls, (though I'd guess it's either Page_Load or an event handler), it goes something like this:
Build control tree
Add dynamic controls
Render
(Postback)
Build control tree
Reconstitute viewstate & bind to Post values
Add dynamic controls
Render
To solve this, you need to make sure your controls are created early enough in the lifecycle. Standard practice is to break the "control creation" into a separate method, and during CreateChildControls, check to see if they need to be created:
override CreateChildControls()
{
if(IsPostBack)
{
EnsureDynamicControlsAreAdded();
}
}
This way, if they do need to be initially added by something as far late in the lifecycle as an event handler (Button_Click, for example), you can call the same EnsureDynamicControlsAreAdded method from there as well, and on the next round-trip they'll be created much earlier.
Further to Rex M's answer, you could try creating the controls in the "Page_Init" event - this is one of the first events in the page lifecycle, and is where I would normally create controls in a viewstateless page (NB: If you do this, do not surround the content of the Page_Init handler with an "if (!IsPostback)" - this will prevent it from working as intended).

Update parent updatepanel from within usercontrol

I have an update panel that contains a repeater that calls a user control into each row. When I click a button inside the user control, the page does not refresh at all, only when I reload it completely. How can I make the update panel refresh from user control's button click?
Phairoh is correct that your button needs to successfully cause a PostBack.
There are three things that affect when a UpdatePanel is updated:
The UpdateMode property - This takes two possible values, Always and Conditional.
If it is set to Always then any postback o the page will cause the UpdatePanel to update.
If it is set to Conditional then the UpdatePanel is only updated when the UpdatePanel's Update method is called or when one of the UpdatePanel's triggers does a postback.
The Triggers proroperty - Defines which controls will cause the UpdatePanel to be updated when UpdateMode is set to Conditional.
The ChildrenAsTriggers property - This is a boolean value that determines if child controls of the UpdatePanel are automatically considered triggers without having to be added to the Triggers collection.
Because your button is in a UserControl it won't be easy to add the control to the Triggers collection or to have the button call the Update method on the UpdatePanel that is not inside the UserControl.
Because your UpdatePanel contains the UserControl's your best bet is to enable ChildrenAsTriggers. If this doesn't work try setting the UpdateMode property to Always. If this still doesn't work then Phairoh is probably correct and your Button isn't posting back.
Sometimes solving this problem not really convenient with Triggers. So here is another fancy approach which could be used even from your user controls without any knowledge about Update Panel:
var scriptManager = ScriptManager.GetCurrent(Page);
if (scriptManager != null)
scriptManager.RegisterPostBackControl(SomeControlID);
Are you sure your buttons on your user control are attempting to do a postback? I haven't used update panels in a while (and I'd actually recommend avoiding them) but if I recall correctly they will refresh whenever a postback is done within them. This means that your buttons must be Asp:Buttons (or similar controls that cause postback) and not have some kind of javascript on them that would not allow their action to continue (such as a return false).

Resources