简体   繁体   English

有条件地更改Repeater DropDownList的背景色吗?

[英]Conditionally change Repeater DropDownList background color?

We are trying to change the background color of Repeater DropDownList to Gray when RadioButtonList selectedItem is New. 当RadioButtonList selectedItem为New时,我们试图将Repeater DropDownList的背景颜色更改为Gray。

Otherwise, keep the background as White. 否则,将背景保持为白色。

The following code keeps the background to Gray whether RadioButtonList selectedItem is New or Used. 无论RadioButtonList selectedItem是New还是Used,以下代码都会使背景保持灰色。

What am I missing? 我想念什么?

//css // 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 ItemDataBound事件中的C#

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. 您正在使用两个CSS类来设置下拉列表的样式。

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. 您需要弄清使用哪一种-可以与JQuery一起使用-从下拉列表中添加和删除类。 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. 在您的下拉列表中只有一个CSS类。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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