Update parent updatepanel from within usercontrol - asp.net

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).

Related

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

DetailsView resetting visibility on bind?

I am using entity framework 4.0 to bind a database object to a DetailsView on an ascx control. Within the DetailsView, I have a number of asp:panels that I'd like to show/hide depending on what's happening in that person's visit.
So, the first time through the page I'm setting panelA.Visible=false in the FormView_OnLoad event, and all is well - that panel is not output in the HTML. It listens to what I'm asking here.
Once I click submit and postback, I am again checking what's going on and setting panelA.Visibe=false in both FormView_OnLoad and EntityData_OnUpdating. But this time, when the page comes up panelA is showing.
I find that I can only hide that panel after postback by setting visible=false in DetailsView_PreRender, or by binding visibility to a public variable.
I'm thinking perhaps in the life cycle the DetailsView is binding again way toward the end, and throws away my visibility settings, even though they're not bound. So to show/hide panels within the DetailsView on postback, will I always have to set visibility on DetailsView_PreRender or after?
Am I on the right track here, or is something else resetting me at the last second?
Why can I set visibility the first time through the page but not postback?
You should always make final modification of your page structure after postback processing - that is the reason why PreRender event exists. Other possible event in your scenario can be handling DataBound event but better and more clear way is PreRender.

What is the difference between AsyncPostBackTrigger & PostBackTrigger?

What is the difference between AsyncPostBackTrigger & PostBackTrigger?
Controls inside an UpdatePanel by default cause a partial page update, controls outside cause a postback, using these triggers it is possible to change this behaviour as required.
From http://seminaarit.codezone.fi/video/devdays-2007/track1/2/2-ASP-dotNET_AJAX_Extensions.ppt: (dead link)
AsyncPostBackTrigger
Converts postbacks into async callbacks * Typically used to
trigger updates when controls outside an UpdatePanel post back * If
ChildrenAsTriggers="false", can be used to specify which controls
inside UpdatePanel should call back rather than post back
PostBackTrigger
Lets controls inside UpdatePanel post back. * Typically used to
allow certain controls to post back when ChildrenAsTriggers="true"
Here's a blog post which explains the difference:
In the template in an
update panel, there are the options of
an AsyncPostBackTrigger or a
PostBackTrigger.
By default, controls outside of an
update panel will trigger a normal
synchronous post back. The
AsyncPostBackTrigger “wires” up these
controls to trigger an asynchronous
post back. Conversely, controls
declared inside an update panel will
trigger an asynchronous call by
default. The PostBackTrigger short
circuits this, and forces the control
to do a synchronous post back.
1. AsyncPostBackTrigger
it is the one which enforces Asynchonous post back of the Page.., i.e. the AJAX way. The data will be transacted without full post back. When you are using functionalities like login, you may use this.
Ex. You are having two dropDowns Viz., Countries and States. the states should be loaded when a country is selected and it should be changed on Countries change.
You may use AsyncPostBackTrigger in this scenario, which will populate the states ddl without full post back.
2. PostBackTrigger
It is the one which does not follow the AJAX functionalities, but the full post back as usually(as without using UpdatePanel). Situtions are there where you would not like to enforce Partial Post back (as explained in Point 1. above).
Like you are having FileUpload Control withing the UpdatePanel and when you do it by AsyncPostBack, you will not get any values to the server. It requires Full PostBack. in such a case you should use this trigger.
Suppose Button1 is inside your Update panel and Button2 is outside the update panel.
Now let's undertsand that the controls that are outside the update panel are doing a Asyncpostback and that are inside creates a Syncpostback.
So as both buttons are on a form Button1 in inside Update panel and bUtton2 is outside it.
So by the way by giving the Button2's ID and its Event name to the Asyncpostback Trigger as given in the example we suppose that now it will create a syncpostback with the updatepanel as like the Button1.

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.

ASP.NET refresh Update Panel

I have a ListView inside of an Update Panel and wanted to change the Select Query from the code behind fired by a button click event and then reload the ListView inside of the Update Panel. Does anyone know how to cause the Update Panel to refresh from the code behind?
Just do:
YourUpdatePanelId.Update();
From MSDN:
If the page is enabled for partial-page rendering, when you invoke the Update method, the UpdatePanel control's content is updated in the browser. Call the Update method if you have server code that must execute to determine whether an UpdatePanel control should be updated. If you plan to use the Update method, set the UpdateMode property to Conditional. If you want the decision to update the panel to be determined in server logic, make sure that the ChildrenAsTriggers property is false and that no explicit triggers are defined for the panel.
In a typical page development scenario, if you define triggers or if the ChildrenAsTriggers property is true for the UpdatePanel control, the Update method is automatically called during the page life cycle.
If the ContentTemplate property is not defined for the UpdatePanel control, no updates of the panel will occur.

Resources