2013-07-20 24 views
1

我有一個嵌套在GridView中的中繼器控件。在GridView更新我試圖DataBind中繼器。我沒有得到任何錯誤,但數據綁定不起作用。添加列表到嵌套中繼器控件

我的設置是2個表,有多對多的關係。員工和PrincipleStaff。我正在使用PrincpleStaffs的導航屬性(主要員工和員工之間的隱藏連接表)。我能夠通過編輯更新數據庫,但在更新後無法看到更新。

這是我的代碼。 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();  
         } 

        } 

       } 
     } 


    } 
} 
+0

任何人有任何建議嗎?我應該在這裏採取不同的方法嗎?感謝您提供任何人可以提供的信息。 – chuckles

回答

0

,我想在這裏做了錯誤的事情!我需要一個RowDataBound命令。這將更新父級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(); 
       } 
      } 
     }