Objective: Modify the text color of an ASP Label based on the selection made in an ASP Dropdown ListItem. If the ListItem's value is false, then set the Label's text color to red; otherwise, keep it black.
Challenge: The color only changes when the page is refreshed or if the initial value is false from the database upon loading the page.
Approaches Attempted:
- Tried using both .Text and .SelectedValue
- Attempted to execute this during the DropdownList's OnSelectedIndexChanged event
- Explored other resources that suggest using javascript for changing color within the same control, rather than across different controls (e.g., ASP.NET Change link button color when clicked without post-back or update panel)
Is it feasible to achieve this solely with C#/ASP.NET without needing a page refresh? If implementing javascript is necessary, how can it be done from the Dropdownlist to the Label?
HTML:
<div>
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Image ID="img1" runat="server" height="20" width="20" CssClass="providericon"/><sup><asp:Label ID="myLabel" runat="server" Font-Size="20px" CssClass="oval"/></sup>
<asp:Label ID="myLabelMid" runat="server" CssClass="professorname"/>
<asp:DropdownList runat="server" id="ddlAvailability1" AutoPostBack="true" OnSelectedIndexChanged="ddlAvailability_OnSelectedIndexChanged" CssClass="dropdowns">
<asp:ListItem ID="LT1"></asp:ListItem>
<asp:ListItem ID="RT1"></asp:ListItem>
</asp:DropdownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:Panel ID="row2" runat="server" Visible="false">
<%--<asp:ScriptManager runat="server"></asp:ScriptManager>--%>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Image ID="img2" runat="server" CssClass="professoricon"/><sup><asp:Label ID="L2" runat="server" Font-Size="20px" CssClass="oval"/></sup> <asp:Label ID="M2" runat="server" CssClass="professorname"/>
<asp:DropdownList runat="server" id="ddlAvailability2" AutoPostBack="true" OnSelectedIndexChanged="ddlAvailability_OnSelectedIndexChanged" CssClass="dropdowns">
<asp:ListItem ID="LT2"></asp:ListItem>
<asp:ListItem ID="RT2"></asp:ListItem>
</asp:DropdownList>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
CSS:
.professorname {
margin-left: 15px;
position: absolute;
left: 55px;
}
.redtext {
color: red;
margin-left: 15px;
position: absolute;
left: 55px;
}
C# Code:
Page_Load(){
myLabelMid.CssClass = (ddlAvailability1.SelectedValue == "False") ? "redtext" : "professorname";
M2.CssClass = (ddlAvailability2.SelectedValue == "False") ? "redtext" : "professorname";
M3.CssClass = (ddlAvailability3.SelectedValue == "False") ? "redtext" : "professorname";
}
protected void ddlAvailability_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlSender = (DropDownList)sender;
int professorIndex = getProviderIndex(ddlSender.ClientID);
}
public int getProfessorIndex(string ddlSenderClientID)
{
int professorIndex = 0;
switch (ddlSenderClientID)
{
case "ddlAvailability1":
professorIndex = 0;
myLabelMid.CssClass = (ddlAvailability1.SelectedValue == "False") ? "redtext" : "professorname ";
break;
case "ddlAvailability2":
professorIndex = 1;
M2.CssClass = (ddlAvailability2.SelectedValue == "False") ? "redtext" : "professorname ";
break;
}
return professorIndex;
}