【问题标题】:Bind multiple value to DropDownList using LINQ?使用 LINQ 将多个值绑定到 DropDownList?
【发布时间】:2011-09-15 07:54:06
【问题描述】:

我有一个字符串集合,我正在字符串集合上执行 foreach 循环。因此,在 foreach 中,我想使用 LINQ 查询将值绑定到下拉列表。那个时间 for 循环执行没有错误,但下拉列表中的绑定覆盖了该值。

请检查我的sn-p:

public void binddrop(StringCollection colle)
{
    foreach (string str in (StringCollection)colle)
    {
        string[] user = str.Split('|'); 

        iPhoneDataContext objdata = new iPhoneDataContext();

        var userdetails = (from users in objdata.UserDetails.AsEnumerable()
                           where users.UserType != null && users.Email==user[2].ToString()
                           select new
                           {
                               Name = users.FirstName,
                               ID = users.UserId
                           }) 

        drpvendor.DataSource = userdetails;
        drpvendor.DataTextField = "Name";
        drpvendor.DataValueField = "ID";
        drpvendor.DataBind();
    }
     drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
}

由于某种原因,StringCollection 的 for 循环会覆盖下拉列表中的值。那我怎么能用另一种方式绑定呢?

【问题讨论】:

  • 纯 linq 解决方案见我的帖子
  • 感谢您的 cmets..m Linq.i 中的新成员将参考您的帖子解决方案并解决我的问题:P

标签: c# asp.net visual-studio linq


【解决方案1】:

值被覆盖的原因是因为您在 foreach 中进行绑定。基本上在第一个 foreach 循环中,您将获得第一个 userdeails,然后将其绑定到 DDL,在下一个循环中,您将第二个 userdetails 绑定到 DDL 等等,所以您最终得到的只是 DDL 中的最后一个 userdetails。

您需要做的是获取集合中的所有用户详细信息,然后将 DDL 数据绑定到该集合。

你需要做这样的事情:

[索引.aspx.cs]

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace DropDownListBinding
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            binddrop();
        }

        public void binddrop()
        {
            // this is the collection that will be bound to the DDL
            var userdetailsCollection = new List<object>();

            // generate some userdetails and add them to the collection
            for (var i = 0; i < 3; i++)
            {
                var userdetails = new
                {
                    Name = "User" + i,
                    ID = i.ToString()
                };
                userdetailsCollection.Add(userdetails);
            }

            // now we can bind the DDL to the collection
            drpvendor.DataSource = userdetailsCollection;
            drpvendor.DataTextField = "Name";
            drpvendor.DataValueField = "ID";
            drpvendor.DataBind();

            drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
        }
    }
}

[索引.aspx]

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="DropDownListBinding.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:DropDownList runat = "server" ID="drpvendor" />
    </form>
</body>
</html>

【讨论】:

    【解决方案2】:

    Hy

    如果您只想使用字符串集合在 LINQ 中使用一个查询,请查看下一个示例

        public void binddrop(StringCollection colle)
        {
    
            var userdetails = 
            (
                from elem in colle.Cast<string>()
                from mail in elem.Split('|')
                join users in objdata on mail equals users.Email
                where users.UserType != null 
                   select new
                   {
                       Name = users.FirstName,
                       ID = users.UserId
                   }
            );
    
    
            drpvendor.DataSource = userdetails;
            drpvendor.DataTextField = "Name";
            drpvendor.DataValueField = "ID";
            drpvendor.DataBind();
    
    
            drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
    
        }
    

    -------我如何测试前一个

    为了测试这个功能,我制作了一些硬编码数据

    StringCollection colle = new StringCollection();

    IEnumerable objdata;

    protected void Page_Load(object sender, EventArgs e) {

    colle.Add("pippo@msn.it|pippo2@msn.it|pippo3@msn.it");
    colle.Add("pluto@msn.it|pluto2@msn.it|pluto3@msn.it");
    
    objdata = new UserDetails[] 
    {
        new UserDetails
        {
            UserType = "Normal",
            Email = "pippo3@msn.it",
            FirstName = "pippo",
            UserId = "pippo"
        },
        new UserDetails
        {
            UserType = "Normal",
            Email = "pluto3@msn.it",
            FirstName = "pluto",
            UserId = "pluto"
        }
    };
    
    binddrop(colle);
    

    }

    我定义了一个类来绕过你的数据上下文

    class UserDetails
        {
            public string UserType;
    
            public string Email;
    
            public string FirstName;
    
            public string UserId;
        }
    

    【讨论】:

      【解决方案3】:

      谢谢你回复我.. 当我从查询它的覆盖在我的问题下方的下拉列表中获取用户详细信息时,我做了另一种方式,所以我刚刚添加了 Foreach 循环

       public void binddrop(StringCollection colle)
        {
      foreach (string str in (StringCollection)colle)
      {
          string[] user = str.Split('|'); 
      
          iPhoneDataContext objdata = new iPhoneDataContext();
      
          var userdetails = (from users in objdata.UserDetails.AsEnumerable()
                             where users.UserType != null && users.Email==user[2].ToString()
                             select new
                             {
                                 Name = users.FirstName,
                                 ID = users.UserId
                             });
               foreach (var v1 in userdetails)
                  {
                      drpvendor.Items.Add(v1.Name);
                     drpvendor.Items.FindByText(v1.Name).Value = v1.ID.ToString();
      
                  }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 2010-09-26
        • 2012-06-08
        • 2010-09-13
        相关资源
        最近更新 更多