【问题标题】:how to change change the colour of dropdown in Asp.net?如何更改 Asp.net 中下拉菜单的颜色?
【发布时间】:2017-08-23 05:47:03
【问题描述】:

我有一个下拉列表 cboVendor 我想将下拉列表中的数据放入 来自ELSE 的红色部分在下面的code.but 问题 是我在 if-else 之后绑定数据。

.aspx

<asp:DropDownList ID="cboVendor" runat="server" AppendDataBoundItems="True"
AutoPostBack="true"> <asp:ListItem Value="0">- Select Vendor -</asp:ListItem>
</asp:DropDownList>

c#代码

if (CheckBox1.Checked == true) {
    cboVendor.DataSource = DAL.CertificationDA.GetFullAccreditedVendors(vendid);
    cboVendor.DataTextField = "SupplierName";
    cboVendor.DataValueField = "SupplierID";
} else {
    cboVendor.DataSource = Supplier.GetSuppliersForSite(UserWrapper.GetCurrentUser.GetValidLocations.wsm_Ref_Buildings.FindByBuildingId(cboBuilding.SelectedValue).SiteId);
    cboVendor.DataTextField = "SupplierName";
    cboVendor.DataValueField = "SupplierID";
}
cboVendor.DataBind();

【问题讨论】:

  • DDL 在 HTML 中呈现为 Select,因此您可以定义 CSS 类 vendor-list option { color: red } 并使用 CssClass 属性在 else 块中将此类分配给 cboVendor
  • 写你的 else 块 cboVendor.BackColor = System.Drawing.Color.Red;

标签: c# asp.net dropdown


【解决方案1】:

绑定数据后,您可以遍历列表项,您可以像这样为这些项设置样式或类

  if(Checkbox.Checked!=true){
       foreach(ListItem item in cboVendor.Items) {   
                 item.Attributes.Add("style", "background-color:red;");  
     }  
   }

【讨论】:

  • 实际上两个 SP 都返回不同的列集,我希望其他部分中的红色
  • 你可以在上面加个检查
  • 你可以在上面加个检查
  • 然后接受这个作为答案,这样其他人就不会浪费时间阅读这个问题
猜你喜欢
  • 1970-01-01
  • 2014-03-14
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
相关资源
最近更新 更多