Могу ли я контролировать метки при использовании RadioButtonLists? - PullRequest
0 голосов
/ 02 декабря 2009

Это

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 к автоматически сгенерированному тегу <label>?

Ответы [ 2 ]

2 голосов
/ 02 декабря 2009

Вы можете наследовать от RadioButtonList, как показано здесь или здесь .

Последняя ссылка, вероятно, более соответствует тому, что вы хотите.

Вам нужно только переопределить метод 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);
}

Обратите внимание, я на самом деле не скомпилировал приведенный выше код, но этого должно быть достаточно, чтобы направить вас в нужном направлении:)

0 голосов
/ 02 декабря 2009

Я не ASP.NET pro, но я знаю, что вы можете легко контролировать представление LABEL внутри вашего элемента с известным идентификатором, используя дочерние селекторы.

#myElementID LABEL {
  padding: 5px;
}
...