Предположим, вы говорите о RadioButtonList. Проблема заключается в том, что он использует элемент управления RadioButton и имеет 3 свойства атрибутов - Атрибуты, InputAttributes и LabelAttributes. Каждый из них используется для конкретного элемента html.
Проблема с RadioButtonList заключается в том, что он использует только свойство Attributes и не использует InputAttributes. Вот код метода RadioButtonList.RenderItem:
protected virtual void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
if (repeatIndex == 0)
{
this._cachedIsEnabled = this.IsEnabled;
this._cachedRegisterEnabled = this.Page != null && !this.SaveSelectedIndicesViewState;
}
RadioButton controlToRepeat = this.ControlToRepeat;
int index1 = repeatIndex + this._offset;
ListItem listItem = this.Items[index1];
controlToRepeat.Attributes.Clear();
if (listItem.HasAttributes)
{
foreach (string index2 in (IEnumerable) listItem.Attributes.Keys)
controlToRepeat.Attributes[index2] = listItem.Attributes[index2];
}
if (!string.IsNullOrEmpty(controlToRepeat.CssClass))
controlToRepeat.CssClass = "";
ListControl.SetControlToRepeatID((Control) this, (Control) controlToRepeat, index1);
controlToRepeat.Text = listItem.Text;
controlToRepeat.Attributes["value"] = listItem.Value;
controlToRepeat.Checked = listItem.Selected;
controlToRepeat.Enabled = this._cachedIsEnabled && listItem.Enabled;
controlToRepeat.TextAlign = this.TextAlign;
controlToRepeat.RenderControl(writer);
if (!controlToRepeat.Enabled || !this._cachedRegisterEnabled || this.Page == null)
return;
this.Page.RegisterEnabledControl((Control) controlToRepeat);
}
controlToRepeat что RadioButton, и он определяет только атрибуты свойства и игнорирует InputAttributes.
Я могу предложить способ исправить это - вы можете создать новый класс, который наследует RadioButtonList, и использовать его вместо стандартного. Вот код этого класса:
public class MyRadioButtonList : RadioButtonList
{
private bool isFirstItem = true;
protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
if (isFirstItem)
{
// this.ControlToRepeat will be created during this first call, and then it will be placed into Controls[0], so we can get it from here and update for each item.
var writerStub = new HtmlTextWriter(new StringWriter());
base.RenderItem(itemType, repeatIndex, repeatInfo, writerStub);
isFirstItem = false;
}
var radioButton = this.Controls[0] as RadioButton;
radioButton.InputAttributes.Clear();
var item = Items[repeatIndex];
foreach (string attribute in item.Attributes.Keys)
{
radioButton.InputAttributes.Add(attribute, item.Attributes[attribute]);
}
// if you want to clear attributes for top element, in that case it's a span, then you need to call
item.Attributes.Clear();
base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
}
}
Немного описания - он имеет isFirstItem свойство, как контроль RadioButton, который используется ею создается во время выполнения в первом доступе, поэтому мы должны вызвать RenderItem, прежде чем мы сможем обновить Свойство InputAttrubutes. Поэтому мы вызываем его один раз и отправляем некоторый заглушка HtmlTextWriter, поэтому он не будет отображаться дважды. И затем после этого мы просто получаем этот элемент управления как Controls [0], и для каждого ListItem мы обновляем значения InputAttributes.
PS. Извините, я не использовал VB.Net, поэтому управление написано на C#
Имеет ли элемент CheckboxList одну и ту же проблему, и, следовательно, необходимо также переопределить? –
Также как я могу предотвратить его отображение атрибута в обоих местах? Теперь я получаю атрибуты в теге span и теге ввода. Таким образом, возникает другой набор проблем. –
Да, CheckBoxList имеет ту же проблему. Но вместо RadioButtonList, который использует элемент управления RadioButton для элемента, CheckBoxList использует элемент управления CheckBox. И RadioButton наследует этот CheckBox. –