1

I'm trying to find a linkbutton inside on my aspx page.

I don't want to find the originator, like the specific sender, I just want to find the overall linkbutton in the itemtemplate and make them all invisible.

Here is the ASPX code where the linkbutton is

<ItemTemplate>
                 <table>
                     <tr>
                         <td>
                             <asp:TextBox ID="DisplayOP" runat="server" Text="test" Visible="false"></asp:TextBox>
                         </td>
                     </tr>
                 <tr>
                    <td><asp:Linkbutton ID="ItemEmneLabel" runat="server" Text='<%# Eval("Emne") %>' OnClick="ItemShowComments" CommandArgument='<%# Eval("OpslagsID") %>' Visible="true" /></td>
<--- Code Omitted --->

In my code behind .cs file I try to find it like this:

var lnkb = (LinkButton)ListView1.FindControl("ItemEmneLabel");
            lnkb.Visible = false;

and this:

        var lnkb = (LinkButton)this.ListView1.FindControl("ItemEmneLabel");
        lnkb.Visible = false;

However whatever I try I get an error saying:

{"Object reference not set to an instance of an object."}

What am I doing wrong here?

Raker
  • 344
  • 2
  • 6
  • 15
  • 1
    Check this SO post [how-to-know-which-linkbutton-in-a-listview-was-clicked](http://stackoverflow.com/questions/8327665/how-to-know-which-linkbutton-in-a-listview-was-clicked) – haraman Nov 08 '15 at 00:06

2 Answers2

0

it's impossible to find a control declared in an ItemTemplate like this. You can only get the control with sender in ItemShowComments method.

Anıl Okay
  • 111
  • 6
  • Oh... that's a shame. The thing is I want to be able to click on a specific link button from a list. This will change the values of the select statement forcing it to create a new view on pagereload. However I want the buttons inside that view to be invisible. – Raker Nov 07 '15 at 22:20
0

Okay, so I found the solution to my problem.

Now I wanted to disable the visibility of the linkbuttons, when the listview was showing specific data. I kept looking for the answer in code behind, but found out that I could solve it in aspx.

What I did was add a statement to visibility like this:

Visible='<%# !Eval("Emne").Equals("") %>'

The exclamationmark (!) in front of Eval means, if it is NOT equal to "". This means that if "Emne" is not-empty, then the statement is true, thus visible is true. I could do it the other way around by coding it like this:

Visible='<%# Eval("Emne").Equals("") %>'
Raker
  • 344
  • 2
  • 6
  • 15