2012-05-24 24 views
0

我有一個gridview,每行有三個獨立的web用戶控件。每個用戶控件都包含自己的網格視圖。當我重新綁定父Gridview時,我需要能夠重新綁定usercontrols中的gridviews。就像我現在所做的那樣,當我重新綁定父網格時,用戶控件中的所有網格都會釋放所有數據。當我重新綁定父網格時,如何訪問父網格中的用戶控件以重新綁定它們的網格?如何訪問gridview中的Web用戶控件?

回答

0

使用你的父母網格的RowDataBound事件,你可以做這樣的事情:

void theParentGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataRow row = ((System.Data.DataRowView) e.Row.DataItem).Row; 
     MyCustomControl custControl1 = e.Row.FindControl("MyCustomControl1Id") as MyCustomControl; 
     MyCustomControl custControl2 = e.Row.FindControl("MyCustomControl2Id") as MyCustomControl; 
     MyCustomControl custControl3 = e.Row.FindControl("MyCustomControl3Id") as MyCustomControl; 
     if (custControl1!=null) 
      custControl1.bindForRow(row); 
     if (custControl2!=null) 
      custControl2.bindForRow(row); 
     if (custControl3!=null) 
      custControl3.bindForRow(row); 

    } 
} 

當然,定製控件綁定程序將處理基於由row提供的信息調用的DataBind自身的網格。

+0

謝謝,但我認爲這隻適用於自定義用戶控件。我正在使用網絡用戶控件。我將如何訪問這些? – Brian

+0

它將與網絡用戶控件一起使用。這實際上是我在自己的應用程序中所做的,並取得了巨大成功。只要公開一個可以類似於我在示例中所做的調用的公共方法。您的網絡用戶控件將調用其網格的databind()。 – Jeremy

相關問題