一、DropDownList绑定(自己做了测试验证没问题)

namespace NUCTechnologyPark
{
    public partial class CeShi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMenu();
            }
          
        }
        public void BindMenu()
        {
       
            ListItem list = new ListItem();
            list.Text = "请选择新闻类别";
            list.Value = "0";
            list.Enabled = true;
            list.Selected = true;
            DropDownList1.Items.Add(list);
            DataTable dt = B_News.GetClassNameList(111);
            //BindingUtils bind1 = new BindingUtils();
            //bind1.BindListControl(dt, DropDownList1, "Menu_Pk", "Class_Name");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListItem list1 = new ListItem();
                list1.Text = dr["Class_Name"].ToString();
                list1.Value = dr["Menu_Pk"].ToString();
                list1.Enabled = true;
                list1.Selected = false;
                DropDownList1.Items.Add(list1);
            }
        }
        //DropDownList选择事件
        protected void SelectIndexChanged_Click(object sender, EventArgs e)
        {
            this.Label1.Text = DropDownList1.SelectedValue;
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (this.DropDownList1.SelectedValue == "0")
            {
                MessageBox.Alter("请选择新闻类别","CeShi.aspx");
            }
        }
    }
}

二、RadioButtonList绑定

  DataTable dt = B_Storage.StorageList();
  BindingUtils bind = new BindingUtils();
  bind.BindListControl(dt, RadioButtonList1, "CIndex", "CName");

 /// <summary>
        /// 提供用来绑定所有列表形式控件的方法,如:CheckBoxList、DropDownList、ListBox 、RadioButtonList 类
        /// </summary>
        /// <param name="dt">要绑定的DataTable</param>
        /// <param name="ListControl1">要绑定的列表控件</param>
        /// <param name="DataValueField">各列项对应的数据字段</param>
        /// <param name="DataTextField">各列项对应的文本字段</param>      
        public void BindListControl(DataTable dt, ListControl ListControl1, string DataValueField, string DataTextField)
        {
            if (dt.Rows.Count > 0)
            {
                ListControl1.DataValueField = DataValueField;
                ListControl1.DataTextField = DataTextField;
                ListControl1.DataSource = dt;
                ListControl1.DataBind();
            }
        }

posted on 2012-06-28 16:00  微笑点燃希望  阅读(1046)  评论(0编辑  收藏  举报
font=white