我有一个字符串集合,我正在字符串集合上执行foreach循环。所以在foreach中我想使用LINQ查询将值绑定到dropdownlist。执行循环的时间没有错误,但是dropdownlist中的绑定会覆盖该值。
请查看我的代码:
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循环会覆盖下拉列表中的值。那我怎么能以另一种方式结合呢?
答案 0 :(得分:3)
值被覆盖的原因是因为你在foreach中进行绑定。基本上在第一个foreach循环中,您将获得第一个用户索引,然后将其绑定到DDL,在下一个循环中,您将第二个用户详细信息绑定到DDL,依此类推,因此您最终得到的只是DDL中的最后一个用户详细信息。
您需要做的是获取集合中的所有用户详细信息,然后将DDL数据绑定到该集合上。
你需要做这样的事情:
[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>
答案 1 :(得分:0)
HY
如果你想使用stringcollection只使用一个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);
}
我定义了一个绕过你的datacontext的类
class UserDetails
{
public string UserType;
public string Email;
public string FirstName;
public string UserId;
}
答案 2 :(得分:0)
谢谢你回复我.. 我做了另一种方式,当我从查询中获取用户详细信息时它在我的问题下面的Dropdownlist上覆盖,所以我只是添加了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();
}
}