使用LINQ用父子关系填充dropdownlist [英] populating dropdownlist with parent child relation using LINQ
本文介绍了使用LINQ用父子关系填充dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绑定两个dropdownlist和2nd dropdownlist数据必须基于作为父子关系的第一个dropdownlist选择,我想使用LINQ查询做到这一点
请提供执行此过程的代码或示例
感谢inadvance
i want to bind the two dropdownlist and 2nd dropdownlist data must be based on the first dropdownlist selection that is parent-child relation i want do this using LINQ query
plz provide me code or example to do this process
Thanks inadvance
推荐答案
我为球员和球队准备了一个方案.我在下面写过课程
I prepared a scenario for players and teams. I have written classes below
[Serializable]
public class Team
{
public string Name { get; set; }
}
[Serializable]
public class Player
{
public Team Team { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
我的WebForm代码如下.
My WebForm code is below.
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
Teams</td>
<td>
<asp:DropDownList ID="ddlTeam" runat="server"
onselectedindexchanged="ddlTeam_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Players</td>
<td>
<asp:DropDownList ID="ddlPlayer" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Literal ID="ltResult" runat="server"></asp:Literal>
</td>
</tr>
</table>
</div>
</form>
</body>
而后面的代码在下面.这是我将数据存储在ViewState上的示例,因此我将类标记为Serializable.
and the codebehind is below. This is the example I stored my data on ViewState so I marked my classes as Serializable.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
FillTeam();
}
private void FillTeam()
{
Team t1 = new Team { Name = "Barcelona" };
Team t2 = new Team { Name = "Milan" };
Team t3 = new Team { Name = "Bayern Munchen" };
List<Player> players = new List<Player>();
players.Add(new Player { Team = t1, Name = "Xavi" });
players.Add(new Player { Team = t1, Name = "Iniesta" });
players.Add(new Player { Team = t1, Name = " Busquets" });
players.Add(new Player { Team = t2, Name = "Ibrahimovic" });
players.Add(new Player { Team = t2, Name = "Gattuso" });
players.Add(new Player { Team = t2, Name = "Robinho" });
players.Add(new Player { Team = t2, Name = "Pato" });
players.Add(new Player { Team = t3, Name = "Ribery" });
players.Add(new Player { Team = t1, Name = "Robben" });
ViewState["players"] = players;
ddlTeam.Items.Add("Choose...");
ddlTeam.Items.Add(new ListItem { Text = t1.Name });
ddlTeam.Items.Add(new ListItem { Text = t2.Name });
ddlTeam.Items.Add(new ListItem { Text = t3.Name });
ddlTeam.SelectedIndex = 0;
}
protected void ddlTeam_SelectedIndexChanged(object sender, EventArgs e)
{
List<Player> players = (List<Player>)ViewState["players"];
if (null != players)
{
//var playerinTeams = players.Where(p => p.Team.Name == ddlTeam.SelectedItem.Text);
var playerinTeams = from player in players
where player.Team.Name == ddlTeam.SelectedItem.Text
select player;
ddlPlayer.DataSource = playerinTeams;
ddlPlayer.DataBind();
}
}
}
}
跟随下面的链接
http://elidotnet.blogspot.in/2010/10/cascading- drop-down-list-using-aspnet.html [ ^ ]
hi ,
follow below link
http://elidotnet.blogspot.in/2010/10/cascading-drop-down-list-using-aspnet.html[^]
这篇关于使用LINQ用父子关系填充dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文