简体   繁体   English

将列表添加到嵌套转发器控件

[英]Add List to Nested Repeater Control

I have a repeater control nested in a GridView. 我有一个嵌套在GridView中的中继器控件。 On GridView Updated I'm trying to DataBind the Repeater. 在更新的GridView上,我尝试对中继器进行数据绑定。 I'm not getting any errors but the databind is not working. 我没有收到任何错误,但是databind无法正常工作。

My setup is 2 tables with a many to many relationship. 我的设置是2个具有多对多关系的表。 Employee and PrincipleStaff. 员工和负责人 I am using the navigation property of PrincpleStaffs (hidden join table between Principle Staff and Employee). 我正在使用PrincpleStaffs的导航属性(Principle Staff和Employee之间的隐藏联接表)。 I am able to update the database through edit but cannot see the updates after update. 我可以通过编辑来更新数据库,但是更新后看不到更新。

Here is my code. 这是我的代码。 GridView Update is working in the database but GridView updated is not filling the repeater control. GridView更新正在数据库中工作,但GridView更新未填充转发器控件。

aspx: 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 file .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();    
                        }

                    }

                }
        }


    }
}

I was trying to do the wrong thing here! 我在这里尝试做错事! I needed to have a RowDataBound command. 我需要一个RowDataBound命令。 This updates the nested gridview in the parent gridview. 这将更新父gridview中的嵌套gridview。

    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();
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM