绑定 DropDownList

绑定 DropDownList

注:ds为DataSet

            DataTable table=ds.Tables["mytable"];
            
foreach(DataRow row in table.Rows)
            
{
                mylist.Items.Add(
new ListItem(row[1].ToString(),row[0].ToString()));
            }
也可以用
            mylist.DataSource=ds.Tables["mytable"].DefaultView;
            mylist.DataTextField
="tname";
            mylist.DataValueField
="tid";
            mylist.DataBind();

注:BindList()最好写在OnInit中

        #region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            myConn
=new SqlConnection(strConn);
            
base.OnInit(e);
            BindList();
        }
posted @ 2008-10-05 10:20  hb  阅读(176)  评论(0编辑  收藏  举报