0

我在这样的用户控件中有一个超级简单的 gridview 设置:

    <asp:GridView ID="gvTestUC" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
            <asp:BoundField DataField="EmailAddress" HeaderText="Email Address" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
        </Columns>
</asp:GridView>

我正在像这样注册用户控件:

<%@ Register src="Controls/test1.ascx" tagname="test1" tagprefix="test1uc" %>
<test1uc:test1 ID="test1a" runat="server" />

我的问题是,我不知道如何在使用用户控件的 .aspx 页面中对其进行数据绑定。

我试着做:

test1a.gvTestUC

但它找不到gridview。

我试图从 .aspx 页面对 gridview 进行数据绑定的原因是,所有需要此用户控件的页面都需要将不同的数据绑定到其中的 gridview。

任何帮助,将不胜感激。

谢谢!

4

1 回答 1

3

您只需要提供一个可以从您的页面调用的公共方法。

在你的UserControl

Public Sub BindData(gridSource As ComponentModel.IListSource)
    BindGridView(dataSource)
End Sub

Private Sub BindGridView(gridSource As ComponentModel.IListSource)
    gvTestUC.DataSource = gridSource 
    gvTestUC.DataBind()
End Sub

Page您想要对其进行数据绑定时:

test1a.BindData(GetGridDataSource())

旁注:永远不要让UserControl数据从Page_Load. 这可能会导致令人讨厌的副作用,从页面中删除控件,该页面是控制器,并且会在需要时防止延迟加载控件(在 TabIndexChanging 上使用TabContainer控件)。

于 2013-02-13T15:45:20.693 回答