Need to make changes in my db based on the data of the ListViewItem of the button that is clicked. (i.e, if someone clicked on the button in the first item, that Item's dataSource has a SessionId column/value that I need to access)
I've tried accessing the Item using SelectedIndex property (Is it supposed to return -1 ?), but uncertain on steps to take next
Were I using WPF I believe I could just access the Buttons DataContext but apparently its different in asp.net & I cannot find the equivalent.
Listview in .aspx:
<asp:ListView
ID="lvInstructors"
runat="server"
itemwDataBound="lvDataBound"
itemCommand="lvCommand"
Visible="true">
<LayoutTemplate>
<div class="container" id="mainContent">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="row instructorItem" id="instructorItem">
<div class="col-2 sessionStartTimeDiv">
<p class="sessionStartTime"><%#Eval("SessionStartTime")%></p>
</div>
<div class="col-2 instructorHeadshotDiv">
<asp:Image class="instructorHeadshot" runat="server" src='<%#Eval("InstructorHeadshot")%>' />
</div>
<div class="col-5 sessionInfoDiv">
<h3 class="instructorName"><%#Eval("InstructorName")%></h3>
<p class="sessionInfo"><%#Eval("SessionInfo")%></p>
</div>
<div class="col-3 checkInBtnDiv">
<asp:Button class="checkInBtn" ID="checkInBtn" runat="server" OnClick="CheckInBtn_Click" Text="Check-In"></asp:Button>
</div>
</div>
<hr />
</ItemTemplate>
<EmptyDataTemplate>
<br />
<br />
No Sessions to Display.
</EmptyDataTemplate>
</asp:ListView>
This is how I populate my lv in .aspx.cs Codebehind:
private void UpdateInstructorListView()
{
//Make Data table to hold ListViewItem Data
dt = new DataTable();
dt.Columns.Add("SessionId");
dt.Columns.Add("SessionStartTime");
dt.Columns.Add("InstructorHeadshot");
dt.Columns.Add("InstructorName");
dt.Columns.Add("SessionInfo");
DataRow dr;
foreach (Session S in UpcomingSessions)
{
foreach (Enrollment I in S.Instructors())
{
//
SessionId = S.SessionId;
SessionStartTime = S.FirstDateTime().ToShortTimeString();
// TODO: JUSTIN FIGURE OUT HOW/WHERE TO PULL HEADSHOT FROM THEN BEST WAY TO ADD IT TO src ATTRIBUTE
InstructorHeadshot = "headshots/Justin.jpg";
InstructorName = I.FirstName + " " + I.LastName;
SessionInfo = S.Name + " , " + S.Room.ToString();
//Fill rows in DataTable with variables
dr = dt.NewRow();
dr["SessionId"] = SessionId;
dr["SessionStartTime"] = SessionStartTime;
dr["InstructorHeadshot"] = InstructorHeadshot;
dr["InstructorName"] = InstructorName;
dr["SessionInfo"] = SessionInfo;
dt.Rows.Add(dr);
}
}
//Bind datatable to lv
lvInstructors.DataSource = dt;
lvInstructors.DataBind();
}
I expect to be able to access the data of the ListViewItem whose button was clicked on. (i.e be able to get/use lvInstructorList.selectedIndex.InstructorName )
I would use a Repeater instead of a ListView. You can then use the Repeaters OnItemCommand to catch a click and evaluate the Buttons CommandArgument to take the appropriate action.
I think it might be worth a try.