André
André

Reputation: 783

UpdatePanel with RadioButtonList

I've been trying to learn how to use Update Panel but I'm not being able to make it show a different string in the label inside of a div on the Update Panel depending on the RadioButtonList choice.

                        <td class="right-td">
                            <asp:RadioButtonList ID="rdlUrgencia" runat="server" RepeatDirection = "Horizontal" AutoPostBack="true" OnSelectedIndexChanged="rdlUrgencia_SelectedIndexChanged">
                                <asp:ListItem Text="Sim" Value="1" />
                                <asp:ListItem Text="Não" Value="0" />
                            </asp:RadioButtonList>
                            <asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" RenderMode="Inline">
                                <ContentTemplate>
                                    <div id="divTexto" runat="server" visible="false">
                                        <asp:Label ID="labelSN" runat="server" Visible="false"></asp:Label>
                                    </div>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="rdlUrgencia" EventName="rdlUrgencia_SelectedIndexChanged" />
                                </Triggers>
                            </asp:UpdatePanel>
                            <br/>
                        </td>

And:

    protected void rdlUrgencia_SelectedIndexChanged(object sender, EventArgs e)
    {
        divTexto.Visible = true;
        labelSN.Visible = true;

        if (rdlUrgencia.Text == "Sim")
        {
            labelSN.Text = "Sim";
        }
        if (rdlUrgencia.Text == "Não")
        {
            labelSN.Text = "Não";
        }
    }

What am I doing wrong and how can I fix it?

Upvotes: 1

Views: 2446

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73731

The name of the event for the RadioButtonList trigger should be SelectedIndexChanged:

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" RenderMode="Inline">
    <ContentTemplate>
        <div id="divTexto" runat="server" visible="false">
            <asp:Label ID="labelSN" runat="server" Visible="false"></asp:Label>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="rdlUrgencia" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

As for the event handler in code-behind, you can use the SelectedValue of the RadioButtonList instead of the Text property:

protected void rdlUrgencia_SelectedIndexChanged(object sender, EventArgs e)
{
    divTexto.Visible = true;
    labelSN.Visible = true;

    switch (rdlUrgencia.SelectedValue)
    {
        case "0":
        {
            labelSN.Text = "Não";
            break;
        }

        case "1":
        {
            labelSN.Text = "Sim";
            break;
        }
    }
}

Upvotes: 1

Related Questions