嵌套网格视图和新创建的行在回发时消失

时间:2012-01-11 03:39:40

标签: c# asp.net

我有2个Gridvies页面的第一个加载数据,在编辑按钮下面会出现一个新添加的行。然后它在该行中创建一个网格,问题是当有回发帖时我需要重新创建该行。在我的代码中,新添加的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 个答案:

答案 0 :(得分:0)

  1. 尝试在RowCreated事件中添加嵌套网格视图。
  2. 首先将编辑事件处理程序绑定到网格,然后在页面中添加网格。即。
  3. 绑定事件处理程序并将网格添加到单元格后,尝试绑定嵌套网格。
  4. 编辑:

    嗯,#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);
    }