A potentially dangerous Request.Form value was detected from the client on click of image button

Viewed 39

I want to download Excel file of HTML table. But while click of button i am getting error Like A potentially dangerous Request.Form value was detected from the client

To resolve this issues i used

ValidateRequest="false"

on <%@ Page ........%> But no luck

ASPX

<asp:ImageButton ID="ImgBtnExcel" runat="server" src="../../Images/excel_icon.gif" ToolTip="Export to excel" Style="cursor: pointer;" OnClick="ImgBtnExcel_Click" />
<input type="hidden" id="hdnReworkStatusData" name="hdnReworkStatusData" />

My Method

private void ExportToExcel(string data)
    {
        if (data == null || data == string.Empty)
            return;
        //data = HttpUtility.UrlDecode(data);
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Data.xls");
        Response.Charset = "";
        Response.ContentType = "application/excel";
        HttpContext.Current.Response.Write(data);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }

Button Click Event

protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        ExportToExcel(Request.Form["hdnReworkStatusData"]);
    }

I am binding my HTML table with AJAX call which is working as expected. Then i am trying to store these table value into that Hidden Field hdnReworkStatusData something Like this:

var AllDataGrid = [];
        function load_DataForGrid() {
            //debugger;
            var params = {
                UserName: $('#txtUserName').val(), UserID: $('#txtUserIDEdit').val(),
                Status: $('input[name="Status"]:checked').val()
            };
            $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(params),
                url: "MobileUser_Login.aspx/Get_Data",
                dataType: "json",
                async: false,
                success: function (result) {
                    FunctionalDataForGrid(result.d)
                    OnSuccessFunctionalGridCall();

                },
                error: function (err) { // Added this event to capture the failed requests.
                    console.log(err);
                }
            });

            function FunctionalDataForGrid(pdata) {
                AllDataGrid = pdata;
            }
            function OnSuccessFunctionalGridCall(data, status) {

                d = AllDataGrid;

                //d = data.GetDraftedSDDResult.ResultObject;

                var tbl = "<table id='lineItemTable' border='1' cellspacing='0' cellpadding='3' width='100%'>";
                if ($('#test').length != 0)  // remove table if it exists              
                { $('#test').empty(); }

                if (d.length == 0) {
                    alert('No Record to display');
                }
                else {

                    tbl += "<tr class='tr-Highlighted' style='height:30px;'><td class='align-text-center' >Sr.No</td><td class='align-text-center' style='font-weight:bold !important'>User Login</td><td id='tdUserName' class='align-text-center' style='font-weight:bold !important'>User Name</td><td class='align-text-center' style='font-weight:bold !important'>Status</td><td class='align-text-center' style='font-weight:bold !important'>Action</td></tr>";

                    for (i = 0; i < d.length; i++) {
                        tbl += "<tr><td class='width4p align-text-center'>"
                            + (i + 1) + "</td><td class='width10p'>"
                            + d[i].USER_LOGIN + "</td><td class='width24p'>"
                            + d[i].USER_NAME + "</td><td class='width14p'>"
                            + d[i].Status + "</td><td class='width4p'>" +
                            "<a href='#my_modal' data-toggle='modal' class='open-AddBookDialog' data-book-id='my_id_value'><img src='Images/edit.png' height='20' width='20'></a>&nbsp&nbsp&nbsp&nbsp<a href='#my_modal' class='delete-Record'><img src='Images/delete.png' height='18' width='18'></a></td>";
                    }
                    $("#test").append(tbl);
                    
                    $('#hdnReworkStatusData').val(tbl);

                    return;
                }
            }
            return false;
        };

Can any one help me why i am getting this Error where i am doing mistake

0 Answers
Related