2017-02-23 20 views
1
/*data access layer */ 
public DataSet getdata(string procedurename, SqlParameter[] param) 
{ 
    try 
    { 
      SqlCommand command; 
      command = new SqlCommand(procedurename, connection); 
      command.CommandType = CommandType.StoredProcedure; 
      SqlDataAdapter adapter = new SqlDataAdapter(command); 
      DataSet set = new DataSet(); 
      if (param != null) 
      { 
      for (int i = 0; i < param.Length; i++) 
      { 
       command.Parameters.Add(param[i]); 
      } 
      } 
      adapter.Fill(set); 
      return set; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      closeConnection(); 
     } 
    } 

中间层:与泛型列表绑定下拉列表中的三层架构

public class dropdownbind 
    { 
     private string _itemName; 
     private int _itemvalue; 

     public int Itemvalue 
     { 
      get { return _itemvalue; } 
      set { _itemvalue = value; } 
     } 

     public string ItemName 
     { 
      get { return _itemName; } 
      set { _itemName = value; } 
     } 

     public List<dropdownbind> getDepartment() 
     { 
      DBlist obj = new DBlist(); 
      DataSet ds = obj.getdata("str_getdepartment",null); 
      List<dropdownbind> departments = new List<dropdownbind>(); 
      foreach (DataRow orow in ds.Tables[0].Rows) 
      { 
       dropdownbind dlist = new dropdownbind(); 
       dlist.ItemName = orow["deparment_name"].ToString(); 
       dlist.Itemvalue = int.Parse(orow["id"].ToString()); 
       departments.Add(dlist); 
      } 
      return departments; 
     } 
    } 

UI: -

protected void BindDdlList() 
    { 
     dropdownbind dlist = new dropdownbind(); 
     List<dropdownbind> departments = dlist.getDepartment(); 
     ddlEmpDepatment.DataSource = departments; 
     ddlEmpDepatment.DataTextField = dlist.ItemName; 
     ddlEmpDepatment.DataValueField = dlist.Itemvalue.ToString(); 
     ddlEmpDepatment.DataBind(); 

    } 

我试图绑定部门采用三层架构到下拉列表。 但这段代码不起作用,它显示文本字段中的middletier.dropdownbind。

+0

你有机会看我的答案吗?这是否解决了你的问题? –

回答

0

您需要更正DataTextField & DataValueField性质是这样的: -

ddlEmpDepatment.DataValueField = "Itemvalue"; 
ddlEmpDepatment.DataTextField = "ItemName"; 

您指定属性名,而你需要指定属性名作为string