我有一个嵌套在GridView中的转发器控件。在GridView更新我正在尝试DataBind中继器。我没有收到任何错误,但数据绑定不起作用。
我的设置是2个表,有很多关系。员工和PrincipleStaff。我正在使用PrincpleStaffs的导航属性(Principle Staff和Employee之间的隐藏连接表)。我可以通过编辑更新数据库,但在更新后无法看到更新。
这是我的代码。 GridView Update正在数据库中工作,但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();
}
}
}
}
}
}
答案 0 :(得分:0)
我试图在这里做错事!我需要一个RowDataBound命令。这将更新父网格视图中的嵌套网格视图。
protected void AddPrincipleStaff_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)// Bind nested grid view with parent grid view
{
var psid = DataBinder.Eval(e.Row.DataItem, "PrincipleStaffID");
int intpsid = 0;
intpsid = Int32.Parse(psid.ToString());
using (var context = new FactoryTheaterModelFirstContainer())
{
var query = (from c in context.PrincipleStaffs
from p in c.Employees
where c.PrincipleStaffID == intpsid
select new
{
Name = p.empEmployeeName
}).ToList();
if (query.Count > 0)
{
GridView childgrd = (GridView)e.Row.FindControl("ListEmployees"); // find nested grid view from parent grid veiw
childgrd.DataSource = query;
childgrd.DataBind();
}
}
}