日期:2014-05-18  浏览次数:20665 次

DropDownList动态绑定下拉菜单的CS类方法
各路大侠请帮忙,如题:DropDownList动态绑定下拉菜单CS类方法,能让在多个页共同调用的方法.谢谢,在线等!!

------解决方案--------------------
传参数,返回ds
------解决方案--------------------
C# code
/// <summary>
        /// 根据传入的语句,得到DataSet
        /// </summary>
        /// <param name="strsql"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string strsql)
        {
            //this.Reset();

            cmd = new SqlCommand("", conn);
            cmd.CommandText = strsql;
            DataSet ds = new DataSet();
            da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(ds);
            return ds;
        }

        /// <summary>
        /// 根据传入的语句,得到DataTable
        /// </summary>
        /// <param name="strsql"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string strsql)
        {
            DataSet ds = GetDataSet(strsql);
            return ds.Tables[0];
        }

------解决方案--------------------
C# code

public Dictionary<string, A> GetDDL()
{
Dictionary<string, A> d = new Dictionary<string, A>();  
            d.Add("aa", new A("1", "xx"));  
            d.Add("bb", new A("2", "yy"));  
            d.Add("cc", new A("3", "zz"));  
            return d;
   
} 
            //DataList1.DataSource = d;  
            //DataList1.DataBind(); 
            //绑定。

------解决方案--------------------
调用:

DropDownListData(this.DropDownList2,参数,参数)



把下面的代码写入一个公共类
public static void DropDownListData(System.Web.UI.WebControls.DropDownList dd,string kindno,string dept)
{
dd.DataSource = DAL.DBAccess.Qery(select * from 表)//在这里绑定dataset
dd.DataTextField = "kind_name";
dd.DataValueField = "kind_no";
dd.DataBind();
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Dispose();
connection.Close();
}
return ds;
}
}
}