Reputation: 545
I created a datalist that pulls images and data from my sql database. I just added a checkbox value that my users checks if they want the item to be hidden in the list. If it is not checked it will display.
I was able to figure out how to display all the items but even if the item is checked in the database I am not able to hide that individual item.
Here is what I have so far:
//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();
}
So the goal is have it so my data is stored but if the checkbox item for each project item is checked that individual item is hidden and the rest that aren't are not hidden.
Thank you!
Upvotes: 0
Views: 420
Reputation: 5771
There are various ways to do this, but looking at your example, why don't you just filter the results at the database fetching level itself.
SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl WHERE Private == 'N'", connection.con);
Upvotes: 2