如果选中数据库中的复选框值,则隐藏数据列表项
Hide Datalist Item if checkbox value in database is checked
我创建了一个数据列表,从我的 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();
}
所以目标是存储我的数据,但如果选中每个项目项的复选框项,则单个项被隐藏,其余未隐藏的项则不会被隐藏。
谢谢!
有多种方法可以做到这一点,但看看你的例子,你为什么不直接在数据库提取级别本身过滤结果。
SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl WHERE Private == 'N'", connection.con);
我创建了一个数据列表,从我的 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();
}
所以目标是存储我的数据,但如果选中每个项目项的复选框项,则单个项被隐藏,其余未隐藏的项则不会被隐藏。
谢谢!
有多种方法可以做到这一点,但看看你的例子,你为什么不直接在数据库提取级别本身过滤结果。
SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl WHERE Private == 'N'", connection.con);