Reputation: 39
How to excess 'imginfo' in the server side?Please help.I have to hide the image if the login info does not match.
Upvotes: 0
Views: 58
Reputation: 4587
So, you want to hide the image if certain data has certain value. You, can simply compare the value and apply a css
class as hidden to that element like:
First, add this css
in the head
element.
.d-none {
display: none !important;
}
Then replace your image control with this markup:
<asp:ImageButton runat="server" ID="imgInfo" CssClass='<%# Eval("SomeColumn") == DBNull.Value ? "d-none" : "" %>' ImageUrl="~/Images/info-note.png" tooltip='<%# Eval("user_address").ToString().Trim() %>' style="position: center; top: 3px; padding-right: 3px; padding-left:5px;cursor: help;" />
You can replace SomeColumn
with the database column that contains your value to be compared and I just compared if it was null, you can do other comparisons as well.
UPDATE
You can add another clause in the comparison and we can use string.IsNullOrEmpty()
method to check if the varchar
column is empty.
<asp:ImageButton runat="server" ID="imgInfo" CssClass='<%# Eval("SomeColumn") == DBNull.Value || string.IsNullOrEmpty(Eval("SomeColumn").ToString()) ? "d-none" : "" %>' ImageUrl="~/Images/info-note.png" tooltip='<%# Eval("user_address").ToString().Trim() %>' style="position: center; top: 3px; padding-right: 3px; padding-left:5px;cursor: help;" />
Upvotes: 1
Reputation: 599
You can access the control at a row level in the function given to OnItemDataBound of the list view.
protected void lvLoginDetails_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item is ListViewDataItem)
{
ImageButton imb = (ImageButton)e.Item.FindControl("imgInfo");
// ...
}
}
Edit 1
If you want to access it anywhere else you will have to iterate through the listView items like below. Make sure u have bound some data before trying to access the control.
foreach (ListViewItem item in lvLoginDetails.Items)
{
ImageButton imb = (ImageButton)item.FindControl("imgInfo");
// ...
}
Upvotes: 1