locked
How to change Link button fore color using jquery while export data from gridview to Excel RRS feed

  • Question

  • User79369997 posted

    I have aspx application, I have used gridview inside dvData div. gridview header has link button. on exporting to excel, i need link button fore color has to change. anyone please help me.

    In Jquery function :

    function Search_ExportToExcel() {
    var data = $("#dvData").html();
    data = escape(data);
    $('body').prepend("<form method='post' action='exportPage.aspx' style='top:-3333333333px;' forecolor:'white' id='tempForm'><input type='hidden' name='data' value='" + data + "' ></form>");
    $('#tempForm').submit();
    $("tempForm").remove();
    return false;

    }

    In code behind : 

    protected void Page_Load(object sender, EventArgs e)
    {
    string data = Request.Form["data"];
    data = HttpUtility.UrlDecode(data);
    Response.Clear();
    string filename = "QAP_SearchReport_" + DateTime.Now.ToString() + ".xls";
    Response.AddHeader("content-disposition", "attachment;filename=" + filename + "");
    Response.Charset = "";
    Response.ContentType = "application/excel";
    HttpContext.Current.Response.Write(data);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
    }

    Please help me

    Thursday, February 16, 2017 10:04 AM

All replies

  • User-707554951 posted

    Hi shiva jav,

    Please try to change the forecolor and call your  Search_ExportToExcel() method in behind code.

    In aspx:

    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.1.1.js"></script>
        <script>
            function Search_ExportToExcel() {
                var data = $("#dvData").html();
                data = escape(data);
                $('body').prepend("<form method='post' action='exportPage.aspx' style='top:-3333333333px;' forecolor:'white' id='tempForm'><input type='hidden' name='data' value='" + data + "' ></form>");
                $('#tempForm').submit();
                $("tempForm").remove();
                return false;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="dvData">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="ItemName">
                            <HeaderTemplate>
                                <asp:LinkButton ID="LinkButton1" Text="Linkbutton" runat="server"></asp:LinkButton>
                            </HeaderTemplate>
                            <ItemTemplate>
                                  <asp:Label ID="txtItemsName" runat="server"
                                    Text='<%# Eval("ItemsName")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
           
        </form>
    </body>
    </html>

    In behind code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                var button = GridView1.HeaderRow.FindControl("LinkButton1") as LinkButton;
                button.ForeColor = System.Drawing.Color.Red;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "export", "Search_ExportToExcel()", true);
     
            }


    Best Regards

    Cathy

    Friday, February 17, 2017 5:47 AM