RadioButtonList等數據源的綁定案例

 public class M_DATA
    {
        string key;

        public string Key
        {
            get { return key; }
            set { key = value; }
        }
        string value;

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Hastable構造的數據源
        Hashtable m_data = new Hashtable();
        for (int i = 0; i < 10; i++)
        {
            m_data.Add(i, "Txt" + i.ToString());
        }
        CheckBoxList1.DataSource = m_data;
        CheckBoxList1.DataTextField = "key";
        CheckBoxList1.DataValueField = "value";
        CheckBoxList1.DataBind();
        //類定義的構造的數據源
        List<M_DATA> m_list = new List<M_DATA>();
        for (int j = 0; j < 10; j++)
        {
            M_DATA m_c = new M_DATA();
            m_c.Key = j.ToString();
            m_c.Value = "TxtClass" + j.ToString();
            m_list.Add(m_c);

        }

        RadioButtonList2.DataSource = m_list;
        RadioButtonList2.DataMember = "M_DATA";
        RadioButtonList2.DataTextField = "value";
        RadioButtonList2.DataValueField = "key";
        RadioButtonList2.DataBind();

            
    }

發佈了32 篇原創文章 · 獲贊 11 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章