使用web.config从aspx页面删除/隐藏asp控件 [英] Removing/Hiding asp control from aspx page using web.config

查看:120
本文介绍了使用web.config从aspx页面删除/隐藏asp控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在开发一个包含四个菜单项/页面的应用程序(Default.aspx,Links.aspx,Analytics.aspx和AddNewTab.aspx)和Site.Master页面上的下拉列表。



如何通过web.config从Analytics.aspx和Links.aspx页面隐藏下拉列表,其中有可用的在Default.aspx和AddNewTab.aspx页面。

Hi,

I am developing an application with four menu items/pages (Default.aspx, Links.aspx, Analytics.aspx and AddNewTab.aspx) and a dropdown list on Site.Master page.

How I can hide dropdown list from Analytics.aspx and Links.aspx pages through web.config, where as have that available on Default.aspx and AddNewTab.aspx pages.

推荐答案

这里我做了什么,它非常有用

在web.config中添加这个

Here what I did and it's very useful
In web.config add this
<configuration>
  <appSettings>
    <!-- Dynamic Drop down list keys-->
    <add key="Default.aspx" value="true"/>
    <add key="HomeLHA.aspx" value="true"/>
    <add key="Analytics.aspx" value="true"/>
    <add key="Links.aspx" value="false"/>
    <!-- Dynamic Drop down list keys-->
  </appSettings>
</configuration>





并写了一个方法





and wrote a method

public void ddlDynamicVisible(string PageName, DropDownList ddlDynamic)
       {
          if (ddlDynamic != null)
          {
                String isVisible = string.Empty;
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                
                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
                if (appSettingsSection != null)
                {
                    foreach (string key in appSettingsSection.Settings.AllKeys)
                    {
                        if (key == PageName)
                        {
                            isVisible = System.Configuration.ConfigurationManager.AppSettings[key];
                            ddlDynamic.Visible = Convert.ToBoolean(isVisible);
                        }
                    }
                }
            }
        }





制作一个从要显示或隐藏控件的每个页面调用此方法





make a call to this method from each page where you want to display or hide control

public partial class Links : System.Web.UI.Page
    {
        DropDownList ddlDynamic;
        protected void Page_Load(object sender, EventArgs e)
        {
            ddlDynamic = (DropDownList)Master.FindControl("ddlDynamic");
            CustomMethods Methods = new CustomMethods();
            Methods.ddlDynamicVisible(Path.GetFileName(Request.PhysicalPath), ddlDynamic);
        }
    }





现在此下拉列表仅在您在web.config中设置为True的页面上可用。



谢谢,

享受编码。



now this dropdown will only be available on pages where you set True in web.config.

Thanks,
Enjoy coding.


我认为这是最好的想法从主页面中删除不需要的控件,如下所示

我假设您的下拉列表控件是runat ='server'然后,您可以在要删除该下拉列表的页面上执行此操作:

i think it will be the best idea to remove the unwanted controls from master page like below
I assume that your dropdown list control is runat='server' then, You could do this on your page where you want to remove that dropdown list:
System.Web.UI.HtmlControls.HtmlGenericControl DropDownList=
                             this.Master.FindControl("dropdownlistID") as 
                             System.Web.UI.HtmlControls.HtmlGenericControl;
     this.Master.Controls.Remove(DropDownList);





希望它会帮助你



hope it will help you


Here what I did and it's very useful
In web.config add this

<configuration>
  <appSettings>
    <!-- Dynamic Drop down list keys-->
    <add key="Default.aspx" value="true"/>
    <add key="HomeLHA.aspx" value="true"/>
    <add key="Analytics.aspx" value="true"/>
    <add key="Links.aspx" value="false"/>
    <!-- Dynamic Drop down list keys-->
  </appSettings>
</configuration>
and wrote a method

public void ddlDynamicVisible(string PageName, DropDownList ddlDynamic)
       {
          if (ddlDynamic != null)
          {
                String isVisible = string.Empty;
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
                if (appSettingsSection != null)
                {
                    foreach (string key in appSettingsSection.Settings.AllKeys)
                    {
                        if (key == PageName)
                        {
                            isVisible = System.Configuration.ConfigurationManager.AppSettings[key];
                            ddlDynamic.Visible = Convert.ToBoolean(isVisible);
                        }
                    }
                }
            }
        }
make a call to this method from each page where you want to display or hide control

public partial class Links : System.Web.UI.Page
    {
        DropDownList ddlDynamic;
        protected void Page_Load(object sender, EventArgs e)
        {
            ddlDynamic = (DropDownList)Master.FindControl("ddlDynamic");
            CustomMethods Methods = new CustomMethods();
            Methods.ddlDynamicVisible(Path.GetFileName(Request.PhysicalPath), ddlDynamic);
        }
    }
now this dropdown will only be available on pages where you set True in web.config.

Thanks,
Enjoy coding.


这篇关于使用web.config从aspx页面删除/隐藏asp控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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