C# 如果选中数据库中的复选框值,则隐藏数据列表项

C# 如果选中数据库中的复选框值,则隐藏数据列表项,c#,html,asp.net,.net,checkbox,C#,Html,Asp.net,.net,Checkbox,我创建了一个数据列表,从我的sql数据库中提取图像和数据。我刚刚添加了一个复选框值,我的用户可以检查该项是否隐藏在列表中。如果未选中,则将显示 我能够找出如何显示所有项目,但即使在数据库中检查了项目,我也无法隐藏单个项目 以下是我到目前为止的情况: //this is the aspx page info that has the datalist <asp:CheckBox runat="server" ID="in

我创建了一个数据列表,从我的sql数据库中提取图像和数据。我刚刚添加了一个复选框值,我的用户可以检查该项是否隐藏在列表中。如果未选中,则将显示

我能够找出如何显示所有项目,但即使在数据库中检查了项目,我也无法隐藏单个项目

以下是我到目前为止的情况:

//this is the aspx page info that has the datalist
                                    <asp:CheckBox runat="server" ID="indicator" Enabled="False" Visible="false"/>

                                    <asp:Label runat="server" Font-Size="20px" Font-Bold="true" ID="EmptyMessage" Visible="false" ForeColor="#023AA7" Text="No Projects have been created yet, you be the first!" Style="font-style: italic; align-content: center; align-items: center"></asp:Label>
                                    <div style="margin-bottom: 80px"></div>
                                    <asp:DataList ID="DataListProjectImg" runat="server" RepeatColumns="4"
                                        RepeatDirection="Horizontal" CellSpacing="12" OnSelectedIndexChanged="DataListProjectImg_SelectedIndexChanged"
                                        DataKeyField="Id" CssClass="tblProject" ItemStyle-CssClass="tdProject">
                                        <ItemTemplate>
                                            <a href='<%#Eval("hrefPath") %>' runat="server">
                                                <em class="overflow-hidden">

                                                    <asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval("Image") %>' runat="server"
                                                        OnClick="ImageButton1_Click" CommandArgument='<%#Eval("Id") %>'></asp:ImageButton>
                                                    <a href="<%#Eval("hrefPath") %>"></a>
                                                </em>
                                            </a>
                                            <a href="<%#Eval("hrefPath") %>"></a>

                                        </ItemTemplate>
                                    </asp:DataList>


    private void LoadData()
    {
//where my sql command is located to pull data from
        var data = DataAccess.GetFutureProjects();
        Connection connection = new Connection();
        try
        {
//sql connection string class
            connection.connection1();
            SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl", connection.con);
            SqlDataReader dataReader = com.ExecuteReader();

            while (dataReader.Read())
            {
//where I get the value from the sql database to determine if the checkbox value for that particular item is checked
                indicator.Checked = (dataReader["Private"].ToString() == "Y");

            }
            dataReader.Close();
        }
        catch (Exception)
        {

            throw;
        }

//add link to each individual item, if clicked it will drill into the item displaying the user input
        data.Columns.Add("hrefPath");
        foreach (DataRow dr in data.Rows)
        {
            //link to the item's project information
            dr["hrefPath"] = "projects_future.aspx?ID=" + dr["Id"];
        }

        DataListProjectImg.DataSource = data;
        DataListProjectImg.DataBind();
    }
//这是包含数据列表的aspx页面信息
私有void LoadData()
{
//从中提取数据的sql命令所在的位置
var data=DataAccess.GetFutureProjects();
连接=新连接();
尝试
{
//sql连接字符串类
connection.connection1();
SqlCommand com=newsqlcommand(“从FutureProjTbl中选择图像、用户名、描述、私有、CompValue”,connection.con);
SqlDataReader=com.ExecuteReader();
while(dataReader.Read())
{
//其中,我从sql数据库获取值,以确定是否选中了该特定项的复选框值
indicator.Checked=(dataReader[“Private”].ToString()=“Y”);
}
dataReader.Close();
}
捕获(例外)
{
投掷;
}
//向每个单独的项目添加链接,如果单击,它将深入显示用户输入的项目
data.Columns.Add(“hrefPath”);
foreach(data.Rows中的DataRow dr)
{
//链接到项目的项目信息
dr[“hrefPath”]=“projects_future.aspx?ID=“+dr[“ID””;
}
DataListProjectionMg.DataSource=数据;
DataListProjectImg.DataBind();
}
因此,目标是存储我的数据,但如果选中每个项目项的复选框项,则单个项将隐藏,其余未隐藏的项将不隐藏


谢谢大家!

有多种方法可以做到这一点,但是看看您的示例,为什么不在数据库获取级别本身过滤结果呢

SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl WHERE Private == 'N'", connection.con);