Radiobutton returning selected status only

Asked

Viewed 838 times

0

Follows the code:

<asp:RadioButtonList ID="rdlCpfCnpjAvalista" CssClass="cPFCNPJRadioButtonList" runat="server" RepeatDirection="Horizontal" AutoPostBack="false">
                        <asp:ListItem Text="CPF" Value="1" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="CNPJ" Value="2" Selected="True"></asp:ListItem>
                    </asp:RadioButtonList>

Any choice I make, it just brings me the value = 1. These selected = true, are my attempts.

int teste = int.Parse(rdlCpfCnpjAvalista.SelectedValue);

Test is always equal to 1.

  • what do you have Selected="True"? It thus fetches the value of the first selected, in this case, the value 1

  • But if I leave nothing is happening the same thing. This Selected, as I said, are already the tests that I have been doing.

  • I just tested, it worked normal, where you’re putting this event to capture the value?

  • At the Click of the Button.

  • Tested here: http://i.stack.Imgur.com/2ME3B.png

  • With me it’s not working. Only comes Value = 1

  • I solved it. The question and I don’t know why, when I posted it here, the boxes were reversed. It’s just how are the names. should be this rdlCPCNPJAvalist and the colleague left so: rdlCpfCnpjAvalist

Show 2 more comments

1 answer

1

as reported by @Laerte, I performed the test below and worked perfectly.

Aspx

<form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="rdlCpfCnpjAvalista" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Text="CPF" Value="1" />                
            <asp:ListItem Text="CNPJ" Value="2" />
        </asp:RadioButtonList>
        <asp:Button ID="btnEnviar" runat="server" Text="Enviar" OnClick="btnEnviar_Click" />
    </div>
</form>

C#

protected void btnEnviar_Click(object sender, EventArgs e)
{
    // Verificando se algum valor foi selecionado.
    if (this.rdlCpfCnpjAvalista.SelectedIndex == -1) return;

    // Recuperando valor selecionado.
    var valor = this.rdlCpfCnpjAvalista.SelectedValue;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.