zoukankan      html  css  js  c++  java
  • 在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 事件中要先创建

            }

        }

  • 相关阅读:
    【引用】Android.mk简介
    android02android的四大组件
    rpm 安装指令全
    android04activity的布局管理器
    代码积累1统计图
    清除防火墙所有配置规则
    代码积累2tab页面滑动效果
    RHEL5 配置YUM源 安装RZSZ
    系统安全漏洞扫描软件
    liunx下防火墙的配置
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/1981174.html
Copyright © 2011-2022 走看看