Rajeev Ranjan

Rajeev Ranjan

  • NA
  • 8
  • 5.1k

PageBase class in not hiding button control in run time

Apr 8 2014 3:16 PM
Dear Friend,

I am facing one strange behavior in my ASP.net (Web Form) web application. I have created one PageBase class (PageBase.cs) and I am implementing this in my all web 

page (code behind class). Actually I am hiding/showing a particular button control in each page based on some condition so implemented that logic in PageBase class on 

"OnPreRender" event. I am recurssively searching that button in each page and performing action. This functionality is working perfectly when I run application through 

code but same functionality stops working after deployment on web site (under IIS). I am not sure why this behavior is happenning. My sample code is below. Could you 

please tell me what could be possibility. Thanks for your support in advance.
PageBase.cs

===========

public class PageBase : System.Web.UI.Page
{
        protected override void OnPreRender(EventArgs e)
        {
               base.OnPreRender(e);
               Page page = (Page)HttpContext.Current.Handler;
               ShowHideControl(Page.Controls);
        }

        private void ShowHideControl(ControlCollection page)
        {
                foreach (Control ctrl in page)
                {
                      //If current control is AddNew button
                      if (ctrl.ID == "btnAddNew")
                      {
                             //Hide button if add permission is not given
                             if (isAdd==false)
                             { 
                                        ctrl.Visible = false;
                              }
                      }

                      if (ctrl.HasControls())
                      {
                             ShowHideControl(ctrl.Controls);
                      }
                }
         }
}

BusinessMaster.aspx.c

===============

public partial class BusinessMaster : PageBase 



BusinessMaster.aspx

=============

<table style="width: 100%">
<tr>
<td align="right">
</td>
<td height="35" align="right" style="width: 50%">
<asp:Button ID="btnAddNew" runat="server" Text="Add New" CssClass="FormBtnSmall" OnClick="btnAddNew_Click" TabIndex="8" />
</td>
</tr>
</table>