简体   繁体   English

在后面的代码中设置链接的可见性

[英]Setting visibility of a link in code behind

i have this in source code: 我在源代码中有这个:

<a href="CreateAlbum.aspx" id="createalbumlink">                    
                Create New Album
            </a>

now i want to set its visibility in code behind. 现在我想在后面的代码中设置它的可见性。 How can i do so? 我该怎么办? I have this link within ListView Control. 我在ListView控件中有此链接。 I replaced the above with 我用上面的替换了

<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/CreateAlbum.aspx"> Create New Album1</asp:LinkButton>

still couldnt detect in codebehind. 仍然无法在代码隐藏中检测到。

SOurce code: 源代码:

   <form id="form1" runat="server">

    <asp:ListView ID="lvAlbums" runat="server"
            DataSourceID="SqlDataSource1" GroupItemCount="3" 
            InsertItemPosition="LastItem">            
            <LayoutTemplate>                
                    <table border="1">
                       <tr ID="groupPlaceholder" runat="server">
                       </tr>
                    </table>                       
            </LayoutTemplate>                                              
            <GroupTemplate>
                    <tr>
                        <td ID="itemPlaceholder" runat="server">
                        </td>
                    </tr>
             </GroupTemplate>             
             <ItemTemplate>
                    <td id="Td3" width="150px" height="150px" align="center" style="background-color: #e8e8e8;color: #333333;">
                    <asp:HiddenField ID="hfPhotoID" runat="server" Value='<%# Eval("DefaultPhotID") %>' />
                    <a href='<%# "Photos.aspx?AlbumID="+Eval("AlbumID") %>'> 
                    <asp:Image CssClass="Timg" runat="server" ID="imPhoto" ImageUrl='<%# "ThumbNail.ashx?ImURL="+Eval("Photo") %>' />
                    </a>
                    <br />                    
                    <b><asp:Label ID="lblAlbumName" runat="server" Text='<%# Eval("AlbumName") %>'></asp:Label>   </b>
                    </td>                
                </ItemTemplate>

                <InsertItemTemplate>
                <td id="Td3" width="150px" height="150px" runat="server" align="center" style="background-color: #e8e8e8;color: #333333;">
                    <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/CreateAlbum.aspx"> Create New Album1</asp:LinkButton>
             <%--   <a href="CreateAlbum.aspx" id="createalbumlink" runat="server">                    
                    Create New Album
                </a>--%>
                </td>              
                </InsertItemTemplate>             
            </asp:ListView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SLIITComDBConnectionString %>" 

        SelectCommand="SELECT Album.AlbumID, Album.DefaultPhotID, Album.AlbumName, PhotAlbum.Photo FROM Album INNER JOIN PhotAlbum ON Album.DefaultPhotID = PhotAlbum.PhotoID where album.userid=@userid">
       <SelectParameters>
       <asp:QueryStringParameter Name="userid" Type="int32"  QueryStringField="id" />

         <%--<asp:SessionParameter Name="userid" Type="String"  SessionField="UserId" />--%>
</SelectParameters>
        </asp:SqlDataSource>
    </form>

Well you can't access the LinkButton directly since its inside a ListView , you can iterate through each item in the ListView and find the link button using FindControl and then set the Visible property. 好吧,因为LinkButtonListView内部,所以不能直接访问它,您可以遍历ListView中的每个项目并使用FindControl查找链接按钮,然后设置Visible属性。 Something like: 就像是:

foreach (ListViewItem item in listView.Items)
{
    LinkButton linkButton = item.FindControl("LinkButton1") as LinkButton;
    if (linkButton != null)
        linkButton.Visible = false;
}

the above will disable LinkButton for all the items. 以上将禁用所有项目的LinkButton

使用LinkBut​​ton的OnClick事件处理程序并在此处切换可见性。

Edit 编辑

Try using FindControl: 尝试使用FindControl:

LinkButton LinkButton1 = (LinkButton)ListView1.FindControl("LinkButton1");
LinkButton1.Visible = false;

在后面的代码中的某个位置执行此操作:

LinkButton1.Visible = false;
LinkButton1.Visible = false; //control will not rendered
LinkButton1.Attribute["styles"] = "display:none"; // control will be hidden

You can alse use tag, but you have to add runat="server" param 您还可以使用标签,但必须添加runat =“ server”参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM