locked
How to hide a control in an User Control at runtime asp.net? RRS feed

  • Question

  • User-595193528 posted

    Hi,

    I've an User Control with 4 text boxes and I use this User Control in many pages.

    Now, I want to show only 2 or 3 textboxes in User Control based on permission in web pages.

    How to hide server controls in User control at runtime?

    Tuesday, October 9, 2012 9:22 AM

Answers

  • User1815553045 posted

    define a public method in the usercontrol which will control the visibility of text boxes.

    Call the public method on page prerender.

    Hope this will help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 9, 2012 9:57 AM

All replies

  • User-179079940 posted

    i hop help this code

    try it

    private void Page_Load(object sender, System.EventArgs e)
    {
    	if (!IsPostBack)
    	{
    		// Load the Header UserControl. 		Control hdrCtl = null;
    		try
    		{
    			hdrCtl = LoadControl("./controls/SiteHeader.ascx");
    			if (hdrCtl != null)
    			{
    				((SiteHeader)hdrCtl).LeftLogoImgPath = "..\\images\\ps_logo.gif";
    				((SiteHeader)hdrCtl).RightLogoImgPath = "..\\images\\ps_name.gif";
    				HeaderCtl.Controls.Add(hdrCtl);
    			}
    		}
    		catch (Exception ex)
    		{
    			Trace.Write(ex.Message);
    		}
    	}
    }
    Tuesday, October 9, 2012 9:27 AM
  • User1815553045 posted

    define a public method in the usercontrol which will control the visibility of text boxes.

    Call the public method on page prerender.

    Hope this will help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 9, 2012 9:57 AM
  • User3866881 posted

    Hi,

    I think you can just use FindControl to find certain controls from the user control and then use its Visible property to set with "true" or "false".

    Wednesday, October 10, 2012 10:04 PM