我想让“ CheckIn”按钮的颜色显示为绿色,具体取决于代码中其他数据的值,但是我无法在其onClick方法之外访问该按钮。我应该能够通过其ID访问它,但由于某种原因无法访问
<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">
<asp:HiddenField ID="sessionID" runat="server" Value='<%#Eval("SessionID")%>' />
<asp:HiddenField ID="hasChckedIn" runat="server" Value='<%#Eval("hasCheckedIn")%>' />
<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>
我如何在onClick()中访问它:
protected void CheckInBtn_Click(object sender, EventArgs e)
{
Button checkInBtn = (Button)sender;
checkInBtn.Text = "Check-Out";
checkInBtn.BackColor = Color.Green;
...
}
(侧问:当我单击该按钮时,为什么SelectedIndex返回-1?)
答案 0 :(得分:1)
在itemwDataBound事件中,查找所需的值,然后在此处设置按钮颜色。您也可以在按钮命令参数中设置值,然后在click事件中获取它。
答案 1 :(得分:1)
您需要访问Button
的{{1}}事件上的ItemDataBound
,以有条件地更改ListView
的颜色。
以下是可以帮助您的链接:
datalist itemdatabound event having issues changing item bg color on condition