代码之家  ›  专栏  ›  技术社区  ›  user366312

ASP.NET-GridView动态页脚行创建问题

  •  0
  • user366312  · 技术社区  · 15 年前

    我可以在网格视图中动态创建边界字段和页脚行,如下所示:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    CreateGridView();
                }
            }
    
            private void CreateGridView()
            {
                GridView1.Columns.Clear();
    
                DataTable dataTable = Book.GetBooksDataSet().Tables[0];
    
                CommandField cf = new CommandField();
                cf.ShowEditButton = true;
    
                GridView1.Columns.Add(cf);
    
                int colCount = 1;
                foreach (DataColumn c in dataTable.Columns)
                {
                    BoundField boundField = new BoundField();
    
                    boundField.DataField = c.ColumnName;
                    boundField.HeaderText = c.ColumnName;
                    //boundField.FooterText = "---";
    
                    if (colCount == 3 || colCount == 5)
                    {
                        boundField.ReadOnly = true;
                    }
    
                    GridView1.Columns.Add(boundField);
                    colCount++;
                }
    
                GridView1.ShowFooter = true;
    
                GridView1.DataSource = dataTable;
                GridView1.DataBind();
    
                GridViewRow footerRow = GridView1.FooterRow;
                Button b = new Button();
                b.Text = "Add New";
                int i = 0;
                footerRow.Cells[i].Controls.Add(b);
                foreach (DataColumn c in dataTable.Columns)
                {
                    ++i;
                    TextBox tb = new TextBox();
                    footerRow.Cells[i].Controls.Add(tb);
                }
            }
    ....................................
    ....................................
    ....................................
    }
    

    但问题是,当我单击“添加新”按钮时,它会立即消失。而且,我也无法向它添加任何事件处理程序。或者像这样拦截它的行动:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                int index = Convert.ToInt32(e.CommandArgument);
    
                if (e.CommandName == "Edit")
                {
                    GridView1.EditIndex = index;
    
                    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
    
                    //We can get cell data like this
                    string id = selectedRow.Cells[1].Text;
                    string isbn = selectedRow.Cells[2].Text;
    
                    //This is necessary to GridView to be showed up.
                    CreateGridView();
                }
                else if (e.CommandName == "Update")
                {
                    LinkButton updateButton = (LinkButton)e.CommandSource;
    
                    DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
    
                    GridViewRow gvr = (GridViewRow)dcfc.Parent;
    
                    //The update...................
                    //Update grid-data to database
                    UpdateDataInTheDatabase(gvr.Cells[1].Controls);                
    
                    //Grid goes back to normal
                    GridView1.EditIndex = -1;
    
                    //This is necessary to GridView to be showed up.
                    CreateGridView();
                }
            }
    

    还有一件事,我看到了一些建议处理网格视图的解决方案 rowBound 事件。但我需要从内部去做 Page_load 事件处理程序,或, GridView1_RowCommand 事件处理程序。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Jamie Ide    15 年前

    动态创建的控件 re-created on every postback . “添加新的”按钮会导致回发,因此动态创建的页脚将消失。是否需要动态创建此网格?从您发布的代码来看,您可以在标记中执行此操作。如果没有,则必须在每次回发时重新创建动态控件。

    编辑添加: 我玩过一点,下面的工作是网格不会消失,事件也会处理,但实际上它什么都不做。希望这有帮助。

    Markup:

        <p><asp:Literal ID="Literal1" runat="server" /></p>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            OnRowCommand="GridView1_RowCommand" 
            OnRowEditing="GridView1_RowEditing"/>
    

    代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridView();
    }
    
    private DataTable GetBooksDataTable()
    {
        var dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Title", typeof(string));
        dt.Columns.Add("Author", typeof(string));
    
        for (int index = 0; index < 10; index++)
        {
            dt.Rows.Add(index, "Title" + index, "Author" + index);
        }
        return dt;
    }
    
    private void BindGridView()
    {
        var dt = GetBooksDataTable();
    
        GridView1.Columns.Clear();
        GridView1.ShowFooter = true;
    
        var cf = new CommandField();
        cf.HeaderText = "Action";
        cf.ShowEditButton = true;
        GridView1.Columns.Add(cf);
    
        for (int index = 0; index < dt.Columns.Count; index++)
        {
            var boundField = new BoundField();
            boundField.DataField = dt.Columns[index].ColumnName;
            boundField.HeaderText = dt.Columns[index].ColumnName;
            GridView1.Columns.Add(boundField);
        }
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
        var footer = GridView1.FooterRow;
        var b = new LinkButton();
        b.Text = "Add New";
        b.CommandName = "Add New";
        footer.Cells[0].Controls.Add(b);
        for (int index = 1; index < dt.Columns.Count + 1; index++)
        {
            var tb = new TextBox();
            footer.Cells[index].Controls.Add(tb);
        }
    
    }
    
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Literal1.Text = e.CommandName;
    }
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
    }
    
        2
  •  1
  •   Jan Jongboom    15 年前

    将代码从 Page_Load Page_Init . 添加的内容 页负荷 仅在回发的一个生命周期内持续。

    然后您就可以添加事件处理程序、拦截事件等。

    推荐文章