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