atc
atc

Reputation: 621

Hide item template in aspx page based on condition

I have following item template in my listview

<ItemTemplate>
    <asp:LinkButton ID="ibtnEdit" runat="server" Height="20px" Width="20px" ToolTip="Edit  this Category" CommandArgument='<%#  Eval("Category_ID")  %>' CommandName="EditObject"  CausesValidation="False"> <i class="glyphicon glyphicon-edit"></i>&nbsp;</asp:LinkButton> 
</ItemTemplate>

I want to hide this column based on following condition. This status field contain either 1 or 0.

<%#    Bind("Status") %>

I am not getting an idea how to do

Upvotes: 3

Views: 2774

Answers (3)

benicillin
benicillin

Reputation: 101

You can just set the Visible property on the link button like this:

Visible='<%# Iif(Eval("Status")=1,True,False) %>'

Note you want to use Eval not Bind to do a one way data read.

Upvotes: 0

atc
atc

Reputation: 621

I tried with following code and it works

 <asp:LinkButton ID="ibtnEdit" runat="server" Height="20px" Width="20px" ToolTip="Edit  this Category"
                        CommandArgument='<%#  Eval("Category_ID")  %>' Visible='<%# DecideHere((int)Eval("Status")) %>'  CommandName="EditObject"
                        CausesValidation="False"> <i class="glyphicon glyphicon-edit"></i>&nbsp;</asp:LinkButton> 

In code behind

 protected bool DecideHere(int id)
             {
                if (id == 1 )
                    return true;
                else
                    return false;
             }

Upvotes: 2

Dharam Rai
Dharam Rai

Reputation: 81

@aniltc Do you also want to make it visible basing on the condition 0 and 1?

Try This, but not tested, logic will be similar I believe:

    if (txt_Status.Text != "")
        {
            string a, b;
                a=0;
                b=1;
        if (txt_Status.Text==a)
                    {

                        LinkButton lnkEdit = [YourDatalist].FindControl("ibtnEdit") as LinkButton;
                        lnkEdit.Visible = false;
                    }
         else if (txt_Status.Text == b)
                    {

                        LinkButton lnkEdit = [YourDatalist].FindControl("ibtnEdit") as LinkButton;
                        lnkEdit.Visible = true
                      }

           }

Upvotes: 0

Related Questions