Click here to Skip to main content
15,888,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 5-Jul-13 11:24am
v2

Here what I did and it's very useful
In web.config add this
XML
<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.
 
Share this answer
 
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:
C#
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
 
Share this answer
 
v3
Comments
zunorain 11-Jul-13 10:54am    
Thanks for the solution, but it will only remove it through code what if this need to be display or hide through web.config.
XML
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900