2013-12-18 79 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。 –