Monday, March 26, 2012

ListBox error with UpdatePanel

Using a listbox in an update panel with visible= false and AutoPostBack=true causes an error in the foloiwng code below. If I remove either the AutoPostBack and instead use a button to PostBack it works fine or if I remove the Visible it works fine. My workaround is to set the style of the listbox to display:none instead of using Visible.

I'm using the June CTP

<formid="form1"runat="server">
<atlas:ScriptManagerID="scriptManager"runat="server"EnablePartialRendering="true"/>
<div>
<atlas:UpdatePanelID="upListBox"runat="server">
<ContentTemplate>
<asp:ButtonID="Button1"runat="server"Text="Button"OnClick="AddListItems"/>
<asp:ListBoxID="ListBox1"runat="server"OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"Visible="false"AutoPostBack="true">
<asp:ListItemText="one"/>
<asp:ListItemText="two"/>
<asp:ListItemText="three"/>
</asp:ListBox>
</ContentTemplate>
</atlas:UpdatePanel>
</div>
</form>

CODEBEHIND

publicpartialclassDefault2 : System.Web.UI.Page
{
protectedvoid ListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
ListBox1.Visible =false;
}

protectedvoid AddListItems(object sender,EventArgs e)
{
ListBox1.Visible =true;
}
}

Hi,

there's an issue with the UpdatePanel and the __doPostBack function that prevents it to be injected on the page if it is required by a control inside an UpdatePanel.

The solution is to force the server to drop the __doPostBack function. Add this statement in the Page_Load method:

Page.ClientScript.GetPostBackEventReference(this, String.Empty);

No comments:

Post a Comment