Little and Mickle

All things are difficult before they are easy.

导航

Loop through controls in asp.net

codes in BasePage.aspx
    <body>
        
<form id="Form1" method="post" runat="server">
            
        
</form>
    
</body>

codes in BasePage.aspx.cs,  Provide Programming access to html<form> at the server
protected System.Web.UI.HtmlControls.HtmlForm Form1;

Loop through controls in Form1:
        protected void LoopThroughControls()
        
{
            StringBuilder sb 
= new StringBuilder();
            
foreach(Control k in Form1.Controls)
            
{
                
if(k.ID!=null&&k.ID!=string.Empty)
                
{
                    sb.AppendFormat(k.GetType().Name 
+ ":" + k.ID  + "\\n");
                    
                }

            }

            Response.Write(
"<script>alert('"+sb.ToString()+ "');</script>");
}



Tip about BasePage class:
      When we construct a derived page class inherit from BasePage, and invoke LoopThroughControls() of the base class in the Page_Load(), the method would loop through all controls in the derived class.  It is a very useful character in asp.net, especially when we need permission control for each control. We can set each control's behavior, as visible/invisible, for different user in the BasePage(user information could be stored in Session), thus we would not need to manipulate permission in the derived class.
      I would write an article on permission control, mainly about button permission control later. Any questions, contact me freely.
  

posted on 2005-08-01 18:45  davidullua  阅读(398)  评论(0)    收藏  举报