Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套的Gridview和新创建的行显示在回发上_C#_Asp.net - Fatal编程技术网

C# 嵌套的Gridview和新创建的行显示在回发上

C# 嵌套的Gridview和新创建的行显示在回发上,c#,asp.net,C#,Asp.net,我有两个网格,第一次加载页面的数据,在编辑按钮上,一个新添加的行出现在下面。然后在该行中创建一个网格问题是,当有回帖时,我需要重新创建该行。在我的代码中,新添加的GridView编辑事件处理程序不会触发,我需要能够在回发时重新创建该行,并使触发回发的GridView进入编辑模式。我不知道现在该做什么,有人能帮我吗,这是我的代码。顺便说一下,PageSettings是GridView,WebservicesGrid是嵌套的 protected void PageSettings_RowEditin

我有两个网格,第一次加载页面的数据,在编辑按钮上,一个新添加的行出现在下面。然后在该行中创建一个网格问题是,当有回帖时,我需要重新创建该行。在我的代码中,新添加的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();
}
受保护的无效页面设置\u行编辑(对象发送方,GridViewEditEventArgs e)
{
((GridView)sender).EditIndex=e.NewEditIndex;
EditIndex=e.NewEditIndex;
lblStatus.Text=string.Format(“编辑行:{0}”,(e.NewEditIndex+1.ToString());
}
公共int EditIndex=-1;
受保护的无效页面设置\u数据绑定
(对象发送方,GridViewRowEventArgs e)
{
如果((e.Row.RowState和DataControlRowState.Edit)>0&&
((GridView)发件人).EditIndex==EditIndex
|| 
请求[“\uuu EVENTTARGET”]=“ctl00$MainContent$PageSettings$ct103$WebServicesGridView”)
{            
GridViewRow row=新的GridViewRow(e.row.RowIndex+2,-1,DataControlRowType.DataRow,DataControlRowState.Normal);
row.CssClass=“网格视图交替行”;
//row.Cells.AddRange(CreateCells());
表细胞;
单元格=新表单元格();
cell.ColumnSpan=2;
行.单元格.添加(单元格);
单元格=新表单元格();
cell.ColumnSpan=4;
SystemPage SysPage=e.Row.DataItem作为SystemPage;
GridView gv=CreateGridView(“WebServices”,SysPage);
细胞。对照。添加(gv);
行.单元格.添加(单元格);
//行Edting事件也未触发
gv.RowEditing+=新的GridViewEditEventHandler(服务\u RowEditing);
Table Table=e.Row.Parent作为表;
table.Rows.AddAt(e.Row.RowIndex+2,Row);
}
}
私有GridView CreateGridView(字符串类型,系统页面)
{
GridView服务=新建GridView();
开关(类型)
{
案例“Web服务”:
CommandField CommandEdit=新建CommandField();
CommandEdit.ButtonType=ButtonType.Link;
CommandEdit.ShowEditButton=true;
CommandEdit.UpdateText=“更新”;
CommandEdit.EditText=“编辑”;
services.Columns.Add(CommandEdit);
BoundField WebServiceName=新的BoundField();
WebServiceName.DataField=“name”;
WebServiceName.HeaderText=“WebService”;
services.Columns.Add(WebServiceName);
BoundField WebServiceUrl=新的BoundField();
WebServiceUrl.DataField=“url”;
WebServiceUrl.HeaderText=“Url”;
services.Columns.Add(WebServiceUrl);
列表=新列表();
添加(新的WebService{Name=“Test”,Url=“Test”});
services.ID=“WebServicesGridView”;
services.AutoGenerateColumns=false;
services.DataSource=list;
services.DataBind();
打破
}
返回服务;
}
受保护的无效服务\u行编辑(对象发送方,GridViewEditEventArgs e)
{
((GridView)sender).EditIndex=e.NewEditIndex;
lblSubGridStatus.Text=e.NewEditIndex.ToString();
PageSettings.DataBind();
}
  • 尝试在事件中添加嵌套的栅格视图
  • 首先将编辑事件处理程序绑定到网格,然后将网格添加到页面中。i、 e
  • 在绑定事件处理程序并将网格添加到单元格后,尝试绑定嵌套网格
  • 编辑:

    嗯,#1可能对您来说很困难,因为您使用的数据项仅在数据绑定事件中可用。对于#2和#3


    我的表对象上有一个错误,它表示对象引用未设置为对象的实例。你能编辑我的代码吗?告诉我你的意思。我试过了,我没有得到结果它必须是Table=e.row.Parent作为Table=not setting Table=nulls我会试试,但很可能行会消失什么then@KDM,您需要检查回发时是否触发了数据绑定事件。如果您依赖视图状态在post back中重新创建行,则可能不会触发该事件。在这种情况下,在post ack场景中重新绑定外部网格。我这样做了,接下来的行数据消失,事件不会发生。您真的需要在这里重新考虑您的方法。以编程方式创建
    gridview
    或其他控件是一个非常糟糕的主意,因为您已经遇到了所有问题。能否在标记中定义第二个
    GridView
    ,然后根据行状态显示/隐藏它?我认为这将是一个良好的开端。
    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);
    }