如何隐藏asp.net中的linkbutton onload

时间:2015-11-30 11:42:23

标签: asp.net listview

我在listview中有一个链接按钮。我想知道如何在某些条件下隐藏此链接按钮。

我的代码

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return deleteConfirm();">    
   <img src="../Admin/Images/deletebtn.png"alt="deletebtn"  class="deleteimgbtn" id="dltbtn"/>
  </asp:LinkButton> 

protected void Page_Load(object sender, EventArgs e)
     {
       ListView1.FindControl("LinkButton1").Visible = false;//Iam tried by using this code,but doesn't work
     }

1 个答案:

答案 0 :(得分:0)

定义ListView模板时,它只是......模板,因此按钮尚不存在。因此,当每个项目被绑定时,您需要使用ListView.ItemDataBound事件来查找和禁用您的按钮。

<asp:ListView ID="MyListView" runat="server" OnItemDataBound="MyListView_ItemDataBound">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return deleteConfirm();">    
            <img src="../Admin/Images/deletebtn.png"alt="deletebtn"  class="deleteimgbtn" id="dltbtn"/>
        </asp:LinkButton> 
    </ItemTemplate>
</asp:ListView>
protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        var LinkButton1 = (LinkButton)e.Item.FindControl("LinkButton1");
        if (true) // insert your condition here
        {
            LinkButton1.Visible = false;
        }
    }
}