Wednesday, October 15, 2008

Update Panel intricacies

While experimenting with multiple update panels on a single page there were certain weird things that I observed.
1.) Duplicate controls created: I had placed a label within an update panel like below:
< tr >
< asp:updatepanel id="UpdatePanel3" runat="server" updatemode="Always" rendermode="Block">
< contenttemplate>
< td colspan="2">
< asp:label id="Label1" runat="server" text="Test Label">
< /td>
< /contenttemplate>
< /asp:updatepanel>
< /tr>


And somewhere else in the page:

< tr>
< td style="WIDTH: auto">
< asp:dropdownlist id="DropDownList1" runat="server" autopostback="true">
< asp:listitem>One
< asp:listitem>Two
< asp:listitem>Three
< asp:listitem>Four
< asp:listitem>Five
< /asp:dropdownlist>
< /td>
< td style="WIDTH: auto">
< asp:updatepanel id="UpdatePanel2" runat="server" updatemode="Conditional">
< contenttemplate>
< asp:textbox id="TextBox1" runat="server">
< /contenttemplate>
< triggers>
< asp:asyncpostbacktrigger controlid="DropDownList1" eventname="SelectedIndexChanged">
< /triggers>
< /asp:updatepanel>
< /td>
< /tr


Now whenever an AJAX postback happened, the page will have two lables shown to it. I found that if put the property RenderMode="Inline" in UpdatePanel1, the label is no more duplicated. It would be worth noting that the "Block" RenderMode renders two labels, as well. In the source of the HTML I found that the "Inline" mode renders the UpdatePanel in < span>tags while the "Block" mode renders the UpdatePanel in < div>tags.

The problem goes away if the < td>is wrapped around the UpdatePanel. So after modifying the first HTML to

< td colspan="2">
< asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Always">
< contenttemplate>
< asp:label id="Label1" runat="server" text="Test Label">
< /contenttemplate>
< /asp:updatepanel>
< /td>


I do not need to explicitly set the RenderMode value to "Inline".

But why it happens? No idea.