Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 数据绑定用户控件内的gridview_Asp.net_.net_Vb.net_Gridview - Fatal编程技术网

Asp.net 数据绑定用户控件内的gridview

Asp.net 数据绑定用户控件内的gridview,asp.net,.net,vb.net,gridview,Asp.net,.net,Vb.net,Gridview,我在用户控件中有一个超级简单的gridview设置,如下所示: <asp:GridView ID="gvTestUC" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-HorizontalAlign="Left"

我在用户控件中有一个超级简单的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" />
但它找不到gridview

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

任何帮助都将不胜感激


谢谢

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

用户控件中

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
页面中
中,当您要对其进行数据绑定时:

test1a.BindData(GetGridDataSource())

旁注:切勿让
用户控件
数据从
页面加载
绑定。这可能会导致严重的副作用,从本应作为控制器的页面中删除控件,并在需要时防止延迟加载控件(例如,在TabinExchanging上使用
TabContainer
控件)。

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

用户控件中

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
页面中
中,当您要对其进行数据绑定时:

test1a.BindData(GetGridDataSource())

旁注:切勿让
用户控件
数据从
页面加载
绑定。这可能会导致严重的副作用,从本应作为控制器的页面中删除控件,并在需要时防止延迟加载控件(例如,在TabIndexChanging上使用
TabContainer
control)。

您不能使用test1a.FindControl(“gvTestUC”)查找文章,然后进行数据绑定吗?您不能使用test1a.FindControl吗(“gvTestUC”)查找文章然后进行数据绑定?但这是在用户控件中获取数据源。我正在页面中填充一个数据集,然后需要使用该数据集绑定用户控件gridview。Thanks@999cm999:然后提供一个公共属性
DataSource
或-更好-将其作为参数传递给方法。我将编辑我的答案。但是这是在用户控件中获取数据源。我正在页面中填充一个数据集,然后需要使用该数据集绑定用户控件gridview。Thanks@999cm999:然后提供一个公共属性
DataSource
或-更好-将其作为参数传递给方法。我将编辑我的答案。