個人而言,我不會用一個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();
}
關鍵是中繼器的ItemDataBound
事件,這是每次創建中繼器行時調用的方法。這是數據綁定DataGrid源的地方。您可以使用RepeaterItemEventArgs
參數將您需要的任何邏輯放入此方法中,以訪問您綁定到Repeater的數據項。
+1爲GridView和ListView插件 – 2009-10-22 02:51:49
+1很好的解釋,有很好的代碼示例。 – 2009-10-22 03:09:14
我不太明白。我需要我的轉發器有一個sqlDataSource作爲源,它獲得社區的必需子集。然後我需要讓這個社區裏的所有人。如果我使用SqlDataSource設置我的中繼器,當我嘗試在Page_Load事件中綁定它時出現錯誤。 – 2009-10-22 03:24:34