My C# WebFormsHelper class

I’ve posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my WebFormsHelper class:

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics ;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace FSCSharpLib.ASP

{

    //TODO merge with FSHelperLib.WebFormsHelper

    public static class WebFormsHelper

    {

        #region  “Recursively FindControl”

               

        // From http://forums.asp.net/2/880691/ShowThread.aspx

        //useful for debugging TODO: add trace option ,

        //see also DNN FindControlRecursiveDown and FindControlRecursive and  http://geekswithblogs.net/mnf/archive/2006/08/16/88101.aspx

        public static System.Web.UI.Control RecursivelyFindControl(System.Web.UI.ControlCollection oParentControls, string ServerID)

        {

            return RecursivelyFindControl(oParentControls, ID, false);

        }

        public static System.Web.UI.Control RecursivelyFindControl(System.Web.UI.ControlCollection oParentControls, string ID,bool bClientID)

        {

             //oChild;

            if (oParentControls.Count > 0)

            {

                foreach (System.Web.UI.Control oChild in oParentControls)

                {

                   // Debug.WriteLine(“Control: ” + oChild.ID + ” (” + oChild.ClientID + “)”);

                    bool bFound = false;

                    if (bClientID)   { bFound = (oChild.ClientID == ID); }

                    else             { bFound = (oChild.ID == ID); }

                    if (bFound)

                    {

                        string sMsg=“Found Control: “ + oChild.ID ;

                        if(bClientID)  sMsg += ” (“ + oChild.ClientID + “)”;

                        Debug.WriteLine(sMsg);

                        return oChild;

                    }

                    if (oChild.HasControls())

                    {

                        System.Web.UI.Control oChild1 = RecursivelyFindControl(oChild.Controls, ServerID);

                        if (!(oChild1 == null))

                        {

                            // Debug.WriteLine(“Recursive Found Control: ” + oChild1.ID + ” (” + oChild1.ClientID + “)”);

                            return oChild1;

                        }

                    }

                }

            }

            return null;

        }

        public static System.Web.UI.Control RecursivelyFindClientControl(System.Web.UI.ControlCollection oParentControls, string ClientID)

        {

            return RecursivelyFindControl(oParentControls, ID, true);

            // System.Web.UI.Control oChild;

            //if (oParentControls.Count > 0)

            //{

            //    foreach (System.Web.UI.Control oChild in oParentControls)

            //    {

            //        Debug.WriteLine(“Control: ” + oChild.ID + ” (” + oChild.ClientID + “)”);

            //        if (oChild.ClientID == ClientID)

            //        {

            //            Debug.WriteLine(“Found Control: ” + oChild.ID + ” (” + oChild.ClientID + “)”);

            //            return oChild;

            //        }

            //        if (oChild.HasControls())

            //        {

            //            System.Web.UI.Control oChild1 = RecursivelyFindClientControl(oChild.Controls, ClientID);

            //            if (!(oChild1 == null))

            //            {

            //                Debug.WriteLine(“Recursive Found Control: ” + oChild1.ID + ” (” + oChild1.ClientID + “)”);

            //                return oChild1;

            //            }

            //        }

            //    }

            //}

            //return null;

        }//end

        #endregion  //”Recursively FindControl”

 

        //From http://www.eggheadcafe.com/articles/20050609.asp “Which control generated my postback?”

        public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)

        {

            Control control = null;

            string ctrlname = page.Request.Params[“__EVENTTARGET”];

            if (ctrlname != null && ctrlname != String.Empty)

            {

                control = page.FindControl(ctrlname);

            }

            // if __EVENTTARGET is null, the control is a button type and we need to

            // iterate over the form collection to find it

            else

            {

                string ctrlStr = String.Empty;

                Control c = null;

                foreach (string ctl in page.Request.Form)

                {

                    // handle ImageButton controls …

                    if (ctl.EndsWith(“.x”) || ctl.EndsWith(“.y”))

                    {

                        ctrlStr = ctl.Substring(0, ctl.Length – 2);

                        c = page.FindControl(ctrlStr);

                    }

                    else

                    {

                        c = page.FindControl(ctl);

                    }

                    if (c is System.Web.UI.WebControls.Button ||

                             c is System.Web.UI.WebControls.ImageButton)

                    {

                        control = c;

                        break;

                    }

                }

            }

            return control;

        }

        #region  “GridView helpers”

        public static int ColumnIndexBySortExpression(DataControlFieldCollection Columns, string SortExpression)

        {

            for (int i = 0; i < Columns.Count ; i++)

            {

                if (Columns[i].SortExpression == SortExpression)

                {

                    return i;

                }

            }

            return -1;

        }

        public static TableCell CellBySortExpression(GridViewRow Item, string SortExpression)

        {

            GridView gridView =(GridView) Item.NamingContainer;

            int nColumn = ColumnIndexBySortExpression(gridView.Columns, SortExpression);

            if (nColumn < 0)

            {

                return null;

            }

            else

            {

                return Item.Cells[nColumn];

            }

        }

 

        public static DataControlField ColumnBySortExpression(DataControlFieldCollection Columns, string SortExpression)

        {

            DataControlField oRet = null;

            int i = ColumnIndexBySortExpression(Columns, SortExpression);

            if (i >= 0)

            {

                oRet = Columns[i];

            }

            return oRet;

        }

        public static string GetColumnValueBySortExpression(GridViewRow gvRow, string SortExpression)

        {

            TableCell cell = WebFormsHelper.CellBySortExpression(gvRow, SortExpression);

            string sColumnName = cell.Text;

            return sColumnName;

        }

        #endregion  //”GridView helpers”

 

    }//class

}

 

 


Advertisements