2013-12-18 75 views
0

因此,我有一些基于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) 

问题是,当我运行的代码与页面呈现的属性被添加到控制不是输入项目的进行跨标签本身。所以呈现的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标记?

谢谢!

回答

4

我想你是在谈论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是单选按钮,而且只规定了财产的属性,而忽略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财产,为使用它在第一次访问运行时创建单选按钮控制,所以我们需要调用RenderItem才可以更新InputAttrubutes属性。所以我们调用它一次并发送一些存根HtmlTextWriter,所以它不会显示两次。然后,我们只需将这个控件作为Controls [0],并且为每个ListItem我们更新InputAttributes值。

PS。对不起,我没有使用VB.Net所以控制是用C#编写的。

+0

CheckboxList项是否遭受同样的问题,因此也需要覆盖? –

+0

另外,如何防止它在两个位置呈现属性呢?现在我正在获取span标签和输入标签的属性。所以这是另一个问题。 –

+0

是的,CheckBoxList有同样的问题。但是,对于使用RadioButton控件的RadioButtonList,CheckBoxList使用CheckBox控件。 RadioButton继承了CheckBox。 –