【问题标题】:How can I access web user controls within a gridview?如何访问网格视图中的 Web 用户控件?
【发布时间】:2012-05-24 23:33:00
【问题描述】:

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

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    使用父网格的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。

    【讨论】:

    • 谢谢,但我认为这仅适用于自定义用户控件。我正在使用网络用户控件。我将如何访问这些?
    • 它将与网络用户控件一起使用。这基本上是我在自己的应用程序中所做的并取得了巨大成功。只需公开一个可以类似于我在示例中所做的调用的公共方法。您的 Web 用户控件将调用其网格的 databind()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多