Explanation:
We will be able to access the Row item of a Datalist when the DataList is already
bounded to the Database. so we will using ItemDataBound
event of the DataList.
our DataList with its DataSource Control look like this:
<asp:DataList ID="DataList1" runat="server" DataKeyField="AlbumID"
DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
AlbumID:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
Caption:
<asp:Label ID="CaptionLabel" runat="server" Text='<%# Eval("Caption") %>' />
<br />
IsPublic:
<asp:Label ID="IsPublicLabel" runat="server" Text='<%# Eval("IsPublic") %>' />
<br />
UserName:
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Personal %>"
SelectCommand="SELECT * FROM [Albums]"></asp:SqlDataSource>
Now the Code behind for ItemDataBound event is the following:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label exam_weigh = (Label)e.Item.FindControl("Label1");
exam_weigh.Text = "Test String";
}
}
You noticed that we are accessing two ListItemType (Item and AlternatingItem) types from the current row.
|