使用c#为列表视图设置数据绑定
本文关键字:设置 数据绑定 视图 列表 使用 | 更新日期: 2023-09-27 18:03:18
我对使用实体框架和Linq相当陌生。我在一个webform中有一个ListView,当从页面顶部的下拉列表中选择一个"区域"时,我想填充它。数据将基于下拉菜单中选择的内容。我使用EF填充下拉菜单的选项,我的挑战是让页面在页面返回时填充来自selectedValue的数据。我得到的点页面张贴回来,它返回"没有数据被返回",我的文本在EmptyDataTemplate。在某个地方,它没有检索我的数据,所以我希望有人能告诉我我做错了什么。
<section class="featured" style="text-align:center; background-color: #fff; padding:15px">
<asp:Label ID="lblChooseArea" runat="server" Text="Choose an area: "></asp:Label>
<asp:DropDownList ID="AreaDropDown"
ItemType="Level1One.Classes.Area" AutoPostBack="true"
runat="server" DataTextField="AreaName"
SelectMethod="GetAreas" >
</asp:DropDownList>
<br /><hr /><br />
<ul>
<asp:ListView ID="ticketList" runat="server"
GroupItemCount="3"
ItemType="Level1One.Classes.Ticket" >
<EmptyDataTemplate>
<table id="Table1" runat="server" >
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td id="Td1" runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr ID="itemPlaceholderContainer" runat="server">
<td ID="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td id="Td2" runat="server">
<table>
<tr>
<td> </td>
<td>
<a href="ProductDetails.aspx?ticketID=<%#:Item.TicketID%>">
<span class="ProductName">
<%#:Item.TicketSubArea%>
</span>
</a>
<br />
<span class="ProductPrice">
<b>Description: </b><%#:Item.Description%>
</span>
<br />
</td>
</tr>
</table>
</td>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td3" runat="server">
<table ID="groupPlaceholderContainer" runat="server">
<tr ID="groupPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server"><td id="Td4" runat="server"></td></tr>
</table>
</LayoutTemplate>
</asp:ListView>
</ul>
</section>
下面是后面的代码:
public partial class TicketList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string dllArea = AreaDropDown.SelectedValue;
int dllAreaNum;
int.TryParse(dllArea, out dllAreaNum);
using (TicketContext adrop = new TicketContext())
{
var result = (from a in adrop.Tickets
where (a.AreaID == dllAreaNum)
select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();
ticketList.DataSource = result;
ticketList.DataBind();
}
}
}
public IQueryable<Area> GetAreas()
{
var db = new Level1One.Classes.TicketContext();
IQueryable<Area> query = db.Areas;
return query;
}
}
My classes and Context:
using System.Data.Entity;
namespace Level1One.Classes
{
public class TicketContext : DbContext
{
public TicketContext() : base("Level1One")
{
}
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Area> Areas { get; set; }
}
}
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Level1One.Classes
{
public class Ticket
{
[ScaffoldColumn(false)]
public int TicketID { get; set; }
[Required, StringLength(100), Display(Name = "Area")]
public string TicketArea { get; set; }
[Required, StringLength(100), Display(Name = "SubArea")]
public string TicketSubArea { get; set; }
[Required, StringLength(10000), Display(Name = "Description"), DataType(DataType.MultilineText)]
public string Description { get; set; }
public int? AreaID { get; set; }
public virtual Area Area { get; set; }
}
}
添加DataValueField = AreaID
到您的区域下拉列表
用
代替 if (IsPostBack)
{
string dllArea = AreaDropDown.SelectedValue;
int dllAreaNum;
int.TryParse(dllArea, out dllAreaNum);
using (TicketContext adrop = new TicketContext())
{
var result = (from a in adrop.Tickets
where (a.AreaID == dllAreaNum)
select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();
ticketList.DataSource = result;
ticketList.DataBind();
}
:
if (IsPostBack)
{
string dllArea = AreaDropDown.SelectedValue;
int dllAreaNum;
int.TryParse(dllArea, out dllAreaNum);
IQueryable<Ticket> result = getTicketbyArea(dllAreaNum)
ticketList.DataSource = result.ToList();
ticketList.DataBind();
}
}
删除:
using (TicketContext adrop = new TicketContext())
{
var result = (from a in adrop.Tickets
where (a.AreaID == dllAreaNum)
select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();
ticketList.DataSource = result;
ticketList.DataBind();
}
将此方法添加到您将在页面加载时调用的代码中。
public IQueryable<Ticket> getTicketbyArea(int dllAreaNum)
{
var _db = new TicketContext();
var query = _db.Tickets
.Where(x => x.AreaID== dllAreaNum).Select(x =>x)
return query;
}