ASP.Net - Browse Radiobuttonlist?

Asked

Viewed 356 times

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 that:

    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! ^_^";
        }
    }

Inside the Masterpage I’m using, after the tag <body>, possesses:

    <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’m 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 Radiobuttonlist?

  • The radioButtonList have the names as "rblResp01, rblResp02, ..." The problem would be that I do not find them

  • put all the code .ASPX please in your question, edit.

  • 2

    Is Bart the page??? also

  • rblResp01.SelectValue That’s not all @Bart?

  • @Marconi after having managed to access the control just do it! The problem was that I could not access it...

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;

Observing: pay close attention to where the parent control starts until you reach the desired control.

Own master page, this being the father of all controls, the first Findcontrol would be:

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

References:

  • The bootstrap tags will then influence?

  • 1

    @Bart only the controls, which has runat=server!

  • @Bart has MasterPage on that page?

  • 1

    yes, it’s a Web Form with Master Page

  • @Bart test the code with master page that I put, if it doesn’t work put for me everything that is inside the tag <Form>

  • Unfortunately it didn’t work. I added the codes to the question.

  • @Bar looks at the second example with master page was added the above element! remember gives a checked!

  • 1

    It worked! Needed to call . Findcontrol("Contentplaceholder1"). Thank you.

Show 3 more comments

0

The Findcontrol Method is not recursive, that is, it does not search at all levels of your page. Check if Radiobutton is inside a Panel or other 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");

}
  • I have involved the Radiobuttonlist for a Panel component of Asp.net and he is not finding this controlPai...

Browser other questions tagged

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