I am working on asp.net
website and I have Imagebutton
that call c#
function on click event. I want to prevent this image button from creating a postBack.
I have tried adding
OnClientClick="return false;"
and UseSubmitBehavior="false"
but it didn't work for me.
Do you have a solution?
I tried to call the server function through client side function as the following, but it seems there is something wrong with my code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$('#excelImageButton').click(function funcall() {
$.ajax(
{
type: "POST",
url: "BerthOccupancyForm.aspx/GridToExcel",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
});
return false;
});
</script>
This is code behind:
[WebMethod()]
public static void GridToExcel(GridView berthGridView, GridView recap)
{
if (berthGridView.Rows.Count > 0)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=BerthOccupancy.xls");
HttpContext.Current.Response.ContentType = "text/csv";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
berthGridView.RenderControl(htw);
if (RT == "Year To Date")
{
foreach (GridView gv in Code)
{
gv.RenderControl(htw);
}
}
else
{
recap.RenderControl(htw);
}
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
Thanks