如何在gridview中访问Web用户控件?

时间:2012-05-24 23:33:01

标签: asp.net gridview

我有一个gridview,每行有三个单独的Web用户控件。每个用户控件都包含自己的网格视图。当我重新绑定父网格视图时,我需要能够重新绑定用户控件中的网格视图。就像我现在一样,当我重新绑定父网格时,用户控件中的所有网格都会丢失所有数据。当我重新绑定父网格时,如何访问父网格中的用户控件以重新绑定网格?

1 个答案:

答案 0 :(得分: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。