2009-12-02 2 views
0

ЭтоМожно ли управлять ярлыками при использовании RadioButtonLists?

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> тег?

ответ

1

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

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

Вы должны только переопределить метод 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

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

#myElementID LABEL { 
    padding: 5px; 
} 
+0

Да, спасибо, это идея, которую я могу выдвинуть, но дизайнеры могут быть такими суетливыми: D – inspite

Смежные вопросы