如何根据数据库中的数据检查我的复选框

时间:2017-03-22 12:57:49

标签: c# html asp.net

<asp:TemplateField HeaderText="CAT A or CAT C">
     <ItemTemplate>
        <asp:CheckBox ID="chkcata" OnCheckedChanged="chkcata_CheckedChanged" OnDataBinding="chkcata_DataBinding" runat="server" Text='<%# Eval("cat_a") %>' Checked='<%# Eval("cat_a").ToString().Equals("1")%>' />
     </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Port to Trikon">
     <ItemTemplate>
        <asp:CheckBox ID="chkport" Style="text-align: center" OnCheckedChanged="chkport_CheckedChanged" OnDataBinding="chkport_DataBinding" runat="server" Text='<%# Eval("Port_Trikon") %>' />
     </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="White Pages Listing? Y/N">
     <ItemTemplate>
        <asp:CheckBox ID="chkwhilepages" Style="text-align: center" OnCheckedChanged="chkwhilepages_CheckedChanged" OnDataBinding="chkwhilepages_DataBinding" runat="server" Text='<%# Eval("while_pages") %>' />
     </ItemTemplate>
</asp:TemplateField>

图像你可以看到哪个是我的gridview,其中给出了复选框。现在在执行我的filladd()函数的更新时间,即使它们是真的,也不会检查复选框,所以请帮助它.. !!

private void filladd(int p)
{
    DataTable dt = new DataTable();
    dt = b.get_subpaf(p);//MJ SP CHANGE
    if (dt.Rows.Count > 0)
    {        
        DataExtensionList.DataSource = dt;
        DataExtensionList.DataBind();
        ViewState["ExtensionId"] = dt.Rows.Count;
        ExtensionBind();
    }
}

2 个答案:

答案 0 :(得分:0)

<asp:CheckBox ID="chkport" runat="server" Checked='<%# DataBinder.Eval (Container.DataItem,"Port_Trikon").ToString()!="0"?true:false %>' />

确保数据库是BIT数据类型或只返回1,0字符串

答案 1 :(得分:0)

我得到了上面的答案,只是将旧函数替换为问题

private void filladd(int p)
{
    DataTable dt = new DataTable();
    dt = b.get_subpaf(p);//MJ SP CHANGE
    if (dt.Rows.Count > 0)
    {

        DataExtensionList.DataSource = dt;
        DataExtensionList.DataBind();
        foreach (GridViewRow it1 in DataExtensionList.Rows)
        {
            CheckBox chk = (CheckBox)it1.FindControl("chkcata");
            CheckBox chk1 = (CheckBox)it1.FindControl("chkport");
            CheckBox chk2 = (CheckBox)it1.FindControl("chkwhilepages");
            if (chk.Text == "True")
            {
                chk.Checked = true;
            }
            if (chk1.Text == "True")
            {
                chk1.Checked = true;
            }
            if (chk2.Text == "True")
            {
                chk2.Checked = true;
            }
        }
     }
}