从List集合c#/ asp.net填充Dropdownlist

时间:2012-09-24 14:06:21

标签: c# asp.net

我有一个名为memberdetails和memberdb的类,它们是我的类库的一部分。我使用带有MSSQL的asp.net C#作为后端

我的memberdetails类

     public int MemID
     {
        get { return memID; }      
     }

     public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

     public MemberDetails(int memID, string firstName)
        {
        this.memID = memID;
        this.firstName = firstName;
        }

memberdb class

public List<MemberDetails> MemberResult(int memid)
    {  
        using (SqlConnection con = new SqlConnection(connectionString))         
        using (SqlCommand cmd = con.CreateCommand())
        {
            List<MemberDetails> memberdetails = new List<MemberDetails>();
            MemberDetails member;

            try
            {
                con.Open();
                cmd.CommandText = "usp_Member_Result";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@memgen", memid);

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        member = new MemberDetails((int)reader["memid"],  (string)reader["firstname"]);
                        memberdetails.Add(member);

                    }
                    reader.Close();
                    return memberdetails;                          
                 }
            }
            catch (SqlException err)
            {
                throw new ApplicationException("Data error.", err);
            }
            finally
            {            
                if (con != null)
                    con.Close();
            }
        }
    }

在将这些类编译成DLL然后将其作为对项目的引用添加后,我试图绑定我的DropDownlist以检索这些值。在代码隐藏页面中,我从gridview获取一个值,将其放入变量“memgen”,将其发送到我的方法然后返回结果。当我调试我的应用程序时,我看到返回的值,但它们没有绑定到DropDownList。

CodeBehind Page

 member.MemberResult(memgen);
   // MemberDetails details = new MemberDetails();

    ddlFamilyMembers.DataTextField = "Text";
    ddlFamilyMembers.DataValueField = "Value"; 
  //  ddlFamilyMembers.DataSource = memberdetails; << "This does not exist in the current context"
    ddlFamilyMembers.DataBind();

3 个答案:

答案 0 :(得分:3)

您的代码隐藏需要更改为:

List<MemberDetails> details = member.MemberResult(memgen); 

ddlFamilyMembers.DataTextField = "FirstName"; 
ddlFamilyMembers.DataValueField = "MemID";  
ddlFamilyMembers.DataSource = details;
ddlFamilyMembers.DataBind(); 

具体变化:

  1. 捕获MemberResult
  2. 的结果
  3. 使用DataTextFieldDataValueField
  4. 的正确字段名称
  5. 设置DataSource属性

答案 1 :(得分:0)

我认为问题是您忘记添加下拉列表的数据源。

您可以执行以下操作:

var ddlvals = member.MemberResult(memgen); 

ddlFamilyMembers.DataTextField = "FirstName"; 
ddlFamilyMembers.DataValueField = "MemID";  
ddlFamilyMembers.DataSource = ddlvals;
ddlFamilyMembers.DataBind();

没有绑定就行不通。我希望这可以帮助你解决问题。

答案 2 :(得分:0)

List<MemberDetails> details = member.MemberResult(memgen); 

ddlFamilyMembers.DataTextField = "FirstName"; 
ddlFamilyMembers.DataValueField = "MemID";  
ddlFamilyMembers.DataSource = details;
ddlFamilyMembers.DataBind();