获取当前的GridView

时间:2014-11-14 16:32:21

标签: asp.net gridview

我有一个包含3个GridView的页面。

<div id="Export" runat="server" class="headeropt noprint">

            <asp:LinkButton runat="server" ID="buttonexport" OnClick="buttonexport_Click" OnClientClick="Javascript:Noshow();" CssClass="btn btn-default">
                <asp:Image runat="server" ImageUrl="/images/Excel-16.gif" />
                <asp:Label runat="server" Text="Export" />
            </asp:LinkButton>
        </div>
<div class="col-md-12">
        <asp:GridView ID="grid1" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid1_RowEditing" datakeynames="grid1">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkname" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("name") %>' Text='<%# Eval("name") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Company">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("company") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="alinha-direita" />
                    <FooterStyle CssClass="alinha-direita" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

        <asp:GridView ID="grid2" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid2_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid2_RowEditing" datakeynames="grid2">
            <Columns>
                <asp:TemplateField HeaderText="Product Family">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkprdfamily" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("prdfamily") %>' Text='<%# Eval("prdfamily") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="alinha-direita" />
                    <FooterStyle CssClass="alinha-direita" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:GridView ID="grid3" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid3_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" datakeynames="grid3">
            <Columns >
                <asp:TemplateField HeaderText="Product">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("product") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("price") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

如您所见,第一个网格链接到第二个网格,第二个网格链接到第二个网格。 我想知道的是,在任何时候我都能看到我正在观看的网格。

刚刚编辑过添加将网格导出为ex​​cel的linkBut​​ton。这就是为什么我需要我当前使用的gridview,以便导出命令随时可用。

这是导出代码:

protected void buttonexport_Click(object sender, EventArgs c)
{
    Master.Page.Form.Attributes.Remove("onsubmit");
    CB.ChangeControlsToValue(grid1);
    string attachment = "attachment; filename=File.xls";
    //Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}

感谢。

1 个答案:

答案 0 :(得分:0)

Nevemind,得到了解决方案。

我刚检查了当前可见的那个并使用它。

bool list = grid1.Visible;
    if (list)
    { CB.ChangeControlsToValue(grid1); }

依旧......