C# 防止图像按钮执行回发
我正在开发C# 防止图像按钮执行回发,c#,asp.net,C#,Asp.net,我正在开发asp.net网站,我有Imagebutton,可以在点击事件时调用c函数。我想阻止此图像按钮创建回发 我试着添加 OnClientClick=“return false;”和使用submitbehavior=“false” 但这对我不起作用 你有解决办法吗 我试图通过客户端函数调用服务器函数,如下所示,但我的代码似乎有问题: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jque
asp.net
网站,我有Imagebutton
,可以在点击事件时调用c
函数。我想阻止此图像按钮创建回发
我试着添加
OnClientClick=“return false;”
和使用submitbehavior=“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>
谢谢首先,您不能将
gridview
传递给webmethod
如果您真的想将网格导出到excel,请使用客户端库从表标记生成excel,或者执行部分回发,生成excel并写入响应
查看此项以在javascript中导出到excel。首先,您不能将
gridview
传递到webmethod
如果您真的想将网格导出到excel,请使用客户端库从表标记生成excel,或者执行部分回发,生成excel并写入响应
请参阅此链接,以在javascript中导出到excel。感谢大家的建议。我就这样解决了 我添加了一个具有静态列表和静态字符串的静态类。我在网络表单中称这个类。这样可以在回发期间保留GridView的数据
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 = "";
}
谢谢你们的建议。我就这样解决了 我添加了一个具有静态列表和静态字符串的静态类。我在网络表单中称这个类。这样可以在回发期间保留GridView的数据
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 = "";
}
您是否在服务器端的
page\u load
上为该按钮编写了代码?还显示ImageButton的完整aspx如果是webforms,则必须在服务器端控件上处理回发。另一种方法就是不能调用OnClick函数。但您仍然可以切换到AJAX,或者只是将ImageButton替换为simple image,然后调用调用Web方法的JS代码(这基本上是执行类似AJAX操作的另一种方式)@coder我也在页面加载中编写了它,但它对我不起作用。@Lorin我尝试使用WebMethod。但是,它要求方法是静态的,并且我在方法中得到错误。你有例子吗?@MaiShibuya是的,应该是静态的。这是一种有点不推荐的方法。但它仍然有效。查看这两页:您是否为服务器端的按钮在page\u load
上写过?还显示ImageButton的完整aspx如果是webforms,则必须在服务器端控件上处理回发。另一种方法就是不能调用OnClick函数。但您仍然可以切换到AJAX,或者只是将ImageButton替换为simple image,然后调用调用Web方法的JS代码(这基本上是执行类似AJAX操作的另一种方式)@coder我也在页面加载中编写了它,但它对我不起作用。@Lorin我尝试使用WebMethod。但是,它要求方法是静态的,并且我在方法中得到错误。你有例子吗?@MaiShibuya是的,应该是静态的。这是一种有点不推荐的方法。但它仍然有效。看看这两页:
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();
}
}