“嵌套网格视图”和“新建行”在回发时显示

本文关键字:显示 新建行 新建 嵌套 网格 视图 | 更新日期: 2023-09-27 18:20:08

我有一个2网格,它是页面的第一个加载数据,在编辑按钮上,下面会出现一个新添加的行。然后它在那一行中创建一个网格,问题是当有帖子回复时,我需要重新创建那一行。在我的代码中,新添加的GridView编辑事件处理程序不会触发。我需要能够在回发时重新创建该行,并使触发回发的GridView进入编辑模式。我不知道现在该怎么办,有人能帮我吗?这是我的代码。顺便说一句,PageSettings是GridView,WebservicesGrid是嵌套的

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

“嵌套网格视图”和“新建行”在回发时显示

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