0

各リストアイテムにGroupName属性を追加するために、RadioButtonListから継承するクラスを作成しました。(なぜそれがまだそこになかったのか私にはわかりません)。

これは、レンダリング時に期待どおりに機能しますが、選択したアイテムをポストバックで保持しません。

public class GroupedRadioButtonList : RadioButtonList
{
    [Bindable(true), Description("GroupName for all radio buttons in list.")]
    public string GroupName
    {
        get;
        set;
    }

    protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, System.Web.UI.HtmlTextWriter writer)
    {
        RadioButton radioButton = new RadioButton();
        radioButton.Page = this.Page;
        radioButton.GroupName = this.GroupName;
        radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
        radioButton.Text = this.Items[repeatIndex].Text;
        radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
        radioButton.Checked = this.Items[repeatIndex].Selected;
        radioButton.TextAlign = this.TextAlign;
        radioButton.AutoPostBack = this.AutoPostBack;
        radioButton.TabIndex = this.TabIndex;
        radioButton.Enabled = this.Enabled;            
        radioButton.RenderControl(writer);

    }
}

誰かが私が欠けているものを知っていますか?

ありがとう。

4

1 に答える 1

0

IPostBackDataHandlerインターフェイスを実装し、いくつかのメソッドを処理する必要があります。RadioButton同様のコントロールで使用したものを提供します。リストではなく、個々のコントロールを拡張しただけです。ボタンにカスタム属性ValueGroupName属性を追加し、ポストバック時に値を初期化しました。

#region IPostBackDataHandler Members
    void IPostBackDataHandler.RaisePostDataChangedEvent()
    {
        OnCheckedChanged(EventArgs.Empty);
    }

    bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
    {
        bool result = false;
        //check if a radio button in this button's group was selected
        string value = postCollection[GroupName];
        if ((value != null) && (value == Value)) //was the current radio selected?
        {
            if (!Checked) //select it if not already so
            {
                Checked = true;
                result = true;
            }
        }
        else //nothing or other radio in the group was selected
        {
            if (Checked) //initialize the correct select state
            {
                Checked = false;
            }
        }
        return result;
    }
#endregion

あまり最適化されていないコードについてお詫びします:)

于 2010-09-06T15:15:16.410 に答える