0

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 )

Justin
  • 89
  • 10

1 Answers1

1

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.

KH S
  • 444
  • 1
  • 4
  • 8
  • 1
    ListView has an ItemCommand as well - https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview.itemcommand?view=netframework-4.8 This is a good example to follow Justin, note how the actual data is not there after it's been bound - you'll need to set the ListView's DataKey property and use something like the code from the example to retrieve the specific item that's selected. – sh1rts Sep 24 '19 at 21:52
  • Thank both of you, very much. I'm going to look into it now. If I were to use a Repeater instead, would there be any advantage ? – Justin Sep 25 '19 at 13:20
  • @Justin I prefer Repeater as I feel more in control of the html output. Might be a little bit more coding in the aspx page, but I do prefer that. – KH S Sep 25 '19 at 14:43
  • Ahh I see. Well, thanks again to you both. I achieved what I wanted with this: ``` (Added a HiddenField control in .aspx) (Then accessed the value of that clicked field in codeBehind) protected void CheckInBtn_Click(object sender, EventArgs e) { Button checkInBtn = (Button)sender; var row = (ListViewItem)checkInBtn.NamingContainer;; int clickedSessionID = Convert.ToInt32(((HiddenField)row.FindControl("sessionID")).Value); ``` – Justin Sep 26 '19 at 14:15