未检查ASP.NET复选框

时间:2017-06-15 07:03:54

标签: c# asp.net gridview checkbox

我会尽力解释它,因为这个问题可能是我发现的最罕见的。

我的控件扩展了asp.net GridView的功能。在其中,我有以下两列:

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

如您所见,两个ItemTemplates都是相同的。我不会在解决方案的任何部分引用它们,而是在row_dataBound事件的后面代码中引用它们。在此方法中,我对两个文本框执行以下操作:

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"));

    }

这是我上网的结果:

enter image description here

毫无意义的。如果我检查代码,即使没有标记复选框,他们也会检查属性&#34; checked = checked&#34;。例如,如果我在第一个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>

现在的结果是:

enter image description here

如果我检查HTML,请查看已选中的chebox和未选中的chebox的HTML:

<input name="ctl00$ContentPlaceHolder1$TabContainer1$TabPanel1$SeguimientoInterfaces$GrdInterfaces$ctl08$chkEnviadoGesDoc" disabled="disabled" id="ContentPlaceHolder1_TabContainer1_TabPanel1_SeguimientoInterfaces_GrdInterfaces_chkEnviadoGesDoc_6" type="checkbox" checked="checked">
<input name="ctl00$ContentPlaceHolder1$TabContainer1$TabPanel1$SeguimientoInterfaces$GrdInterfaces$ctl08$chkEnviadoGesDoc2" disabled="disabled" id="ContentPlaceHolder1_TabContainer1_TabPanel1_SeguimientoInterfaces_GrdInterfaces_chkEnviadoGesDoc2_6" type="checkbox" checked="checked">

如果我手动取消支票并再次检查,则检查有效:

jQuery("#ContentPlaceHolder1_TabContainer1_TabPanel1_SeguimientoInterfaces_GrdInterfaces_chkEnviadoGesDoc2_6").attr("checked", "")
jQuery("#ContentPlaceHolder1_TabContainer1_TabPanel1_SeguimientoInterfaces_GrdInterfaces_chkEnviadoGesDoc2_6").attr("checked", "checked")

如您所见,即使他们在html中设置属性,也不会选中任何复选框。

如果我添加这个&#34; TestColumn&#34;在我的两个相同列的中间,然后它们正常工作:

<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

运行代码时我没有遇到任何异常。当复选框用勾号标记时以及不勾选时,Html是相同的。我不知道会导致这种行为的原因。如果您有,请分享并帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

Question was solved myself. There was a function in javascript setting checked property manually to false (instead using attr function), that's why my checkbox was being displayed without check but still had the "checked" attr in html.

The internal coding of this function caused the stranged behaviors when adding a text column in between the checkboxes columns or before them.