代码之家  ›  专栏  ›  技术社区  ›  Ian G

使用RadioButtonList时我可以控制标签吗?

  •  0
  • Ian G  · 技术社区  · 15 年前

    protected void Page_Load(object sender, EventArgs e) {
        RadioButtonList1.Items.Add(new ListItem("London","1"));
        RadioButtonList1.Items.Add(new ListItem("Paris", "2"));
    }
    

       <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatLayout="Flow">
       </asp:RadioButtonList></div>
    

    生成类似这样的HTML

       <input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="1" />
       <label for="RadioButtonList1_0">London</label>
       <input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="2" />
       <label for="RadioButtonList1_1">Paris</label>
    

    但我真正想要的是, 请注意中的类 <label> 标签。

        <input type="radio" name="Event" id="whatever" value="1" /> 
        <label for="London" class="London">London</label> 
        <input type="radio" name="Event" id="whatever" value="2" /> 
        <label for="Paris" class="Paris">Paris</label> 
    

    我可以将cssClass添加到自动生成的 <标签> 标签?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Sune Rievers    15 年前

    你可以继承 RadioButtonList ,如图所示 here here .

    最后一个链接可能更符合您的要求。

    protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
        RadioButton radioButton = new RadioButton();
        radioButton.Page = this.Page;
        radioButton.GroupName = this.UniqueID;
        radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
        radioButton.Text = this.Items[repeatIndex].Text;            
        radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
        radioButton.Checked = this.Items[repeatIndex].Selected;
        radioButton.TextAlign = this.TextAlign;
        radioButton.AutoPostBack = this.AutoPostBack;
        radioButton.TabIndex = this.TabIndex;
        radioButton.Enabled = this.Enabled;
    
        radioButton.CssClass = InnerCssClass;
        radioButton.Style.Add(HtmlTextWriterStyle.BackgroundColor, this.Items[repeatIndex].Text);
    
        radioButton.RenderControl(writer);
    
        // add new label
        Label radioLabel = new Label();
        radioLabel.Text = this.Items[repeatIndex].Text;
        radioLabel.CssClass = this.Items[repeatIndex].Text;
        radioLabel.AssociatedControlID = this.ClientID + "_" + repeatIndex.ToString();
        radioLabel.RenderControl(writer);
    }
    

    请注意,我并没有实际编译上述代码,但应该足以指导您正确的方向:)

        2
  •  0
  •   Alex Weinstein    15 年前

    #myElementID LABEL {
      padding: 5px;
    }