加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Repeater中的ASP.NET DataGrid

发布时间:2020-12-16 09:14:30 所属栏目:asp.Net 来源:网络整理
导读:我有一个有两列的表: CommunityIDPersonID 还有一个“People”表,其中包括: FirstNameLastName 我想为每个社区显示不同的DataGrid,每个数据网格只包含属于该社区的人员.我想这样做而不使用4个单独的SqlDataSources. 一个转发器看起来是一个好方法,在ItemTe
我有一个有两列的表:

CommunityID
PersonID

还有一个“People”表,其中包括:

FirstName
LastName

我想为每个社区显示不同的DataGrid,每个数据网格只包含属于该社区的人员.我想这样做而不使用4个单独的SqlDataSources.

一个转发器看起来是一个好方法,在ItemTemplate中有一个DataGrid,但我似乎无法做出正确或反复的方法来使用每个重复的不同值.

如果有人对更好的方法有任何建议,我会非常感激,因为这是我第一次进入ASP.NET世界

谢谢,

麦克风

解决方法

我个人不会使用DataGrid控件,因为它会限制您对输出的控制,并且它们已被更新的 GridView&更换. ListView控件(虽然DataGrid是 not obsolete所以如果你愿意,可以随意使用它).您可能需要考虑使用替代方案,但不要求您这样做.

要做你想要的,你会得到如下标记:

<asp:Repeater runat="server" ID="myRepeater" 
              onitemdatabound="Repeater_ItemDataBound">
<ItemTemplate>
    <asp:DataGrid runat="server" ID="myDataGrid">
    </asp:DataGrid>
</ItemTemplate>
</asp:Repeater>

然后,您将使用以下代码隐藏连接标记:

protected void Page_Load(object sender,EventArgs e)
{
    myRepeater.DataSource = new Object[0];
    myRepeater.DataBind();
}

protected void Repeater_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
    DataGrid dg = (DataGrid)e.Item.FindControl("myDataGrid");
    object o = e.Item.DataItem;// Cast the DataItem as whatever 
                               // your Repeater's DataSource is

    // ...
    // Do whatever you need to get the 
    // data source for your DataGrid here
    // ...

    dg.DataSource = DataGridSourceObjectHere;
    dg.DataBind();
}

关键是Repeater的ItemDataBound事件,这是每次创建转发器行时调用的方法.这是数据绑定DataGrid源的地方.您可以使用RepeaterItemEventArgs参数在此方法中放置所需的任何逻辑,以访问绑定到Repeater的数据项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读