DataList

M

Mark

I have a datalist (see code below). Assume that the datalist is populated
with 10 records of data. How do I programatically grab all the data in ALL
the columns of the selected record? I've been trying to use the FindControl
method inside the ItemCommand event of the DataList to get the contents of
the Label below, but it appears that the DataBinder code is making it a
child control of the lblInstructorEmplidDisplayed control, rather than
making it the text property of the Label itself. I'm open to other methods
.... I'd just like to figure out how to use the DataList rather than the
DataGrid to do it. Note that I'm not allowing the user to edit, delete or
update data ... just display it and allow the user to select a single record
using a button in the left most column.

Thanks in advance.
Mark

<asp:DataList id="dlDisplayNames" runat="server" DataKeyField="Emplid">
<ItemTemplate>
<TABLE border="1">
<tr>
<td>
<asp:Button CommandName="SelectInstructorName" runat="server"
ID="lbInstructor" Text="View" />
</td>
<td>
<asp:Label Runat="server" ID="lblInstructorEmplidDisplayed">
<%# DataBinder.Eval(Container.DataItem, "emplid") %>
</asp:Label>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "name") %>
</td>
</tr>
</TABLE>
</ItemTemplate>
</asp:DataList>
 
M

Mark Fitzpatrick

Instead of performing late-binding like this, why not use early binding? You
can set your own event handler for the DataList.ItemCreated event. This will
let you get full access to everything, and a lot more control too.

For example:

private void dlDisplayNames(object sender, DataListItemEventArgs e)
{
// we now have full access to the data itself.
// let's say the data is stored in a DataRow and we'll use it to load
the label
DataRow drCurrentRow = (DataRow) e.Item.DataItem;

Label lblInstructorEmplidDisplayed = (Label)
e.Item.FindControl("lblInstructorEmplidDisplayed");
if(lblInstructorEmplidDisplayed != null)
{
lblInstructorEmplidDisplayed.Text =
drCurrentRow["emplid"].ToString()
}

}


I've avoided using the DataBinder as I tend to loose more control over
things that way, especially when we can get all the control from the
back-end. Of course, you can still probably just create your own ItemCreated
event to grab the data using e.Item.DataItem and casting it to the
appropriate object while still using late binding and the DataBinder.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
M

Mark

This is VERY useful and makes sense. Thank you. So if this goes in my
code-behind, how would my .aspx page look differently?

Thanks Mark.

Mark

Mark Fitzpatrick said:
Instead of performing late-binding like this, why not use early binding? You
can set your own event handler for the DataList.ItemCreated event. This will
let you get full access to everything, and a lot more control too.

For example:

private void dlDisplayNames(object sender, DataListItemEventArgs e)
{
// we now have full access to the data itself.
// let's say the data is stored in a DataRow and we'll use it to load
the label
DataRow drCurrentRow = (DataRow) e.Item.DataItem;

Label lblInstructorEmplidDisplayed = (Label)
e.Item.FindControl("lblInstructorEmplidDisplayed");
if(lblInstructorEmplidDisplayed != null)
{
lblInstructorEmplidDisplayed.Text =
drCurrentRow["emplid"].ToString()
}

}


I've avoided using the DataBinder as I tend to loose more control over
things that way, especially when we can get all the control from the
back-end. Of course, you can still probably just create your own ItemCreated
event to grab the data using e.Item.DataItem and casting it to the
appropriate object while still using late binding and the DataBinder.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


Mark said:
I have a datalist (see code below). Assume that the datalist is populated
with 10 records of data. How do I programatically grab all the data in ALL
the columns of the selected record? I've been trying to use the FindControl
method inside the ItemCommand event of the DataList to get the contents of
the Label below, but it appears that the DataBinder code is making it a
child control of the lblInstructorEmplidDisplayed control, rather than
making it the text property of the Label itself. I'm open to other methods
... I'd just like to figure out how to use the DataList rather than the
DataGrid to do it. Note that I'm not allowing the user to edit, delete or
update data ... just display it and allow the user to select a single record
using a button in the left most column.

Thanks in advance.
Mark

<asp:DataList id="dlDisplayNames" runat="server" DataKeyField="Emplid">
<ItemTemplate>
<TABLE border="1">
<tr>
<td>
<asp:Button CommandName="SelectInstructorName" runat="server"
ID="lbInstructor" Text="View" />
</td>
<td>
<asp:Label Runat="server" ID="lblInstructorEmplidDisplayed">
<%# DataBinder.Eval(Container.DataItem, "emplid") %>
</asp:Label>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "name") %>
</td>
</tr>
</TABLE>
</ItemTemplate>
</asp:DataList>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top