2013-02-22 26 views
0

相關的用戶控件我有一個asp.net web應用,我有在母版頁的aspx頁面..我有兩個用戶控件:(1)UC1(2)UC2。 UC1被添加到主頁面設計和UC2被添加在aspx頁面設計中。問題在ASP.net

現在我在UC1上有一些複選框,AutoPostback屬性爲True ..在Checked_Change事件中,我在Session中添加了一些值。然後我想調用在UC2 userControl

的功能,但問題是,當複選框checkedchanges UC2'code執行第一,然後UC1的代碼執行..所以在UC2的代碼沒有找到任何會話值添加到UC1的代碼

所以如何改變代碼執行用戶控件的順序???

有沒有辦法通過UC1找到母版的兒童頁???

感謝

回答

0

是的,這是沒有對照組之間溝通的一個很好的方式,使用會話中也並非是一個很好的做法。但是,你可以做什麼,是這樣的:

在母版頁中,使用ContentPlaceHolder嘗試找到UC2,你需要它的ID使用FindControl方法:

Control ctrl = ContentPlaceHolder.FindControl("UC2_ID"); 

if (ctrl != null && ctrl is UC2) 
{ 
    UC2 uc2 = (UC2)ctrl; 

    uc2.TakeThisStuff(stuff); 
} 

或者,如果您不知道其ID,則可以遍歷ContentPlaceHolder控件,直到找到控件的類型爲UC2。

public T FindControl<T>(Control parent) where T : Control 
{ 
    foreach (Control c in parent.Controls) 
    { 
     if (c is T) 
     { 
      return (T)c; 
     } 
     else if (c.Controls.Count > 0) 
     { 
      Control child = FindControl<T>(c); 
      if (child != null) 
       return (T)child; 
     } 
    } 

    return null; 
} 

使用方法如下:

UC2 ctrl = FindControl<UC2>(ContentPlaceHolder); 

if (ctrl != null) 
{ 
    UC2 uc2 = (UC2)ctrl; 

    uc2.TakeThisStuff(stuff); 
}