jeudi 15 juin 2017

ASP.NET Checkbox not being checked

I will try to explain it the best I can, cause this problem is maybe the rarest I've found.

I've a control that extends the functionality of asp.net GridView. Inside it, I've the following two columns:

<asp:TemplateField HeaderText="Enviado GesDoc">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Enviado GesDoc2">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc2" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

As you can see, both ItemTemplates are identical. I don't reference them on any part of the solution but in the code behind on row_dataBound event. Inside this method, I do the following for both text boxes:

chk = e.Row.FindControl("chkEnviadoGesDoc") as CheckBox;
    if (chk != null)
    {
        chk.Enabled = false;
        chk.Checked = (rowData["REI_ENVIO_GESDOC"].ToString().Contains("S"));
    }

    chk = e.Row.FindControl("chkEnviadoGesDoc2") as CheckBox;
    if (chk != null)
    {
        chk.Enabled = false;
        chk.Checked = (rowData["REI_ENVIO_GESDOC"].ToString().Contains("S"));

    }

This is the result I get on the web:

enter image description here

Senseless. If I inspect the code, even if the checkboxes are not marked, they've the property "checked = checked". If, for instance, I add a column before the first ItemTemplate:

<asp:TemplateField HeaderText="YouText">
    <ItemTemplate>
        Prueba
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enviado GesDoc">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Enviado GesDoc2">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc2" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

Now the result is:

enter image description here

As you can see, none of the checkboxes are checked, even they've the property set in html.

If I add this "TestColumn" in middle of my both identical columns, then they work correctly:

<asp:TemplateField HeaderText="Enviado GesDoc">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="YouText">
    <ItemTemplate>
        Prueba
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enviado GesDoc2">
    <ItemTemplate>
        <asp:CheckBox ID="chkEnviadoGesDoc2" runat="server" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

enter image description here

I don't get any exception while running the code. Html is the same for the case when the checkbox is marked with the tick and when it's not. I don't have any idea what can cause this behavior. If you have any though, please share it and help me.

Thank you




Aucun commentaire:

Enregistrer un commentaire