'OnItemDataBound in Datalist on how to show and hide labels and linkbuttons
I have a datalist with some labels and link button. I want to hide and show based on some stored procedure. The stored procedures are woprking fine in SQL Server but it does not execute the entire stored procedure apart from the last one only.Any help will be appreciated? Here is datalist code for sake of space couldn't copy the entire datalist code
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemDataBound="DataList1_ItemDataBound">
ALL AVAILABLE BOOKS
TITLE:
<asp:Label ID="Book_nameLabel" runat="server" Text='<%# Eval("Book_name") %>' />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Book_img_link") %>' Width="100px" Height="150px" />
. . . .
<div class="row">
<div class="col-md-7">
<b style="color: #686969">
<asp:Label ID="lblNote" Visible="false" runat="server" Text="Visit institution to read or borrow If a member"></asp:Label></b>
</div>
<div class="col-md-4 padding-right">
<b>
<asp:LinkButton ID="lnkbtnAddToCart" Visible="false" runat="server" CssClass="btn-outline-secondary"><i class="fa fa-shopping-cart"></i>  Add to Cart</asp:LinkButton></b>
<b>
<asp:LinkButton ID="lnkButtonRead" Visible="false" CssClass="btn-outline-secondary" runat="server">Read (free)</asp:LinkButton></b>
<b>
<asp:LinkButton ID="lnkkbtnRequest" Visible="false" CssClass="btn-outline-secondary" runat="server">On Request</asp:LinkButton></b>
<b>
<asp:Label ID="lblRequested" Visible="false" Style="color: #7d7d7d" runat="server" Text="Requested"></asp:Label></b>
</div>
</div>
</ul>
</li>
<li class="button" style="background-image:inherit; background-color:#7d7d7d"><b><a href="#" style="color:whitesmoke">Read Book Preview</a></li></b>
</ul>
</li>
</ul>
</div>
</div>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:eLibraryConnectionString %>" SelectCommand="SELECT * FROM [AllBooks]"></asp:SqlDataSource>
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
LinkButton lnkbtnAddToCart = (LinkButton)e.Item.FindControl("lnkbtnAddToCart");
LinkButton lnkkbtnRequest = (LinkButton)e.Item.FindControl("lnkkbtnRequest");
Label lblNote = (Label)e.Item.FindControl("lblNote");
Label lblRequested = (Label)e.Item.FindControl("lblRequested");
LinkButton lnkButtonRead = (LinkButton)e.Item.FindControl("lnkButtonRead");
if (GetBooksInLibrary())
{
lnkbtnAddToCart.Visible = false;
lnkkbtnRequest.Visible = false;
lblNote.Visible = true;
lblRequested.Visible = false;
lnkButtonRead.Visible = false;
}
if (GetBooksInStores())
{
lnkbtnAddToCart.Visible = false;
lnkkbtnRequest.Visible = true;
lblNote.Visible = false;
lblRequested.Visible = false;
lnkButtonRead.Visible = false;
}
if (GetOnlineFreeBooks())
{
lnkbtnAddToCart.Visible = false;
lnkkbtnRequest.Visible = false;
lblNote.Visible = false;
lblRequested.Visible = false;
lnkButtonRead.Visible = true;
}
if (GetOnlinBooksForSale())
{
lnkbtnAddToCart.Visible = true;
lnkkbtnRequest.Visible = true;
lblNote.Visible = false;
lblRequested.Visible = false;
lnkButtonRead.Visible = false;
}
}
}
bool GetOnlineFreeBooks() { try { SqlConnection con = new SqlConnection(Strcon); if (con.State == ConnectionState.Closed) { con.Open(); }
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "getOnlineBooksForFree";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//Checks if any record exists in the table for online free books and returns the entire rows with the record
if (dt.Rows.Count >= 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|