Update panel can not find the button which trigger it - asp.net

I have a button is inside a another table(s) inside the update panel.
<Update panel>
<ContentTemplate>
<table>
<table>
<Button>
<table>
<table>
</ContentTemplate>
</Update panel>
I would like to add a button to Update panel's trigger.
But am getting an err says "Update panel can not find the button which trigger it".
I am getting "Sys.Webforms.PageRequestmanagerParseErrorException: This message recieved from manager could not be parsed. Common cause for this error are when response is modified by response.write"
Please help!

PostBackTrigger example:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="Button 1-Partial Postback" />
<asp:Button ID="btn2" runat="server" Text="Button 2-Full Postback" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn2" />
</Triggers>
</asp:UpdatePanel>

Related

Make any button on the page as a trigger for an asp.net UpdatePanel other than its sibling or child postback controls

Consider the following code fragment:
<div>
<asp:Button runat="server" ID="trickyUPTrigger" Text="Tricky Update" />
<div>
<asp:Button runat="server" ID="normalUPTrigger" OnClick="normalUPTrigger_Click" Text="Normal Update" />
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="normalUPTrigger" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" ID="changeableLabel" Text="Change me"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
Now make the button with ID of trickyUPTrigger as the trigger of the UpdatePanel. Or, devise a mechanism (probably... using javascript?) so that when this button is clicked UpdatePanel updates without full page postback.
If you want to update the UpdatePanel when clicking on trickyUPTrigger, you can add that button to the triggers list:
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="normalUPTrigger" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="trickyUPTrigger" EventName="Click" />
</Triggers>
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
UPDATE
You asked some code examples showing cases with naming containers, a concept that comes into play for databound controls with item templates, like the GridView and the ListView, and for user controls. In the examples below, I use a ListView, where each item is a separate naming container.
If you wanted to trigger an update of your panel from a button in a ListView item template, the trigger would not be found at runtime, and an exception would occur:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger cannot be found!" OnClick="anotherTrigger_Click" />
</ItemTemplate>
</asp:ListView>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
The reverse case (the UpdatePanel in the ListView item template, the trigger button outside of the ListView) does work, according to my tests, which seems to contradict the note that you mention in your comment:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger works!" OnClick="anotherTrigger_Click" />
Finally, the case where the UpdatePanel and the trigger button are both in the ListView item template also works:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger works!" OnClick="anotherTrigger_Click" />
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:ListView>
You can notice that I set UpdateMode="Conditional" for the panel in this last example. With this setting, the panel is updated only by its own triggers. If the attribute is set to UpdateMode="Always", the panel is updated not only by his own triggers but also by the triggers of the other UpdatePanels in the page. The default value is UpdateMode="Always" (as in your code sample).

ASP.NET CheckBox Value doesn't change

I have the following code :
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional" Visible="true" RenderMode="Inline">
<ContentTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBoxTel" Text="Phone" runat="server" AutoPostBack="true" />
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CheckBoxTel" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
I would like to not refresh the page when a checkbox is checked or unchecked that's why I use a trigger but I'm new to update panel and trigger and I'm not sure why when I click on my submit button that refreshes the page, the checkbox keeps unchecked even if I checked it. Do I have to bind something to the button ?
Here is the button code :
<asp:Button ID="Search" runat="server" Text="Search" CssClass="btn btn-primary" OnClick="Search_OnClick" />

How to update the controls present in two different update panels using the click event of two different buttons

I am quite new to learning asp.net, so the question that I am asking may sound quite basic but I still need an answer as I am doing r&d with some of the toipcs.
I have 2 update panels in my page. Each contains a label. There are two buttons on my page, on the click event of the first button the label in the first update panel should get updated. On the click of the second button the label in the second update panel should get updated.
However when I click any of the two buttons both the labels get updated.
In the load event of the page the foll code is written
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
The code for both the update panel is as follows:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID= "Button1" EventName="click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="click" />
</Triggers>
</asp:UpdatePanel>
Remove the code from the page_load method and place it in the event of each of the buttons. So for button1 you put the code that updates label1 and for button2 you put the code that updates the label2.
You don't even need the triggers unless you're doing cross panel events. However, you do need to properly set up your script manager and update panels:
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional" OnLoad="Panel1_Load">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button1"/>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server"
UpdateMode="Conditional" OnLoad="Panel2_Load">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Button2"/>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Here you'd put your code in Panel1_Load(){} and Panel2_Load(){}.
Notice the settings: EnablePartialRendering="true"
and: UpdateMode="Conditional"
If you don't want the asp:Button controls on your panels, you'd have to put them somewhere that would block their normal full post-back behavior.

<asp:FileUpload with UpdatePanel

Im trying tp upload more than one image, and when each one I upload I will show it in a repeater, but in the code behind the FileUpload1.HasFile is always False , this is a piece of my code :
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Repeater ID="rpUploadedImages" runat="server">
<ItemTemplate>
<img src='../Images/<%# DataBinder.Eval(Container.DataItem, "ImagePath")%>'/><br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnupload" EventName="click" />
</Triggers>
</asp:UpdatePanel>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
The FileUpload control does not work with UpdatePanel, you will need to do a full post back to get the file on the server... Now there are a lot of tricks to make it ajaxy...
http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
Another tricky way is to create iframe with fileupload + submit button(or some trigger) inside your main form. iframe will postback with no effect to main page.

Refresh User Control without Refreshing the Page

I have a page and it has a button and a user control.
I want to refresh the user control without refreshing the page.
I know I cannot do it otherwise so what I did is wrapped my user control inside the Update Panel.
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Button ID="btnAdd" runat="server" Text="Add name to list" OnClick="btnAdd_Click" /><br /><br />
<asp:UpdatePanel ID="upShowNames" runat="server">
<ContentTemplate>
<uc1:ShowNames ID="ucShowNames" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
But I still the control won't refresh.
I also tried calling the update panels. The Update() method by changing its UpdateMode to Conditional but that does not work either...
Does anyone know how can I do it?
Please change these 2 things
<asp:UpdatePanel ID="upShowNames" runat="server" UpdateMode="Conditional">
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click"/>
You missed the EventName on the postback trigger, once you add that, it should work :-)

Resources