How do you use an UpdatePanel properly? - c#

I have an UpdatePanel with some checkboxes in it. I check them, and hit my Save button, but that causes the UpdatePanel to postback (refresh) and sets them all back to blank. The re-drawing method runs before the button code.
What is the correct way to have an UpdatePanel with checkboxes in that you can manipulate?

Example of code:
<asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="updatePanel1">
<ContentTemplate>
<asp:CheckBox runat="server" ID="myCheckBox" Caption="CheckBox"/>
<asp:Button runat="server" ID="saveButton"
Caption="Save" OnClick="SaveButtonClick"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="saveButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Make sure that:
UpdateMode of UpdatePanel is Conditional
SaveButton contained in Triggers-section as ControlID of AsyncPostBackTrigger

Your code behind should look like:
if(!page.ispostback)
{
re-drawing();
}
As when you hit Save button your re-drawing() method is called and it again refreshes your checkboxes. Asynchronous postback behaves and hit to page method the same as full postback, but refreshes the values in any updatepanels.
Also check this URL
http://ajax.net-tutorials.com/controls/updatepanel-control/

Make sure the Save button is inside the Update Panel, for a start, and if not, that is designated as a Trigger for the Update Panel, in the <Triggers> section of the Update Panel.
<asp:UpdatePanel ID="MyControlPanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="SaveButton" />
</Triggers>
<ContentTemplate> ...
Can you show some code for your UpdatePanel?

If you use server controls to render checkboxes you should add EnableViewState="true" attribute to these controls and update panel.
If you have checkboxes that are not server controls, then remove them from update panel, include only server controls inside. This leads to keeping several updates panel on a page that's usually not a big issue.

Add a ScriptManager object to your page if you do not have one. Set EnablePartialRendering="true". Put your UpdatePanel anywhere else on the page and place the content you want ajaxified within a tag within your UpdatePanel.

Related

Change text in UpdatePanel

I am new with ASP.NET. This is my code:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button Text="Change" runat="server" ID="BtnChangeText" OnClick="BtnChangeText_OnClick"/>
<asp:Label runat="server" ID="LblTest" Text="Change me!"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
This is my server code:
protected void BtnChangeText_OnClick(object sender, EventArgs e)
{
LblTest.Text = "Hello World!";
}
Why does it not work?. How can I do for this work?
Thank in advance!
Add this after your </ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Change" EventName="Click" />
</Triggers>
You have set the updateMode property to Conditional so it will the updatePanel won't be refreshed automatically (as this was default)!!
Simpel solution set it back to Always
MSDN UpdateMode
Always
The content of the UpdatePanel control is updated for all postbacks that originate from the page. This includes asynchronous postbacks.
Conditional
The content of the UpdatePanel control is updated under the following conditions:
If the Update method of the UpdatePanel control is called explicitly.
If a control is defined as a trigger by using the Triggers property of the UpdatePanel control and causes a postback. In this scenario, the control is an explicit trigger for updating the panel content. The trigger control can be either inside or outside the UpdatePanel control that defines the trigger.
If the ChildrenAsTriggers property is set to true and a child control of the UpdatePanel control causes a postback. In this scenario, child controls of the UpdatePanel control are implicit triggers for updating the panel. Child controls of nested UpdatePanel controls do not cause the outer UpdatePanel control to be updated unless they are explicitly defined as triggers.
See MSDN for further details!

How to find child page control while using postback trigger

I am having one master page that has one update panel.
Content place holder is within the update panel
Now i have child page which has a File upload control
To make work File upload control, i have to put Postback trigger.
But question is where i can put that Postback trigger ??
If i put Postback trigger in Master page than it will give me error that control does not found
and i can't put Postback trigger because child page has not other update panel
What is the solution for this problem ?
Simply wrap the FileUpload with an UpdatePanel, which don't do anything and hasn't any side effect but will solve the problem.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Button" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>

ImageButton PostBack Issue

I have created a custom UserControl in Asp.Net. This user control contains an asp:ImageButton wrapped in an UpdatePanel with a ScriptManagerProxy in the UserControl and a parent ScriptManger on the main page that contains the asp:Panel these custom UserControls are added to (renders as a list like object with items). I want to suppress the click from the ImageButton (contained in the UpdatePanel) to NOT fire PostBacks that is causing the page to be refreshed. Here is a snippet of the UpdatePanel wrapped ImageControl within the custom UserControl:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CatImage" EventName="Click" />
</Triggers>
<ContentTemplate>
<fieldset>
<asp:ImageButton ID="CatImage" runat="server" Height="128px" Width="128px" ImageUrl="../Handlers/ImageResizer.ashx?imgpath=../PhotographicImages/noImageFound.png&w=180&h=180" OnClick="CatImage_Click"/>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
On the top of the main page that dynamically loads these UserControls to its asp:Panel is:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
This works ONLY for every click AFTER the first click... Meaning the first click causes a PostBack on the main page, but any subsequent click is suppressed.
When this ImageButton is clicked, i fire an event in the CodeBehind to display a "LightBox" object with a larger version of the image.
What is wrong with this code? How can i suppress this first click from causing a PostBack to my parent page?

ajaxToolkit:AjaxFileUpload not working when updatepanel in same page is triggered

I have the following situation:
A master page with a usercontrol inside an update panel that triggers each minute by a timer.
In a content page i have an ajaxToolkit:AjaxFileUpload which have the functionality drag and drop, at page load the upload control is working fine but after the first trigger on the update panel the control stops working.
Thanks in advance.
I was able to replicate the issue and solve it by placing the AjaxFileUpload control inside of the ContentTemplate node of an UpdatePanel.
If you place it in its own UpdatePanel, make sure the UpdateMode is set to "Always". If you want it to be "Conditional", you'll need to have it be updated with the trigger in some way.
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
<ContentTemplate>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

How can I prevent a page from going to the top every time a button with OnClick event is clicked?

How can I prevent a page from going back to the top every time a button is click? I still want to execute the code on the "OnClick" event for the button.
Example:
Almost all of our forms have a button on the bottom of the page which are suppose to bring some data back from the DB and populate some labes and textboxes located on the bottom of the page, when the button is click it takes user back to the top of the page. How can I prevent this from happening?
Any help will be really appreciate it.
Assuming the OnClick event is handled on the server and not in javascript, the easiest thing is wrap the controls to be updated in an UpdatePanel and have the update panel trigger on the button's click event.
Here is an example from Microsoft's documentation:
<asp:Button ID="Button1"
Text="Refresh Panel"
runat="server" />
<asp:ScriptManager ID="ScriptManager1"
runat="server" />
<asp:UpdatePanel ID="UpdatePanel1"
UpdateMode="Conditional"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<fieldset>
<legend>UpdatePanel content</legend>
<%=DateTime.Now.ToString() %>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
There are two options:
Use ASP.NET Ajax, in particular the UpdatePanel control
Set MaintainScrollPositionOnPostback to true on page level or in web.config
in your page declaration you can add MaintainScrollPositionOnPostback="true" ie:
<%# Page Language="C#" ... MaintainScrollPositionOnPostback="true" %>
you can also put that in your web.config under system.web or declare it from codebehind Page.MaintainScrollPositionOnPostback=true;
In the function handler for the button make sure you return false. You might be actually posting the page with those buttons rather than just doing something on the click event.

Categories