简体   繁体   中英

Conditionally change Repeater DropDownList background color?

We are trying to change the background color of Repeater DropDownList to Gray when RadioButtonList selectedItem is New.

Otherwise, keep the background as White.

The following code keeps the background to Gray whether RadioButtonList selectedItem is New or Used.

What am I missing?

//css

<style>
    .disabledcss
    {
        background-color: #F9F9F9;
        color: blue;
        border: 1px solid gray;
        color: Gray;
    }
</style>
<style>
    .enabledcss
    {
        background-color: #fff;
        color: blue;
        border: 1px solid white;
        color: Gray;
    }
</style>

//markup:

<tr>
<td>
Item Type:<asp:RadioButtonList ID="rblPType" runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;"  AutoPostBack="true" OnSelectedIndexChanged="rblPurchaseType_SelectedIndexChanged">
<asp:ListItem Text="New" />
<asp:ListItem Text="Used" />
</asp:RadioButtonList><br />
<asp:RequiredFieldValidator style="color:#ff0000;" id="RequiredFieldValidator1"  ControlToValidate="rblPurchaseType" ErrorMessage="Please choose New or Used"  ValidationGroup ="stype" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td colspan="2">
<asp:Panel ID="uPanel" runat="server" Enabled="false">
STATE: <asp:DropDownList ID="ddlState" cssClass="disabledcss enabledcss" runat="server" AppendDataBoundItems="True">
  <asp:ListItem Value="" Selected="True"></asp:ListItem>
  </asp:DropDownList>    
 </div></span></asp:Panel>
</td>
</tr>

C# in ItemDataBound event

RadioButtonList rbPurchase = e.Item.FindControl("rblPType") as RadioButtonList;

foreach (RepeaterItem ReapterItem in Repeater2.Items)
{
    var rblType = (DropDownList)e.Item.FindControl("ddlState");
    if (rblType.Enabled == false)
    {
        rblType.CssClass = "disabledcss";
    }
    else
    {
        rblType.CssClass = "enabledcss";
    }
}

Note: I have seen about three examples on this forum that do not apply to myself. Thank you

You're using both css classes to style the drop downlist.

cssClass="disabledcss enabledcss"

You need to clarify the use of one or the other - this can be used with JQuery - adding and removing classes from the dropdownlist. Or try something like this:

if (rblType.Enabled == false)
{
    // rblType.CssClass = "disabledcss";
    rblType.CssClass = rblType.CssClass.Replace("enabledcss", "disabledcss");
}
else
{
    // rblType.CssClass = "enabledcss";
    rblType.CssClass = rblType.CssClass.Replace("disabledcss", "enabledcss");
} 

Just have one css class in your dropdownlist.

<asp:DropDownList ID="ddlState" cssClass="disabledcss" runat="server" AppendDataBoundItems="True">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM