悟生慧

 

在GridView 中点击某一个按钮在此按钮的下行动态添加一行,再次点击第二次添加的行隐藏

//页面内容

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowcommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="名称">
                    <ItemTemplate>
                        <asp:Label ID="lblText" runat="server" Text='<%# Eval("V_Text") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="键值">
                    <ItemTemplate>
                        <asp:Label ID="lblCode" runat="server" Text='<%# Eval("V_Code") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lkBtn"  runat="server" CommandArgument='<%# Eval("V_Code") %>' CommandName="btnShow" >新增</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

//后置代码 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("btnShow"))
        {
            //创建一个GridView的一个分隔行(根据DataControlRowType来设置)
            // GridViewRow rowSeparator = new GridViewRow(0, 0, DataControlRowType.Separator, DataControlRowState.Normal);
            //或实现一个数据行
            GridViewRow rowSeparator = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            //设置行的底色
            rowSeparator.BackColor = System.Drawing.Color.White;
            // 获取到行索引 RowIndex
            GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int index = gvrow.RowIndex;
            //获得当前的GridView
            GridView gv = (GridView)sender;
            //设置单元格集
            TableCellCollection cells = gv.Rows[index].Cells;
            //设置单元格,根据实际情况增加,我这儿是增加一个跨所有列的行
            TableCell separatorCell = new TableCell();
            //根据GridView的第一列的显示情况设置单元格和跨列数
            if (GridView1.Columns[0].Visible == true)
            {
                //当前增加的行所跨的列是gv 的所有列
                separatorCell.ColumnSpan = cells.Count-1;
            }
            else
            {
                separatorCell.ColumnSpan = cells.Count - 1;
            }
                //}
                //单元格的对齐
                separatorCell.HorizontalAlign = HorizontalAlign.Center;
                //单元格的背景色
                separatorCell.BackColor = System.Drawing.Color.FromArgb(226, 226, 226);
                //单元格的高度
                separatorCell.ControlStyle.Height = 10;

                //在列中添加一个控件
                TextBox tb = new TextBox();
                //l.Text = "ddddd";
                Control c = (Control)tb;

                TextBox tbi = new TextBox();
                Control ci = (Control)tbi;

                separatorCell.Controls.Add(c);
                separatorCell.Controls.Add(ci);
                //在单元格集中增加单元格控件
                rowSeparator.Cells.Add(separatorCell);
                //设置GridView行的可见性
                rowSeparator.Visible = true;
                //在GridView中的相应行插入行
                GridView1.Controls[0].Controls.AddAt(index + 2, rowSeparator);
                //判断当前按钮点了几次了 同一个按钮点击第一次是添加下面的一行,点击第二次死隐藏当前添加的行
                //ViewState["flat"] 用于标记当前点了几行
                if (ViewState["flat"] != null)
                {
                    if (ViewState["flat"].ToString().Trim().Equals(e.CommandArgument.ToString()))
                    {
                        rowSeparator.Visible = false;
                        ViewState["flat"] = null;
                    }
                    else ViewState["flat"] = e.CommandArgument.ToString();
                }
                else
                    ViewState["flat"] = e.CommandArgument.ToString();


                // 注意: ViewState["flat}=null; 在pageLoad 事件中要先创建

        }

    }

posted on 2011-03-11 12:21  悟生慧  阅读(3565)  评论(2编辑  收藏  举报

导航