How to get value from column in Datalist into variable



Hi Community

I am using a Datalist control on my aspx page with C#. In my
ItemDataBound and ItemCommand
events I am trying to put first column of the fist row into a variable. The
column is the ID#/column and it's <td> tag id is id="incDL" I check what
value is in the variable by showing it in a label control but it is always 0.
The problem is that I know that the value in that column is 38 in the
datasource table for that first row in that first column. The field is the
ID# and I am accessing it thru its id property in the <td> tag. Can anyone
tell me how to get the value (ID#) from the Datalist into the variable? The
following is how the Datalist looks:

<div id="dent">
<div id="denthdr"></div>
<div id="Rptr">
<asp:DataList ID="incDL"
runat="server" onitemdatabound="incDL_ItemDataBound"
onselectedindexchanged="incDL_SelectedIndexChanged" >



<td id="numID"> <%# DataBinder.Eval(Container.DataItem, "ID#")
<td id="TypeID"> <%# DataBinder.Eval(Container.DataItem, " Type")
%> </td>
<td id="QuestID"> <%# DataBinder.Eval(Container.DataItem, "Quest")
%> </td>
<td id="callID"> <%# DataBinder.Eval(Container.DataItem, "Call")%>
<td id="ReasonID" > <%# DataBinder.Eval(Container.DataItem,
"Reason") %> </td>
<td id="PT1ID"> <%# DataBinder.Eval(Container.DataItem, "PT1")%>
<td id="PT2ID"> <%# DataBinder.Eval(Container.DataItem, "PT2")%>
<td id="PT3ID"> <%# DataBinder.Eval(Container.DataItem, "PT#")%>
<td><asp:Button ID="btnSelect" runat="server" Text="Select"
onclick="btnSelect_Click" /></td>





Thank you in advance

Eddy Jawed

I have done exactly the same thing but using VB for code-behind.

Getting the row index in a datalist is quiet easy, all you need to do is either set the 'Datakeyfield' to an ID row in your datalist on the asp front page, then in the code behind you get the 'item' from the 'datakeys' property of your datalist, something like this....

Public Sub SelectItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

'To get value of any cell in a row based on its index number
Dim item As DataListItem = DataList1.Items(e.Item.ItemIndex)
Dim strValue As String = DirectCast(item.FindControl("Datalist_Label_ID"), Label).Text

'OR to get the index number of row selected
Dim intRowID = DataList1.DataKeys.Item(e.Item.ItemIndex)

'OR just get the value of a cell with an imagebutton in it.

strValue = e.CommandName

End Sub

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

Latest member

Latest Threads