Monday, March 26, 2012

updating an updatepanel...

Hi.

I'm sure the update panel is not just a clever name, but, it isn't doing what it ways on the tin!!

UpdatePanel1: Houses a databound GridView
UpdatePanel2: TextBox1 and ListBox1.

I have a page with a search form on. The user submits search criteria which fire a conditional update of UpdatePanel1. The GridView re-binds itself perfectly! Now, I used to have some javascript which picked up the selected value of Listbox1 and dumped it out to TextBox1, it worked fine, until, I installed the latest release of the toolkit and now the javascript cannot find the form object.

No matter, I thought, I'll use an update panel and set it to conditional update and uses ListBox1.SelectedIndexChanged to fire the update, dumping the selected value into TextBox1. So I wrapped TextBox1 in UpdatePanel2 and run the code... nothing happened, until, I hit the search button which fires the update of UpdatePanel1. When I click this button both panels update?

So I thought I'd try putting ListBox1 outside of the UpdatePanel2... still no joy, exactly the same thing happened when the search button is clicked.

I then tried forcing the UpdatePanel2 to update in the code behind using, amazingly, UpdatePanel2.Update()... Guess what... nothing, until I hit the search button again.

I have double checked and both panels are setup as follows: UpdateMode="Conditional" ChildrenAsTriggers="False". My Script manager has Partial Rendering set to true. Both Update Panels have the correct triggers i.e UpdatePanel1 = Button1.click and UpdatePanel2 = ListBox1.SelectedIndexChanged

Anybody have any ideas? I can post the code up if required.

Thanks in advance.

I was able to recreate your example, with a listbox and textbox, populating the textbox on the listbox_SelectedIndexChanged event. At first, it would fire and fire and fire, but the textbox wouldn't update.

The kicker was changing ChildrenAsTriggers="True". See if that doesn't help.


Thanks Dugald.

I tried this but still no luck. Is your ListBox inside or outside the update panel?

I'm not convinced that the event is firing or not in al hoensty! I've tried adding an UpdateProgress template and adding sleep to the thread of 2 seconds but I still see nohting.

Just to confirm your setup.

ScriptManager: PartialRendering="True"
UpdatePanel: UpdateMode="Conditional" ChildrenAsTriggers="true "
ListBox: OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"

?


Have you tried setting a breakpoint in the event code to verify the event is firing?

Here is the code for my simple test:

<%@. Page Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True" UpdateMode="Conditional"> <ContentTemplate> <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"> <asp:ListItem>Alpha</asp:ListItem> <asp:ListItem>Bravo</asp:ListItem> </asp:ListBox> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /><br /> <br /> <asp:TextBox ID="TxtTarget" runat="server" ForeColor="Black"></asp:TextBox> </ContentTemplate> </asp:UpdatePanel> <asp:Button ID="Button1" runat="server" Text="Full Postback" OnClick="Button1_Click2" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </form></body></html>
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { }protected void Button1_Click1(object sender, EventArgs e) { }protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { TxtTarget.Text ="Changed";// ListBox1.SelectedValue; }protected void Button2_Click(object sender, EventArgs e) { TxtTarget.Text ="Pushed"; }protected void Button1_Click2(object sender, EventArgs e) { TxtTarget.Text ="Posted"; TextBox1.Text ="Posted"; }}

Ok, after adding a breakpoint on the ListBox1_SelectedIndexChanged event. I can see that the event is not even firing?
Try deleting the event and recreating it.

AHHHHHHHHH!!!!!!!!!!!!!!

Amazing what AutoPostBack="True" does!

Thanks for your help Dugald! Sorry for the Noob error...

No comments:

Post a Comment