一个Repeater中的ASP.NET DataGrid [英] ASP.NET DataGrid within a Repeater

查看:147
本文介绍了一个Repeater中的ASP.NET DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,它有两列:

  CommunityID 
PersonID

还有一个人物表(其中包括):

  FirstName 
姓氏

我想显示一个不同的DataGrid为每个社区,每个datagrid只有人是该社区的一部分。我想这样做,而不使用4个独立的SqlDataSource。



中继器看起来像一个很好的方式,在ItemTemplate里面有一个DataGrid,但我似乎不能如果有任何人有更好的方法来做任何建议,我将非常感激,因为这是我第一次进入ASP.NET的世界之一



谢谢,



Mike

解决方案

我个人不会使用DataGrid控件,因为它限制了您对输出的控制,而且已被更新的 GridView & ListView 控件(虽然DataGrid是不过时,所以随意使用它,如果你想)。您可能需要考虑使用替代品,但不需要这样做。



要做你想要的,你可以像下面这样的标记:

 < asp:Repeater runat =serverID =myRepeater
onitemdatabound =Repeater_ItemDataBound>
< ItemTemplate>
< asp:DataGrid runat =serverID =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; //将DataItem转换为
//您的Repeater的DataSource是

// ...
// Do无论你需要得到
//数据源为您的DataGrid
// ...

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

关键是Repeater的 ItemDataBound 事件,这是每次创建中继器行时调用的方法。这是数据绑定您的DataGrid源的地方。您可以使用 RepeaterItemEventArgs 参数,以访问您绑定到中继器的数据项。


I have a table that has two columns:

CommunityID
PersonID

And A "People" table that has (among other things):

FirstName
LastName

I would like to display a different DataGrid for each community, each datagrid having only the people that are part of that community. I would like to do this without using 4 seperate SqlDataSources.

A Repeater looks like a good way, with a DataGrid inside the ItemTemplate, but I can't seem to make heads or tails of getting that to work with the different values for each repetition.

If anyone has any suggestions on better ways to do this, I'd be very appreciative, as this is one of my first forays into the world for ASP.NET

Thanks,

Mike

解决方案

Personally I wouldn't use a DataGrid control, since it restricts your control over your output and they've been replaced by the newer GridView & ListView controls (although DataGrid is not obsolete so feel free to use it if you want). You may want to consider using the alternatives but you aren't required to do so.

To do what you're looking for, you would have markup like the following:

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

Then you'll wire up the markup with the following code-behind:

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();
}

The key is the Repeater's ItemDataBound event, which is the method called every time a repeater row is created. This is where you can data bind your DataGrid source. You can put any logic you need to within this method using the RepeaterItemEventArgs parameter to access the data item you bound to your Repeater.

这篇关于一个Repeater中的ASP.NET DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆