开发者

Bind multiple value to DropDownList using LINQ?

I have one string collection and I am executing foreach loop on the string collection. So in foreach I want to bind value to dropdownlist using LINQ query. That time for loop executed without error, but binding in dropdownlist is overriding the value.

Please check my snippet:

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";
      开发者_JAVA技巧  drpvendor.DataValueField = "ID";
        drpvendor.DataBind();
    }
     drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
}

For some reason the for loop for StringCollection overrides the value in dropdownlist. So how could I bind in another way?


The reason that the value gets overwritten is because you are doing the binding inside the foreach. Basically on first foreach cycle you are getting first userdeails and then binding it to the DDL, on next cycle you are binding the 2nd userdetails to the DDL and so on, so what you get in the end is just the last userdetails in the DDL.

What you need to do is to get all the userdetails in a collection and then databind the DDL onto that collection.

You need to do something like this:

[Index.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"));
        }
    }
}

[Index.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>


Hy

if you want to use just one query with LINQ using stringcollection, check the next exemple

    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"));

    }

-------Howto i tested the previous one

In order to test this function i produced some hardcoded data

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);

}

And i defined a class to bypass your datacontext

class UserDetails
    {
        public string UserType;

        public string Email;

        public string FirstName;

        public string UserId;
    }


Thanks for Respond me.. i did another way when i get userdetails from Query its override on Dropdownlist on below my Question so i had just add in Foreach loop

 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();

            }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜