如何访问包装在Repeater中的Listview中的按钮?

时间:2019-09-13 18:27:00

标签: c# asp.net listview controls

我在ListView的ItemTemplate中有一个Button(Id =“ checkInBtn”),并且该ListView在UpdatePanel中。因此,我无法通过代码背后的ID访问Button(到目前为止,我只能通过eventHandler对其进行访问并将其转换为发送方as solved here,因此只能在以下情况下访问它:点击)

现在,我想从一个单独的方法访问此Button-我需要做的是在满足特定条件后让此Button“闪烁”。 还有其他方法可以访问此按钮控件吗?

ASPX:

<asp:UpdatePanel ID="UpdateList" runat="server">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="refreshInstructorList" EventName="Tick" />
                </Triggers>
                <ContentTemplate>
                    <asp:Timer ID="refreshInstructorList" runat="server" Interval="180000" OnTick="RefreshList"></asp:Timer> <!-- Every 3 minutes/ 180k ms -->

                    <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>

   </ContentTemplate>
</asp:UpdatePanel>

ASPX.CS :(如何填充Listview)

private void UpdateInstructorListView()
    {
        //Make Data table to hold ListViewItem Data 
        DataTable dt = new DataTable();
        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();
                InstructorHeadshot = I.Headshot;
                InstructorName = I.FirstName + " " + I.LastName;
                SessionInfo = S.Name + " , " + S.Room.ToString();


                //Fill rows in DataTable with variables
                dr = dt.NewRow();
                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();

    }

1 个答案:

答案 0 :(得分:0)

您必须多次使用FindControl。首先,在正确的Repeater Item中找到ListView。

var listview = Repeater1.Items[i].FindControl("ListView1") as ListView;

然后在正确的ListView项中找到Button。

var button = listview.Items[j].FindControl("checkInBtn") as Button;

现在您可以访问按钮了。

button.Text = "Button found!";

ASPX

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>

        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>

                <asp:Button ID="checkInBtn" runat="server" Text="Button" />

            </ItemTemplate>
        </asp:ListView>

    </ItemTemplate>
</asp:Repeater>