我有一个嵌套在 GridView 中的中继器控件。在更新的 GridView 上,我正在尝试对中继器进行数据绑定。我没有收到任何错误,但数据绑定不起作用。
我的设置是 2 个具有多对多关系的表。员工和原则员工。我正在使用 PrincpleStaffs 的导航属性(Principal Staff 和 Employee 之间的隐藏连接表)。我可以通过编辑更新数据库,但更新后看不到更新。
这是我的代码。GridView 更新在数据库中工作,但 GridView 更新没有填充转发器控件。
aspx:
<asp:GridView ID="AddPrincipleStaff" runat="server" AutoGenerateColumns="False" DataKeyNames="PrincipleStaffID" DataSourceID="PrincipleStaffEmployees" OnRowUpdating="AddPrincipleStaffGridView_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="" SortExpression="PrincipleStaffID">
<EditItemTemplate>
<asp:Label ID="PrincipleStaffIDEditTemplate" runat="server" Text='<%# Eval("PrincipleStaffID") %>' style="display: none;"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="PrinicpleStaffID" runat="server" Text='<%# Bind("PrincipleStaffID") %>' style="display: none;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PrincipleStaffTitle" SortExpression="PrincipleStaffTitle">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeName">
<EditItemTemplate>
<asp:CheckBoxList ID="EmployeesCheckBoxes" runat="server" DataSourceID="EmployeesDataSource" DataTextField="empEmployeeName" DataValueField="empEmployeeID">
</asp:CheckBoxList>
</EditItemTemplate>
<ItemTemplate>
<asp:Repeater runat="server" ID="EmployeeList"></asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ModelFirst;
namespace FactoryWebsite
{
public partial class AddPrincipalStaffEmployees : System.Web.UI.Page
{
FactoryTheaterModelFirstContainer db = new FactoryTheaterModelFirstContainer();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ProductionID"] != null)
{
string stringSession = Session["ProductionID"].ToString();
int intProductionID = Int32.Parse(stringSession);
var production = from p in db.Productions
where p.proProductionID == intProductionID
select p.proProductionTitle;
ProductionTitle.Text = production.FirstOrDefault();
}
else
Response.Redirect("/AddProduction.aspx");
}
protected void AddPrincipleStaffGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
foreach (GridViewRow row in AddPrincipleStaff.Rows)
{
CheckBoxList cbl = (CheckBoxList)row.FindControl("EmployeesCheckBoxes");
if (cbl != null)
{
foreach (ListItem item in cbl.Items)
{
if (item.Selected)
{
Label PrincipleID = AddPrincipleStaff.Rows[e.RowIndex].FindControl("PrinicpleStaffIDEditTemplate") as Label;
int PrincipleStaffID = 0;
PrincipleStaffID = Int32.Parse(PrincipleID.Text);
var ID = item.Value;
int EmployeeID = Int32.Parse(ID);
using (var context = new FactoryTheaterModelFirstContainer())
{
PrincipleStaff principlestaff = context.PrincipleStaffs.Single(s => s.PrincipleStaffID == PrincipleStaffID);
Employee employeeid = context.Employees.Single(s => s.empEmployeeID == EmployeeID);
principlestaff.Employees.Add(employeeid);
context.SaveChanges();
}
}
}
}
}
}
protected void AddPrincipleStaff_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
foreach (GridViewRow row in AddPrincipleStaff.Rows)
{
Label psid = (Label)FindControl("PrinicpleStaffID");
Repeater employees = (Repeater)AddPrincipleStaff.FindControl("EmployeeList") as Repeater;
if (psid != null)
{
int intpsid = 0;
intpsid = Int32.Parse(psid.Text);
var context = new FactoryTheaterModelFirstContainer();
{
var query = context.PrincipleStaffs.Where(c => c.PrincipleStaffID == intpsid)
.SelectMany(c => c.Employees)
.Select(a => a.empEmployeeName).ToList();
employees.DataSource = query;
employees.DataBind();
}
}
}
}
}
}