阻止图像按钮进行PostBack

时间:2016-03-02 09:42:11

标签: c# asp.net

我正在使用asp.net网站,我Imagebutton在点击事件上调用c#功能。我想阻止此图像按钮创建一个postBack。

我尝试过添加

OnClientClick="return false;"UseSubmitBehavior="false"

但它对我不起作用。

你有解决方案吗?

我尝试通过客户端函数调用服务器函数,如下所示,但我的代码似乎有问题:

<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>

enter image description here

这是背后的代码:

[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();
    }
}

谢谢

2 个答案:

答案 0 :(得分:0)

首先,您无法将db.addEmployees(new Employee(id,from,to)); db.addGroup(new Group(Integer.valueOf(userId),name,session,groupId,groupName)); 传递给gridview

如果您真的想要将网格导出到Excel,请使用客户端库从表标记生成excel,或者执行部分回发并生成excel并写入响应。

请参阅此plunker以在javascript中导出到Excel。

答案 1 :(得分:0)

谢谢你们的建议。我能够以这种方式解决它。

我添加了一个静态类,它有静态List和静态字符串。我在WebForm中称这个类。这样在PostBack期间保留GridViews的数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Global
/// </summary>
public static class Global
{

    public static List<GridView> GridViewList = new List<GridView>();
    public static string ReportType = "";
}

以下C#函数使用静态类中的数据导出到excel。

public  void GridToExcel()
{
    if (berthOccupancyDataGridView.Rows.Count > 0)
    {
        Response.Clear();
        //HttpContext.Current.Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=BerthOccupancy.xls");
        //HttpContext.Current.Response.Charset = "";
        Response.ContentType = "text/csv";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        berthOccupancyDataGridView.RenderControl(htw);

        if (Global.ReportType == "Year To Date")
        {
            foreach (GridView gv in Global.GridViewList)
            {
                gv.RenderControl(htw);
            }
        }
        else if(Global.ReportType == "Monthly")
        {
            recapGridView.RenderControl(htw);

        }

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