0

これ

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> 

<label>自動生成されたタグに cssClass を追加できますか?

4

2 に答える 2

2

hereまたはhereRadioButtonListに示すように、から継承できます。

最後のリンクは、おそらくあなたが望むものとより一致しています。

次のように、RenderItem メソッドをオーバーライドするだけで済みます。

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);
}

上記のコードを実際にコンパイルしたわけではありませんが、適切な方向に導くには十分なはずです:)

于 2009-12-02T09:31:09.357 に答える
0

私は ASP.NET の専門家ではありませんが、子セレクターを使用して、既知の ID を持つ要素内の LABEL の表示を簡単に制御できることは知っています。

#myElementID LABEL {
  padding: 5px;
}
于 2009-12-02T09:23:17.683 に答える