How to excess image control in server side in asp.net app?

How to excess ‘imginfo’ in the server side?Please help.I have to hide the image if the login info does not match.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;" />

Method 2

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");
    // ...
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x