如何在ASP.NET中动态设置控件的可见属性 [英] How to set visible property of a control dynamically in ASP.NET

查看:268
本文介绍了如何在ASP.NET中动态设置控件的可见属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉框,如果我的类文件中定义的变量为true,则应该是可见的,否则为false。



我尝试了什么:


我的general.cs页面中的


I have a drop down box which should be visible if a variable defined in my class file is true,else false.

What I have tried:

in my general.cs page

public static class myCommonvar

{

public static bool isVisible  
    {
        get
        {
            return false;
        }
    }
}



在我的asp.net控件存在的asp页面中,我尝试了以下代码


In the asp page where my asp.net control exists, I tried the below code

<asp:DropDownList ID="ddlcompanies"  runat="server" AutoPostBack="True"  Visible="<%# myCommonvar.isVisible %>"  >
										<asp:ListItem Value="0" class="menu-dropdown">View Clients</asp:ListItem>
										<asp:ListItem Value="1" class="menu-dropdown">in line clients/asp:ListItem>
										<asp:ListItem Value="2" class="menu-dropdown">Ext clients</asp:ListItem>
									</asp:DropDownList>





但不工作

任何人都可以帮助我。



but not working
Can anyone help me please.

推荐答案

<%#%>语法用于组件数据绑定时。当您手动设置列表项时,组件永远不会被绑定,因此您需要在后面的代码中启动数据绑定,以使可见属性工作,即使您不需要它用于列表项。 />


The <%# %> syntax is for when the component is databound. As you are setting your list items manually the component is never being "bound", so you need to kick off the databinding in the code behind anyway for the visible property to work even though you don't need it for the listitems.

protected void Page_Load(object sender, EventArgs e)
{
    ddlcompanies.DataBind();
}





也不要使用静态变量,因为它们在所有用户之间共享,所以如果你将isVisible设置为一个值然后访问该网站的每个人都将其视为该值,这可能不是您想要的。



Also don't use static variables as they are shared across all users so if you set isVisible to a value then every person accessing the site sees it as that value and that might not be what you want.


这篇关于如何在ASP.NET中动态设置控件的可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆