2012-01-11 40 views
0

我有一個2 Gridvies頁面的第一個加載數據,並在編輯按鈕下面出現一個新添加的行。然後它在該行創建一個網格,問題是當有回發時,我需要重新創建該行。在我的代碼中,新添加的GridView編輯事件處理程序不會觸發我需要能夠在回發中重新創建該行,並讓GridView將該文章發回到編輯模式。我不知道現在有什麼可以幫助我的人,這裏是我的代碼。順便說PageSettings是GridView和WebservicesGrid是嵌套一個嵌套的GridView和新創建的行在回發時消失

protected void PageSettings_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    ((GridView)sender).EditIndex = e.NewEditIndex; 
    EditIndex = e.NewEditIndex; 
    lblStatus.Text = string.Format("Editing Row: {0}", (e.NewEditIndex + 1).ToString()); 
} 

public int EditIndex = -1; 
protected void PageSettings_DataBound 
    (object sender, GridViewRowEventArgs e) 
{ 

    if ((e.Row.RowState & DataControlRowState.Edit) > 0 && 
     ((GridView)sender).EditIndex == EditIndex 
     || 
     Request["__EVENTTARGET"] == "ctl00$MainContent$PageSettings$ct103$WebServicesGridView") 
    {    
     GridViewRow row = new GridViewRow(e.Row.RowIndex+ 2, -1, DataControlRowType.DataRow, DataControlRowState.Normal); 
     row.CssClass = "gridview-alternating-row"; 
     //row.Cells.AddRange(CreateCells()); 

     TableCell cell; 
     cell = new TableCell(); 
     cell.ColumnSpan = 2; 
     row.Cells.Add(cell); 
     cell = new TableCell(); 
     cell.ColumnSpan = 4; 

     SystemPage SysPage = e.Row.DataItem as SystemPage; 
     GridView gv = CreateGridView("WebServices", SysPage); 
     cell.Controls.Add(gv); 
     row.Cells.Add(cell); 
     // Row Edting Event not firing either 
     gv.RowEditing += new GridViewEditEventHandler(services_RowEditing); 

     Table table = e.Row.Parent as Table; 
     table.Rows.AddAt(e.Row.RowIndex + 2, row); 
    } 
} 

private GridView CreateGridView(string type, SystemPage page) 
{ 
    GridView services = new GridView(); 
    switch (type) 
    { 
     case "WebServices" : 

      CommandField CommandEdit = new CommandField(); 
      CommandEdit.ButtonType = ButtonType.Link; 
      CommandEdit.ShowEditButton = true; 
      CommandEdit.UpdateText = "Update"; 
      CommandEdit.EditText = "Edit";     
      services.Columns.Add(CommandEdit); 

      BoundField WebServiceName = new BoundField(); 
      WebServiceName.DataField = "name"; 
      WebServiceName.HeaderText = "WebService"; 
      services.Columns.Add(WebServiceName); 

      BoundField WebServiceUrl = new BoundField(); 
      WebServiceUrl.DataField = "url"; 
      WebServiceUrl.HeaderText = "Url"; 
      services.Columns.Add(WebServiceUrl); 

      List<WebService> list = new List<WebService>(); 
      list.Add(new WebService { Name = "Test", Url = "Test" }); 

      services.ID = "WebServicesGridView"; 
      services.AutoGenerateColumns = false; 

      services.DataSource = list; 
      services.DataBind(); 

      break; 
    } 

    return services; 

} 

protected void services_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    ((GridView)sender).EditIndex = e.NewEditIndex; 
    lblSubGridStatus.Text = e.NewEditIndex.ToString(); 

    PageSettings.DataBind(); 
} 
+0

您**真**需要重新考慮您的方法。由於所有已經遇到的問題,以編程方式創建'GridViews'或其他控件是一個非常糟糕的主意。你可以在標記中定義第二個'GridView',並且根據行狀態顯示/隱藏它嗎?我認爲這將是一個好的開始。 – jwiscarson 2012-01-11 05:00:25

回答

0
  1. 嘗試增加您的嵌套網格視圖RowCreated事件。
  2. 首先將編輯事件處理程序綁定到網格,然後將網格添加到網頁中。即
  3. 綁定事件處理程序並將網格添加到單元格後,嘗試綁定嵌套網格。

編輯:

好,#1,你所使用的數據項,將只在數據綁定事件提供可能很難爲你。對於#2和#3,

private void CreateGridView(string type, SystemPage page, Cell parent) 
{ 
    GridView services = new GridView(); 
    ... 
    services.RowEditing += new GridViewEditEventHandler(services_RowEditing); 
    parent.Add(services); 
    services.DataSource = list; 
    services.DataBind(); 
} 

protected void PageSettings_DataBound(object sender, GridViewRowEventArgs e) 
{ 
    .... 

    SystemPage SysPage = e.Row.DataItem as SystemPage; 
    // add the row first to the page 
    row.Cells.Add(cell); 
    Table table = e.Row.Parent as Table; 
    table.Rows.AddAt(e.Row.RowIndex + 2, row); 
    CreateGridView("WebServices", sysPage, cell); 
} 
+0

我給它一個鏡頭 – ONYX 2012-01-11 03:59:20

+0

我在我的表格對象上得到一個錯誤,它說對象引用沒有設置爲對象的一個​​實例。你能編輯我的代碼嗎?告訴我你的意思。我發現它並沒有得到結果 – ONYX 2012-01-11 04:07:16

+0

其必須是Table table = e.row.Parent作爲Table not setting table = null – ONYX 2012-01-11 04:09:28