复选框样式属性不会从后面的代码更改

时间:2015-06-18 10:00:07

标签: c# asp.net

我已将style="display:none"添加到CheckBox。在DropDownList1_SelectedIndexChanged事件中,我已更改为display:block。但它没有改为block

<div class="form-group">
    <asp:CheckBox ID="CheckBox1" runat="server" Text="Mark as close" style="display:none"/>
</div>

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     string s = DropDownList1.SelectedItem.Value;
     if (s == "3")
     {
         CheckBox1.Style.Add("display", "block");
         //  CheckBox1.Attributes.Add("Style", "display:block");
     }
     else
     {
         CheckBox1.Style.Add("display", "none");                    
     }        
}

2 个答案:

答案 0 :(得分:0)

您需要添加样式,如下所示: -

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = DropDownList1.SelectedItem.Value;
    if (s == "3")
    {
       CheckBox1.Attributes["style"] = "display:block;";
    }
    else
    {
       CheckBox1.Attributes["style"] = "display:none;";
    }
}

答案 1 :(得分:0)

试试这个

CheckBox1.Style.Add(HtmlTextWriterStyle.Display, "block");