如何在GridView出现时显示此ImageButton?

时间:2012-08-14 09:25:47

标签: c# asp.net gridview imagebutton

我正在开发一个具有小型报告服务的简单系统。报告服务通常是DropDownList,并且基于DropDownList的值,将生成Gridview。在这个GridView下有一个ImageButton,用于将GridView打印为Word文档。 我现在的问题是:该按钮一直出现。它应该与GridView一起出现,因为它与它有关。 那怎么做?

ASP.NET代码:

<div>

        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
            DataSourceID="SqlDataSource2" DataTextField="Title" DataValueField="ID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PM_RegistrationSysDBConnectionString %>" 
            SelectCommand="SELECT [ID], [Title] FROM [Events]"></asp:SqlDataSource>

    </div>

    <br />

    <div>    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true">
            <Columns>
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

                <%--<asp:TemplateField>
                    <FooterTemplate>
                        <asp:ImageButton ID="wordBtn" CssClass="icon" runat="server" ImageUrl="images/word.png" 
                           ToolTip="Generate a Word Document" OnClick="creat_word_table"></asp:ImageButton>
                    </FooterTemplate>
                </asp:TemplateField>--%>
            </Columns>
        </asp:GridView>
        <br />
        <asp:ImageButton ID="wordBtn" CssClass="icon" runat="server" ImageUrl="images/word.png" 
                           ToolTip="Generate a Word Document" OnClick="creat_word_table"></asp:ImageButton>
    </div>

C#代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["PM_RegistrationSysDBConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string eventTitle = DropDownList1.SelectedItem.Text;
        string cmd = @"SELECT     dbo.Events.Title, dbo.Users.Name
                        FROM         dbo.BookingDetails INNER JOIN
                                              dbo.Events ON dbo.BookingDetails.EventID = dbo.Events.ID INNER JOIN
                                              dbo.Users ON dbo.BookingDetails.NetworkID = dbo.Users.NetworkID
                        WHERE     (dbo.Events.IsActive = 1) AND (dbo.Events.Title = '"+eventTitle+"')";
        SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    public void creat_word_table(object sender, EventArgs e)
    {
        Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";

        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        HtmlForm frm = new HtmlForm();
        GridView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(GridView1);
        frm.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());
        Response.End();
    }

1 个答案:

答案 0 :(得分:1)

试试这个

默认情况下将图片按钮设置为visible = false;

myImageButton.Visible = false;

然后在设置网格的数据源之前,检查是否有任何记录

类似于dt.Rows.Count

例如

if(dt.Rows.Count > 0)
   myImageButon.Visible = true;