How to get value from column in Datalist into variable

S

Sharon

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"
RepeatDirection="Vertical"
RepeatLayout="Table"
runat="server" onitemdatabound="incDL_ItemDataBound"
onitemcommand="incDL_ItemCommand"
onitemcreated="incDL_ItemCreated"
onselectedindexchanged="incDL_SelectedIndexChanged" >
<HeaderTemplate>
<table>
<tr>
<td>ID#</td>
<td>Type</td>
<td>Quest</td>
<td>Call</font></td>
<td>Reason</font></td>
<td>PT1</td>
<td>PT2</td>
<td>PT3</td>

</tr>

</HeaderTemplate>

<ItemTemplate>
<tr>
<td id="numID"> <%# DataBinder.Eval(Container.DataItem, "ID#")
%></td>
<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>
<td id="ReasonID" > <%# DataBinder.Eval(Container.DataItem,
"Reason") %> </td>
<td id="PT1ID"> <%# DataBinder.Eval(Container.DataItem, "PT1")%>
</td>
<td id="PT2ID"> <%# DataBinder.Eval(Container.DataItem, "PT2")%>
</td>
<td id="PT3ID"> <%# DataBinder.Eval(Container.DataItem, "PT#")%>
</td>
<td><asp:Button ID="btnSelect" runat="server" Text="Select"
CommandName="select"
onclick="btnSelect_Click" /></td>

</tr>
</ItemTemplate>
<FooterTemplate>

</table>

</FooterTemplate>
</asp:DataList>

</div>
</div>

Thank you in advance
Sharon
 
E

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
WriteLine(strValue)

'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

Top