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

Html 在运行时将属性添加到复选框列表,html,asp.net,forms,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.dropDownLis

因此,我有一些基于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)
Me.radioButtonList=新的radioButtonList()
Me.dropDownList=新的dropDownList()
Me.listControl=无
如果controlType=“dropdown”,则
Me.listControl=Me.dropDownList
其他的
Me.listControl=Me.radioButtonList
如果结束
对于每一个答案,请使用strAnswers
Dim newListItem=新列表项(ansElement.AnswerText,ansElement.AnswerText)
如果AnseElement.ActionID不是Nothing,也不是AnseElement.ActionID“”,则
newListItem.Attributes.Add(“actionID”,AnElement.actionID)
如果结束
Me.listControl.Items.Add(newListItem)
下一个
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标记,我必须做什么


谢谢

我想你说的是RadioButtonList。问题是它使用RadioButton控件,并且它有3个属性-属性、InputAttribute和LabelAttribute。它们中的每一个都用于特定的html元素

RadioButtonList的问题在于,它只使用Attributes属性,而不使用InputAttribute。以下是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就是那个单选按钮,它只指定Attributes属性并忽略InputAttribute

我可以建议修复它的方法——您可以创建继承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才能更新InputTTRubutes属性。所以我们调用它一次并发送一些存根HtmlTextWriter,这样它就不会显示两次。然后,我们将此控件作为控件[0]获取,并为每个ListItem更新InputAttribute值


抱歉,我没有使用VB.Net,所以控件是用C#

编写的,我想你说的是RadioButtonList。问题是它使用RadioButton控件,并且它有3个属性-属性、InputAttribute和LabelAttribute。它们中的每一个都用于特定的html元素

RadioButtonList的问题在于,它只使用Attributes属性,而不使用InputAttribute。以下是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就是那个单选按钮,它只指定Attributes属性并忽略InputAttribute

我可以建议修复它的方法——您可以创建继承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才能更新InputTTRubutes属性。所以我们调用它一次并发送一些存根HtmlTextWriter,这样它就不会显示两次。然后,我们将此控件作为控件[0]获取,并为每个ListItem更新InputAttribute值


另外,对不起,我没有使用VB.Net,所以控件是用C#

编写的。CheckboxList项是否也有同样的问题,因此也需要重写?还有,我如何防止它在两个位置都呈现属性?现在我得到span标签和input标签上的属性。这导致了另一组问题。是的,复选框列表也有同样的问题。但复选框列表使用复选框控件,而不是对项使用RadioButton控件的RadioButtonList。RadioButton继承了该复选框。我用示例更新了如何从span中删除属性的代码。基本上,您只需要调用item.Attributes.Clear();在你将它们复制到InputAttribute之后。太棒了,这对我来说绝对是完美的!回答得很好,谢谢你的帮助!RadioButtonList和CheckBoxList在默认情况下不公开属性,这让我感到奇怪。似乎需要很多东西。CheckboxList项是否也有同样的问题,因此也需要重写?还有,如何防止它在两个位置都呈现属性?现在我得到span标签和input标签上的属性。这导致了另一组问题。是的,复选框列表也有同样的问题。但复选框列表使用复选框控件,而不是对项使用RadioButton控件的RadioButtonList。RadioButton继承了该复选框。我用示例更新了如何从span中删除属性的代码。基本上,您只需要调用item.Attributes.Clear();在你将它们复制到InputAttribute之后。太棒了,这对我来说绝对是完美的!回答得很好,谢谢你的帮助!RadioButtonList和CheckBoxList在默认情况下不公开属性,这让我感到奇怪。似乎需要很多东西。