update panel does not working for views - asp.net

i am using updatepanel for a linkbutton so that it doesnot refresh the whole page . on click of that linkbutton i need to set an active view in a multiview control which is working fine without updatepanel.
but when i use updatepanel it doesnoot work. can you help me how to do this
updatepanel code
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:LinkButton ID="changephoto" runat="server" ForeColor="Blue" OnClick="changephoto_Click">Change Photo</asp:LinkButton><br /><br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="changephoto" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
on click event
Protected Sub changephoto_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MultiView_Updates.SetActiveView(View_ChangePhoto)
'bindprofileimage()
End Sub

put the multiview inside the updatepanel.....

If you want set view on click of the linkbutton which is placed inside the update panel, then your Multiview must also be present inside the update panel. Otherwise, the control will not be refreshed.
UpdatePanel controls work by specifying regions of a page that can be updated without refreshing the whole page. An asynchronous postback behaves like a regular postback in that the resulting server page executes the complete page and control life cycle. However, with an asynchronous postback, page updates are limited to regions of the page that are enclosed in UpdatePanel controls and that are marked to be updated. The server sends HTML markup for only the affected elements to the browser.
Using Nested UpdatePanel Controls
UpdatePanel controls can be nested. If the parent panel is refreshed, all nested panels are refreshed also.
The following example shows markup that defines an UpdatePanel control inside another UpdatePanel control. A button in the parent panel triggers an update of the content in both the parent and the child panel. The button in the child panel triggers an update of only the child panel.

Related

UpdatePanel breaks after adding UpdateProgress

I have some code that contains a panel where I put an ASP chart from codebehind. Below the panel I placed a button with a postback event that changes some parameter of that chart.
Here is a simplified version of the code:
...
<asp:UpdatePanel ID="udp" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl_chart" runat="server" />
<asp:Button ID="btn_chart" UseSubmitBehavior="false" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
...
And the code behind:
Private Property AltChart As Boolean
Get
If Me.ViewState("simulador_avanzado") Is Nothing Then
Return False
End If
Return ToBool(Me.ViewState("simulador_avanzado"))
End Get
Set(value As Boolean)
Me.ViewState("simulador_avanzado") = value
End Set
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
...
If IsPostBack AndAlso ToStr(Me.Request("__EVENTTARGET")).Contains("btn_chart") Then
AltChart = Not AltChart
End If
Dim cht as New Chart
'Build the chart...'
pnl_chart.Controls.Add(cht)
...
End Sub
This works like a charm, but when I add an UpdateProgress the PostBack stops working. Whenever I click the button, the UpdateProgress is shown for 2-3 seconds (exactly what it takes to do the PostBack) but then it hides and the UpdatePanel content isn't refreshed.
This is the new code:
...
<asp:UpdatePanel ID="udp" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl_chart" runat="server" />
<asp:Button ID="btn_chart" UseSubmitBehavior="false" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="udpro" AssociatedUpdatePanelID="udp" DynamicLayout="false" runat="server">
<ProgressTemplate>
<div class="udp_progress">
</div>
</ProgressTemplate>
</asp:UpdateProgress>
...
I debugged the partial postbacks and the code behind is working as intended, it updates the AltChart variable and the chart itself.
To put it simple: UpdatePanel partial postback works perfectly, but when I add an UpdateProgress bound to it, its content isn't updated on partial postbacks.
Update: I'm getting the next client error:
SCRIPT5022: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_Body_ctl02_udp_button'. If it is being updated dynamically then it must be inside another UpdatePanel.
Apparently the js generated by the UpdateProgress is being cached. The UpdatePanel with ID udp_button is from a previous version of the code and it's no longer there (I had the button outside the UpdatePanel and I wrapped it with its own UpdatePanel to make the UpdateProgress aware of the async postback, following this example, now I simply put it inside the chart's UpdatePanel).
Clearing the browser cache does not seem to work (as well as using a different browser).
UPDATE 2: OK, I was mistaken.
udp_button is another UpdatePanel present on the page, it's inside a user control and I hadn't noticed it. I'll try to place some conditional UpdateModes on both UpdatePanels to see if I can prevent the UpdateProgress from updating the wrong UpdatePanel.
I did what I mentioned on the Update 2 and it worked!
Apparently, the javascript code generated by the UpdateProgress control tries to update all UpdatePanels on the page affected by the async postback, and couldn't find an UpdatePanel that was inside a web user control (the ID it was using to look for the panel didn't match the panel ClientID).
Solution: Establish the property UpdateMode="Conditional" for the UpdatePanel inside the web user control, and make it update only when it's needed. This way the web user control isn't updated during the async postback generated by the button, and the UpdateProgress' javascript doesn't try to find it.

How do I force asp.net page to postback when I'm in code behind that called by UpdatePanel

When an UpdatePanel Calls a method in server and for example this method change
textBox1.Text = "12312"
and this textBox1 was out of updatePanle scope
it dosn't change it text till a postback happend to the page
so I need after that calculation and chaging the Textbox's text in the server, I need to forcepage to do postback
plz can any one help ?
If you want to force a refresh you could try: Response.Redirect(Request.Url.AbsoluteUri)
This should force a redirect to the current page.
Hope this helps
If you wish for a control within the UpdatePanel to perform a standard postback, define a PostBackTrigger within the UpdatePanel, setting the ControlID to the ID of the control you wish to perform the postback.
<asp:UpdatePanel ...
...
<Triggers>
<asp:PostBackTrigger ControlID="ControlIDToPostBack" />
</Triggers>
</asp:UpdatePanel>
Or you could add the TextBox control you wish to update to another UpdatePanel setting both of the UpdatePanel's UpdateMode properties to Always.
This will ensure that the content within both UpdatePanel controls is updated for all postbacks that originate from the page. Which includes asynchronous postbacks.
<asp:UpdatePanel ... UpdateMode="Always"

Asp.net Update Panel

I am working in asp.net 3.5 and have some issue of update panel.I have some html content and Formview control in a page. I want to hide the html content when there is no data in the Formview and to show when there is data in the Formview control. My Formview control is in the updatepanel and it it is bind to SqlDataSource and Formview binds on some criteria.
If somebody know how to solve this issue then please help me.
If you want the HTML content to be hidden after an Ajax postback, it will need to be in an UpdatePanel, either the same one your FormView is in, or a separate one.
If it's in a separate one, you'll need some way of forcing it to update when the FormView updates. You can either do this in some code-behind, calling the Update() method of the UpdatePanel, or by adding an AsyncPostBackTrigger to the UpdatePanel containing the HTML, maybe using the DataBound event of the FormView.
Also, to easily hide the HTML content, put it in a Panel, and use the Visible property.
An example of the trigger:
<asp:UpdatePanel ID="updatePanel" ... runat="server">
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="formViewId" EventName="DataBound" />
</Triggers>
</asp:UpdatePanel>
This issue simply resolved using the putting all the html content in the Updatepanels and on UpdatePanel_preRender event of Updatepanels check that if Gridview has data then show html content otherwise hide

Refresh a control on the master page after postback

What i am trying to do here is to show a couple of validation messages in form of a bulletlist, so i have a Div on my master page containing a asp:bulletlist. Like this:
<div>
<asp:BulletedList ID="blstValidationErrorMessage" runat="server" BulletStyle="Disc">
</asp:BulletedList>
</div>
When i then click the Save button from any of my pages (inside the main contentPlaceHolder) i create a list of messages and give this list as datasouce like this:
blstValidationErrorMessage.DataSource = validationMessageCollection;
blstValidationErrorMessage.DataBind();
The save button is located inside an updatepanel:
asp:UpdatePanel runat="server" ID="UpdatePanel" ChildrenAsTriggers="true" UpdateMode="Conditional">
Nothing happens, i can see that the datasource of the bulletlist contains X items, the problems must arise because the Save button is inside an update panel and the elements outside this updatepanel (master page controls for example) is not refreshed.
So my question is, how do i make the bulletlist refresh after the postback?
Thanks in advance.
If your button is inside an UpdatePanel, you should place your BulletedList control inside an UpdatePanel too.
You can place an UpdatePanel surrounding the BulletedList in the MasterPage file. Set "UpdateMode" to "Conditional" then call the Update method of the UpdatePanel to refresh only when needed ('save button' click for example).
The Save button will only update the contents of the UpdatePanel you placed it in. Here's what I recommend doing:
Move the SaveButton outside of the UpdatePanel. Where you put it I'll leave up to you.
Put your validation div inside another UpdatePanel. Call it ValidationUpdatePanel
Add your SaveButton as an AsyncPostbackTrigger for both update panels. Since you may have each UpdatePanel separated into different controls/pages, you'll probably want to do this in the code-behind programmatically.

Trigger an update of the UpdatePanel by a control that is in different ContentPlaceHolder

I have a page with two ContentPlaceHolders. One has a DropDown and another UpdatePanel with content.
How can I trigger update to the UpdatePanel by the DropDown's selectedItemChanged event when they are in different ContentPlaceholders?
The following would not work since UpdatePanel1 doesn't know about DropDown1:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
Some content that needs to be updated here...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDown1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
One way is to make an ajax page method that would be called by javascript on the page when DropDown's item is selected. Then in code behind, inside that page method, call UpdatePanel1.Update().
Is there an easier alternative?
From http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger.aspx
The control that the
AsyncPostBackTrigger references must
be in the same naming container as
the update panel for which it is a
trigger. Triggers that are based on
controls in other naming containers
are not supported.
The workaround is to use the UniqueID of the control that the
trigger is referencing. Unfortunately the UniqueID isn't qualified
until the control has been added to its parent (and its parent
has been added to its parent, all the way up the control tree).
In your code behind, try:
UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger()
{
ControlID = DropDown1.UniqueID,
EventName = "SelectedIndexChanged", // this may be optional
});
In the code-behind file, you should be able to do:
ScriptManager.RegisterAsyncPostBackControl(dropdown1);
You can enforce update any of page UpdatePanels by call updatePanel1.Update() method on server side.
For example during update updatePanel1 on button1.Click call updatePanel2.Update() and both panels will be updated.

Resources