Variable is always valuable, when it should be null at certain 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 those records. This checkbox it appears in a certain situation. The first time the credit analyst will do his analysis he appears, with the word "Make Required." That's his text. When the analyst checks and returns to stores, the retailer can see this document that the analyst has made mandatory. By uparing this document and returning it to the analyst, it now appears to the analyst no longer as a Checkbox (Make Required) plus two RadioButtons: Approve and Disapprove. Well, it turns out that in this "i" position inside the repeater, var vchkTornarObrigatorio should be NULL, right? I thought of nulling all the variables at the end of FOR , but I found this a bit gambiarra, since I already said that I want the value of var in position "i" inside the repeater, according to the code posted. I think of initializing the var within FOR , but it also smells like a gambi, I'm not sure. It's a logic problem and I ask colleagues to solve this problem. How to null this var into 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>
    
asked by anonymous 26.02.2015 / 16:31

1 answer

1

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

Since all RepeaterItem has a CheckBox chkTornarObrigatorio , then it will be found in all interaction items, its idea is invisible or disabled.

EDIT

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

  

then in this case you should check the Visible property, a   chkTornarOriginal.Visible instead of vchkTornarOriginal! = null   should resolve

    
26.02.2015 / 17:17