Wednesday, February 27, 2008

Access the Items of the DataList ASP.NET

Today I learned how to access the selected row items of the datalist. In ItemTemplate of datalist I have added the button and on button click event I can access the items (e.g. MyLabel item of datalist named MyDataList). The steps are as follows

1. Add the button to the Itemtemplate of Datalist
2. enter 'select' in the CommandName property of the button
3. In Datalist SelectedIndexChanged add the following code. The value contained in item MyLabel is stored in the Mystring.

MyDataList.DataBind();
string MyString;
MyString = ((Label)MyDataList.Items[MyDataList.SelectedIndex].FindControl("MyLabel")).Text;

No comments: