How to remove span tag from WebControl when rendered

后端 未结 15 856
生来不讨喜
生来不讨喜 2020-12-31 03:34

When using an ASP.NET CheckBox (and in out case, inherited from a CheckBox) it renders a span around the checkbox input control, this span control

相关标签:
15条回答
  • 2020-12-31 04:25

    Found this useful tip:

    In Code Behind, use InputAttributes instead of Attributes.

    For Example, type this:

    chk.InputAttributes.Add("onchange", "updateFields()")
    

    instead of this:

    chk.Attributes.Add("onchange", "updateFields()")
    

    or instead of inline declarations:

    <asp:CheckBox ID="chk" runat="server" onchange="updateFields()" />
    

    The last two will cause the wrapping span.

    0 讨论(0)
  • 2020-12-31 04:26

    I've found that by implementing a constructor like the one below, you can specify the container tag for your control.

    public MyCustomControl() : base(HtmlTextWriterTag.Div)
    {
    }
    

    You can replace the HtmlTextWriterTag with any of the aviable options, such as Div in the example above. The default is Span.

    0 讨论(0)
  • 2020-12-31 04:29

    $(document).ready(function() {
      /* remove the relative spam involving inputs disabled */
      $('input[type="checkbox"]').parent('.aspNetDisabled').each(function() {
        var $this = $(this);
        var cssClass = $this.attr('class');
        $this.children('input[type="checkbox"]').addClass(cssClass).unwrap().parent('label[for],span').first().addClass('css-input-disabled');
      });
    });
    /* CSS Example */
    .css-input {
      position: relative;
      display: inline-block;
      margin: 2px 0;
      font-weight: 400;
      cursor: pointer;
    }
    .css-input input {
      position: absolute;
      opacity: 0;
    }
    .css-input input:focus + span {
      box-shadow: 0 0 3px rgba(0, 0, 0, 0.25);
    }
    .css-input input + span {
      position: relative;
      display: inline-block;
      margin-top: -2px;
      margin-right: 3px;
      vertical-align: middle;
    }
    .css-input input + span:after {
      position: absolute;
      content: "";
    }
    .css-input-disabled {
      opacity: .5;
      cursor: not-allowed;
    }
    .css-checkbox {
      margin: 7px 0;
    }
    .css-checkbox input + span {
      width: 20px;
      height: 20px;
      background-color: #fff;
      border: 1px solid #ffffd;
      -webkit-transition: background-color 0.2s;
      transition: background-color 0.2s;
    }
    .css-checkbox input + span:after {
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      font-family: "FontAwesome";
      font-size: 10px;
      color: #fff;
      line-height: 18px;
      content: "\f00c";
      text-align: center;
    }
    .css-checkbox:hover input + span {
      border-color: #ccc;
    }
    .css-checkbox-primary input:checked + span {
      background-color: #5c90d2;
      border-color: #5c90d2;
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <!-- Generate from asp.net -->
    <label for="CheckBox1" id="Label4" class="css-input css-checkbox css-checkbox-primary">
      <span class="aspNetDisabled">
        <input id="CheckBox1" 
               type="checkbox" 
               checked="checked" 
               disabled="disabled">
      </span>
      <span></span>Disabled
    </label>

    0 讨论(0)
提交回复
热议问题