Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

The Curious Case of the RadioButtonList not Triggering

The Curious Case of the RadioButtonList not Triggering

  • Comments 1

A question from the London Roadshow this time - why is it this code works:

<%@ Page Language="C#" %>

<html>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  { Label1.Text = new Random().Next(1, 100).ToString(); } 
</script>

<body>
  <form id="form1" runat="server">
  
    <asp:ScriptManager runat="server" />

      <asp:UpdatePanel runat="server">
        <ContentTemplate>
          <asp:RadioButtonList ID="RBL1" runat="server" AutoPostBack="True">
            <asp:ListItem>One</asp:ListItem>
            <asp:ListItem>Two</asp:ListItem>
            <asp:ListItem Selected="True">Three</asp:ListItem>
          </asp:RadioButtonList>
          <asp:Label ID="Label1" runat="server"></asp:Label>
        </ContentTemplate>
      </asp:UpdatePanel>

  </form>
</body>
</html>

But this code doesn't:

<%@ Page Language="C#" %>

<html>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  { Label1.Text = new Random().Next(1, 100).ToString(); } 
</script>

<body>
  <form id="form1" runat="server">
  
    <asp:ScriptManager runat="server" />

      <asp:RadioButtonList ID="RBL1" runat="server" AutoPostBack="True">
        <asp:ListItem>One</asp:ListItem>
        <asp:ListItem>Two</asp:ListItem>
        <asp:ListItem Selected="True">Three</asp:ListItem>
      </asp:RadioButtonList>
      
      <asp:UpdatePanel runat="server">
        <ContentTemplate>
          <asp:Label ID="Label1" runat="server"></asp:Label>
        </ContentTemplate>
        <Triggers>
          <asp:AsyncPostBackTrigger ControlID="RBL1" EventName="SelectedIndexChanged" />
        </Triggers>
      </asp:UpdatePanel>

  </form>
</body>
</html>

The only difference is whether the RadioButtonList is inside the UpdatePanel or associated with the UpdatePanel via an AsyncPostBackTrigger. In the first example the page works as expected and the Label control updates each time a difference RadioButton is selected. In the second example, all seems to work fine until you select the RadioButton that was originally selected when the page was rendered. Nothing happens. In fact no AsyncPostBack is generated. Steve Marx has a thorough explanation on his blog.

Technorati tags: ,