Variable is always with value, when it should be null at certain times

Asked

Viewed 109 times

0

I have this code: This rptAprovaDocumento is a Repeater

public List<ENTSISRegistroPendencia> RegistrarPendencia()
{
    CheckBox vchkTornarObrigatorio = null;
    for (int i = 0; i < rptAprovaDocumento.Items.Count; i++)
    {
        vchkTornarObrigatorio = (CheckBox)rptAprovaDocumento.Items[i].FindControl("chkTornarObrigatorio");

        if (vchkTornarObrigatorio != null)
        {
            if (!vchkTornarObrigatorio.Checked)
            {
                ventRegPendencia.IcDocObrigatorio = 0;
            }
            else
            {
                ventRegPendencia.IcDocObrigatorio = 1;
            }
        }
        else
        {
            ventRegPendencia.IcDocObrigatorio = 0;
        }    
    }
}

How it works. This repeater has a table that lists these records. This checkbox appears in a certain situation. The first time the credit analyst will make his analysis he appears, with the word "Make It Mandatory". This is his text. When the analyst checks and returns to the stores, the shopkeeper can see this listed document that the analyst made mandatory. By uploading this document and returning it to the analyst, it now appears to the analyst no longer as a Checkbox (Make It Mandatory) plus two Radiobutton: Approve and Fail. Well, it turns out that in that position of "i" inside the Peater, the var vchkTornarObrigatorio should come NULL, right? I thought to nullify all variables at the end of the FOR, but I found it a bit tricky, because I already said I want the value of the var in the "i" position inside the Peater, according to the code posted. I think about initializing the var inside the FOR, but I also smell Gambi, I’m not sure. It’s a problem of logic and I ask for help from colleagues to solve this junk. How to nullify this var inside the for.

<asp:Repeater ID="rptAprovaDocumento" runat="server" 
        onitemdatabound="rptAprovaDocumento_ItemDataBound"> 
        <HeaderTemplate>
            <table width="1000">
                <thead>
                    <th width="400">
                        Tipos de documento
                    </th>
                    <th width="150">
                    </th>
                    <th>
                    </th>
                </thead>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:HiddenField ID="hdfCdTipoDocumento" runat="server" Value='<%# Eval("CdTipoDocumento")%>' />
                    <asp:HiddenField ID="hdfCdPendencia" runat="server" Value='<%# Eval("CdPendencia")%>' />
                    <asp:HiddenField ID="hdfCdDocumento" runat="server" Value='<%# Eval("CdDocumento")%>' />
                    <asp:HiddenField ID="hdfIcAprovado" runat="server" Value='<%# Eval("IcAprovado")%>' />
                    <asp:HiddenField ID="hdfIcDocObrigatorio" runat="server" Value='<%# Eval("IcDocObrigatorio")%>' />
                    <asp:HiddenField ID="hdfCdMotivo" runat="server" Value='<%# Eval("CdTipoMotivoRecusa")%>' />

                    <%--<strong><a href="/UpLoads/<%# Eval("DsPathDocumento")%>" class="linkUpload"><%# Eval("NmTipoDocumento")%></a></strong>--%>

                    <strong><a <%# Eval("DsPathDocumento") != null && !String.IsNullOrEmpty(Eval("DsPathDocumento").ToString()) ? String.Concat("href='/UpLoads/", Eval("DsPathDocumento"), "'","class='linkUpload'") : "style='cursor: default; color:#000000;' class='disabled'" %>>
                        <%# Eval("NmTipoDocumento") %>
                    </a></strong>                      

                    <asp:Label ID="lblDtCriacao" Text='' runat="server" /><br />
                    <asp:Label ID="lblNmObrigatorio" Text='<%# Eval("NmTipoObrigatorio") %>' runat="server" Font-Bold="True" />
                </td>
                <td>
                    <asp:RadioButtonList ID="rbIctAprovado" CssClass="radiobuttonAprovaReprova" onclick="javascript:MostraEscondeControle(this);"
                        runat="server" AutoPostBack="false" TextAlign="left" RepeatColumns="0" RepeatDirection="Vertical"
                        CausesValidation="False" OnSelectedIndexChanged="rbIctAprovado_SelectedIndexChanged" RepeatLayout="UnorderedList">
                        <asp:ListItem Value="1">Aprovar</asp:ListItem>
                        <asp:ListItem Value="0">Reprovar</asp:ListItem>
                    </asp:RadioButtonList>
                    <asp:CheckBox ID="chkTornarObrigatorio" runat="server" Text="Tornar Obrigatório" />
                    <br />
                    <asp:DropDownList ID="cmbCdMotivoRecusa" runat="server" Enabled="false" AutoPostBack="false"></asp:DropDownList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="É necessário aprovar ou reprovar este documento para prosseguir."
                        ControlToValidate="rbIctAprovado" ValidationGroup="grupoAprovaReprova" Text=""></asp:RequiredFieldValidator>

                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
  • can post the Markup of these Repeater?

  • I edited and posted Markup.

  • I’m not seeing any condition that makes chkTornarObrigatorio inactive, invisible or nonexistent, as you’re hiding the same?

  • Could you put the code snippet where the chkTornarObrigatorio is disabled or removed?

  • It is not removed. Only it appears or not, according to what the analyst did. I think I should change my logic there in the case, ie, I will create a method that takes the situation of the document in a previous analysis, then yes, I know that it can not change its status ever again.

  • 1

    then in this case you should check the property Visible, one chkTornarObrigatorio.Visible in place of vchkTornarObrigatorio != null must solve

  • I will test and put. I didn’t even think about it. If you solve, post this comment as an answer to finish.

  • It worked in one part and related to the initial post solved, but does not conclude the problem, but is something else. Post your comment as an answer to end this post.

Show 3 more comments

1 answer

1


The command rptAprovaDocumento.Items[i].FindControl("chkTornarObrigatorio") as CheckBox simply returns the CheckBox chkTornarObrigatorio associated with the current item.

Like all the RepeaterItem have a CheckBox chkTornarObrigatorio, then it will be found in all the items of the interaction, idepedente it is invisible or disabled.

EDIT

As identified in conversation with the Questioner, we found a partial resolution.

then in this case you should check the Visible property, a chkTornarObrigatorio.Visible on the place of vchkTornarObrigatorio != null must solve

  • the correct one would be to nullify it at the end of the for or declare it at the beginning or change the logic.

  • you redeclare it at the beginning of the for or invalidate it at the end will not change the current behavior, because the method ...Item[i].FindControl("chkTornarObrigatorio") will always return a CheckBox, in short, your problem lies not in the misunderstanding of the for loop, but in the functioning of the Repeater.

  • Okay, I’m gonna have to rethink another way to do this, thanks.

Browser other questions tagged

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