隐藏嵌套网格视图中的按钮

时间:2016-08-13 06:18:42

标签: c# asp.net gridview

当我将网格导出到Excel文件时,如何隐藏btnadddetail

以下代码无效

导出代码

protected void btnexcel_Click(object sender, EventArgs e)
{
    gridrequisitions.DataSource = r.GetRequisition();
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
        "attachment;filename=RequisitionsReport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gridrequisitions.GridLines = GridLines.Both;
    gridrequisitions.DataBind();

    foreach (GridViewRow row in gridrequisitions.Rows)
    {
        GridView gvItems = row.Cells[0].FindControl("gvItems") as GridView;
        gvItems.Columns[0].Visible = false;
        gvItems.Columns[1].Visible = false;
        gvItems.Columns[2].Visible = false;
    }

    gridrequisitions.Caption = "Requisitions Report";

    foreach (GridViewRow row in gridrequisitions.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            for (int i = cell.Controls.Count - 1; i >= 0; i--)
            {
                if (cell.Controls[i] is Image)
                {
                    Image img = cell.Controls[i] as Image;


                    if (img.ImageUrl.Contains("plus.png") || img.ImageUrl.Contains("minus.png"))
                    {
                        cell.Controls.RemoveAt(i);
                    }
                }
            }
        }
    }

    gridrequisitions.Columns[1].Visible = false;
    gridrequisitions.Columns[2].Visible = false;
    Button btnadddetail = (Button)sender;
    btnadddetail.Visible = false;
    this.ClearControls(gridrequisitions);
    gridrequisitions.RenderControl(hw);
    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

网格视图

<asp:GridView ID="gridrequisitions" OnRowDataBound="gridrequisitions_RowDataBound" OnRowCommand="gridrequisitions_RowCommand" DataKeyNames="RequisitionID" runat="server" CssClass="table table-bordered text-nowrap" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Preview">
            <ItemTemplate>
                <asp:Image ID="imgPlus" runat="server" AlternateText="" ImageUrl="img/plus.png" Style="cursor: pointer" />
                <asp:Panel ID="pnlproducts" runat="server" Style="display: none">
                    <asp:GridView ID="gvItems" CssClass="table table-bordered" OnRowCommand="gvItems_RowCommand" OnRowDataBound="gvItems_RowDataBound" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="ItemID" HeaderText="ItemID" SortExpression="ItemID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:BoundField DataField="ItemName" HeaderText="Item Name" SortExpression="ItemName" />
                        </Columns>
                    </asp:GridView>
                    <div class="row" runat="server" id="hidebtn">
                        <br />
                        <div class="col-md-4">
                            <asp:Button ID="btnadddetail" OnClick="btnadddetail_Click" runat="server" Text="Add New Item" CssClass="btn btn-default" Width="200px" ForeColor="Black" />
                        </div>
                    </div>
                </asp:Panel>
            </ItemTemplate>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

使用控件的.visible属性您可以隐藏该特定控件。

  

btnadddetail.visible = false;

此代码可用于隐藏btnadddetail