ASP.Net - Browse RadioButtonList?

2

I have several RadioButtonList in a form, in the event of submitting the form I want to go through them and get the selected value.

I tried this:

    protected void btnEnviarQuestionario_Click(object sender, EventArgs e)
    {
        Resposta resposta = new Resposta();

        // criar a resposta a partir dos dados do usuario
        resposta.RA = Convert.ToInt32(dropdownAluno.SelectedValue);
        resposta.EscolaEnsinoFundamental = txtEscolaEnsinoFundamental.Text;
        resposta.Expectativas = txtExpectativa.Value;
        resposta.Linguagens = txtLinguagens.Value;

        // percorre as perguntas para criar o vetor de alternativas
        char[] alternativas = new char[14];
        for (int i = 0; i < 14; i++)
        {
            RadioButtonList rbl = FindControl("rblResp" + (i>9?"":"0") + i) as RadioButtonList;

            //if (rbl != null)
            //alternativas[i] = Convert.ToChar(rbl.SelectedValue);
        }

        resposta.Respostas = alternativas;

        // enviar resposta para o banco de dados
        _respostaBo = new RespostaBo();
        try
        {
            _respostaBo.EnviarQuestionario(resposta);
            lblStatus.Text = "Questionário enviado!";
        }
        catch
        {
            lblStatus.Text = "Erro ao enviar questionário! ^_^";
        }
    }

Within the MasterPage I'm using, after the <body> tag, it has:

    <form id="form1" runat="server">

    <nav class="navbar navbar-inverse">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="~/Home/Home.aspx" runat="server">
                    <img alt="Questionário" class="img-responsive" src="~/images/questionario.png" runat="server" />
                </a>
            </div>

            <p class="navbar-text">
                <asp:Label ID="lblNomeDaPagina" runat="server" Text="Nome da Página"></asp:Label></p>
        </div>
    </nav>


    <div class="container">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>

    <script src="../scripts/jquery-1.9.1.min.js"></script>
    <script src="../scripts/bootstrap.min.js"></script>
</form>

On the page I am using MasterPage:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:Panel ID="panelQuestionarios" runat="server">
    <div class="form-horizontal">
        <div class="form-group">
            <asp:Label ID="lblPergunta" CssClass="col-sm-2 control-label" runat="server" Text="1. Qual a pergunta?"></asp:Label>
            <div class="col-sm-10">
                <%-- Alternativas --%>
                <asp:RadioButtonList ID="rblResp01" runat="server">
                    <asp:ListItem Value="A">x</asp:ListItem>
                    <asp:ListItem Value="B">x</asp:ListItem>
                    <asp:ListItem Value="C">x</asp:ListItem>
                    <asp:ListItem Value="D">x</asp:ListItem>
                    <asp:ListItem Value="E">x</asp:ListItem>
                </asp:RadioButtonList>
            </div>
        </div>

   <!-- Mais 13 perguntas iguais acima, simplifiquei o código -->

        <div class="form-group">
            <div class="col-sm-2"></div>
            <div class="col-sm-10">
                <asp:Button ID="btnEnviarQuestionario" CssClass="btn-success btn" runat="server" Text="Enviar questionário" OnClick="btnEnviarQuestionario_Click" />
            </div>
        </div>


    </div>

    </asp:Panel>
</asp:Content>

rbl is getting null . How to select the RadioButtonList?

    
asked by anonymous 01.09.2017 / 14:00

2 answers

1

You need to search for the above element until you reach the innermost, example :

RadioButtonList radio = Page
    .FindControl("panelQuestionarios")
    .FindControl("rblResp01") as RadioButtonList;

with MasterPage :

RadioButtonList radio = Page
    .Master
    .FindControl("ContentPlaceHolder1")
    .FindControl("panelQuestionarios")
    .FindControl("rblResp01") as RadioButtonList;

If you have master page , which is the parent of all controls, the first FindControl would be:

Page.Master.FindControl("ID_DO_Elemento")
       .FindControl("ID_DO_Proximo_Elemento");

01.09.2017 / 14:31
0

The FindControl method is not recursive, that is, it does not search at all levels of your page. Check if the RadioButton is inside a Panel or another control that is a "Container", if this is the case you should look for the radioButton inside this control. For example:

var controlePai = this.FindControl("ControleQueEnvolveORadioButton");
for (int i = 0; i < 14; i++)
{
    RadioButtonList rbl = (RadioButtonList)controlePai.FindControl("rblResp01");

}
    
01.09.2017 / 14:11