GridView里 ButtonField 两次提交的一个Bug .
class Data
{
public string D { get; set; }
public string A { get; set; }
}
public partial class WebForm1 : System.Web.UI.Page
{
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
i++;
Response.Write(i);
if (IsPostBack == false)
{
bind();
}
}
private void bind()
{
this.gv.AutoGenerateColumns = false;
this.gv.Columns.Clear();
List<Data> dat = new List<Data>();
dat.Add(new Data() { D = "ddd" });
this.gv.Columns.Add(new BoundField() { DataField = "D" });
this.gv.Columns.Add(new ButtonField() { ButtonType = ButtonType.Image, ImageUrl = "warningIcon.png",CommandName="Delete" });
this.gv.DataSource = dat;
this.gv.DataBind();
(this.gv.Rows[0].Cells[1].Controls[0] as ImageButton).Attributes["onclick"] = "alert('ok');";
}
protected void gcon_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("(" + i.ToString() + ")");
}
{
public string D { get; set; }
public string A { get; set; }
}
public partial class WebForm1 : System.Web.UI.Page
{
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
i++;
Response.Write(i);
if (IsPostBack == false)
{
bind();
}
}
private void bind()
{
this.gv.AutoGenerateColumns = false;
this.gv.Columns.Clear();
List<Data> dat = new List<Data>();
dat.Add(new Data() { D = "ddd" });
this.gv.Columns.Add(new BoundField() { DataField = "D" });
this.gv.Columns.Add(new ButtonField() { ButtonType = ButtonType.Image, ImageUrl = "warningIcon.png",CommandName="Delete" });
this.gv.DataSource = dat;
this.gv.DataBind();
(this.gv.Rows[0].Cells[1].Controls[0] as ImageButton).Attributes["onclick"] = "alert('ok');";
}
protected void gcon_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("(" + i.ToString() + ")");
}
前台GridView 只要绑定 onrowdeleting="gv_RowDeleting" 即可.
两次提交的原因是:
ImageButton 生成了标签 <input type="image" onclick="javascript:__doPostBack('gv','Delete$0')" />
input type="Image" 本身会 Post 一次, 而 __doPostBack 也会提交一次. 造成两次提交.
根本解决,是弃用 ButtonField中的Image 绑定 .换用 LinkButton , 在 Page_Load 里增加:
(this.gv.Rows[0].Cells[1].Controls[0] as LinkButton).Controls.Add(
new HtmlImage() { Src = "warningIcon.png" });
或用正则表达式,在 __doPostBack 后 添加 ;return false ; 只能在控件内部实现, 不能在页面上全局实现.页面的 Render 里实现会影响Ajax .
重写 GridView 的 RenderContents 方法:
protected override void RenderContents(HtmlTextWriter writer)
{
//base.RenderContents(writer);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.RenderContents(htmlWriter);
string html = stringWriter.ToString();
string[] eachOne = Regex.Split(
html,
@"(?<=<[a-z]*\s+.*[a-z]*=""[^""]*__doPostBack\s*\(\s*'[^""]*""(?=\s))"
, RegexOptions.IgnoreCase | RegexOptions.Singleline
);
for (int i = 0; i < eachOne.Length - 1; i++)
{
eachOne[i] = eachOne[i].Insert(eachOne[i].Length - 1, ";return false;");
}
html= string.Join("", eachOne);
writer.Write(html);
}
{
//base.RenderContents(writer);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.RenderContents(htmlWriter);
string html = stringWriter.ToString();
string[] eachOne = Regex.Split(
html,
@"(?<=<[a-z]*\s+.*[a-z]*=""[^""]*__doPostBack\s*\(\s*'[^""]*""(?=\s))"
, RegexOptions.IgnoreCase | RegexOptions.Singleline
);
for (int i = 0; i < eachOne.Length - 1; i++)
{
eachOne[i] = eachOne[i].Insert(eachOne[i].Length - 1, ";return false;");
}
html= string.Join("", eachOne);
writer.Write(html);
}
作者:NewSea 出处:http://newsea.cnblogs.com/
QQ,MSN:iamnewsea@hotmail.com 如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。 |