Search code examples
c#asp.netlistviewweb-controls

Cant access button in codebehind


I have this button in my .aspx file and would like to access it in my codeBehind .aspx.cs file.

It's in a ListView so I'm assuming that's where the issue is (I have no problem accessing any elements outside of that ListView) - just cant seem to figure it out.

Button code (Its in a ListView with ID lvInstructorList):

<div class="col-3 checkInBtnDiv" runat="server">
     <asp:Button class="checkInBtn" ID="checkInBtn" runat="server" OnClick="CheckInBtn_Click" Text="Check-In"></asp:Button>   
</div>           

What I've tried so far in codeBehind: 1)

protected void CheckInBtn_Click(object sender, EventArgs e)
{
    (lvInstructors.FindControl("checkInBtn") as Button).Text = "New text";
}

2)

protected void CheckInBtn_Click(object sender, EventArgs e)
{
    var ctrl = (Control)sender;
    var lvi = (ListViewItem)ctrl.NamingContainer;
    var checkInBtn = (IButtonControl)lvi.FindControl("checkInBtn");
    checkInBtn.Text = "New Text";
}

but neither of these options have worked. Any ideas/tips? Thank You!

Heres my entire ListView:

<asp:ListView
   ID="lvInstructors"
   runat="server"
   AutoGenerateColumns="False"
   ShowRegularGridWhenEmpty="False"
    EmptyDataText="No Sessions to Display."
    OnRowDataBound="lvDataBound"
   OnRowCommand="lvCommand"
   Visible="true">
      <LayoutTemplate>
          <div class="container" id="mainContent">
          <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
          </div>
      </LayoutTemplate>

       <ItemTemplate>
           <div class="row 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>

Solution

  • You can always cast the sender back to a Button.

    protected void checkInBtn_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        btn.Text = "New text";
    }
    

    The reason your 2 snippets don't work is because the items in the ListView are index based. You would need to do something like this.

    Button btn = ListView1.Items[i].FindControl("checkInBtn") as Button;