如果选中数据库中的复选框值,则隐藏Datalist Item

时间:2015-04-13 15:39:33

标签: c# html asp.net .net checkbox

我创建了一个从我的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();
    }

所以我的目标就是存储我的数据,但是如果检查每个项目项的复选框项目,则隐藏单个项目,而不隐藏其余项目。

谢谢!

1 个答案:

答案 0 :(得分:2)

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

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