在运行时将属性添加到复选框列表

时间:2013-12-18 21:22:14

标签: html asp.net forms

所以我有一些代码可以动态创建基于XML输入文件的ASP.NET表单。我正在尝试在运行时向控件添加属性,我对列表项有一些奇怪的问题。

我的服务器端代码如下所示:

Me.radioButtonList = New RadioButtonList()
Me.dropDownList = New DropDownList()
Me.listControl = Nothing
If controlType = "dropdown" Then
    Me.listControl = Me.dropDownList
Else
    Me.listControl = Me.radioButtonList
End If
For Each ansElement As Answer In strAnswers
    Dim newListItem = New ListItem(ansElement.AnswerText, ansElement.AnswerText)
    If ansElement.ActionID IsNot Nothing AndAlso ansElement.ActionID <> "" Then
        newListItem.Attributes.Add("actionID", ansElement.ActionID)
    End If
    Me.listControl.Items.Add(newListItem)
Next
Me.listControl.ID = controlID
Me.Controls.Add(Me.listControl)

问题是当我运行代码并且页面呈现时,属性被添加到控件的前进span标记而不是输入项本身。因此渲染的HTML最终看起来像这样。

<span actionID="1">
    <input id="lst_dynamic_MedIllnesses_0" name="ctl00$MainContentPlaceHolder$FormGenerator1$lst_dynamic_MedIllnesses$lst_dynamic_MedIllnesses_0" value="None" type="checkbox">
    <label for="lst_dynamic_MedIllnesses_0">None</label>
</span>

我需要做些什么才能将 actionID 属性添加到实际输入控件而不是span标记?

谢谢!

1 个答案:

答案 0 :(得分:4)

我想你在谈论RadioButtonList。它的问题是它使用RadioButton控件,它有3个属性属性 - Attributes,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,它只指定Attributes属性并忽略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控件是在第一次访问时在运行时创建的,所以我们需要在更新InputAttrubutes属性之前调用RenderItem。所以我们调用它一次并发送一些存根HtmlTextWriter,因此它不会显示两次。之后我们将此控件作为Controls [0],并为每个ListItem更新InputAttributes值。

PS。对不起,我没有使用VB.Net,因此控件是用C#编写的