DCSIMG
System.Web.UI.Control.FindControl => FindControlRecursive - שלמה גולדברג (הרב דוטנט)

שלמה גולדברג (הרב דוטנט)

מרצה בסלע ויועץ בעולם ה - net.

System.Web.UI.Control.FindControl => FindControlRecursive

 

אחד הדברים הכי מעצבנים במתודה FindControl ב - Asp.Net, שהוא יודע לחפש רק באוסף הפקדים שלו, אבל הוא לא יודע לחפש פנימה, כלומר - אם יש לי טבלה שבאחד מהעמודות יש פקד שאני מחפש, אני לא אוכל למצוא את זה באמצעות FindControl על הטבלה, רק באמצעות FindControl על העמודה הספציפית שבה הפקד נמצא.
 
כדי לפתור את זה, אפשר להשתמש במתודה הזאת. (היא מוגדרת כ - Extension על Control, אבל כמובן שאפשר למחוק את המילה this, וזה יהפוך למתודה סטטית רגילה)
 

public static Control FindControlRecursive(this Control root, string id)

{

    if (root.ID == id || root.ClientID == id || root.UniqueID == id)

    {

        return root;

    }

 

    foreach (Control control in root.Controls)

    {

        Control foundControl = FindControlRecursive(control, id);

 

        if (foundControl != null)

        {

            return foundControl;

        }

    }

 

    return null;

}

 
ותודה לגיא.
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 8 and 4 and type the answer here:


Enter the numbers above: