GridView.RowCommand的问题。。。。。。。。。。。。。。。。

csz_1987 2011-09-30 11:43:05
我在RowCommand事件内做删除选中行的操作,删除也能成功。但是提示删除成功后,立即刷新页面,则还会进入RowCommand事件绑定的函数,并且在e.CommandArgument里还存在之前删除的那条数据的ID,接着会弹出脚本框提示“删除失败”。

请问这个问题要如何解决?还是删除当前行我这种做法从源头上就错了?
第一次做asp.net。谢谢大家。

protected void gv_Vaccinate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DataDelete")
{
int ID = 0;
if (int.TryParse(e.CommandArgument.ToString(), out ID))
{
bool result = (new Vaccinate()).Update(ID, 1);
if (result)
{
ClientScript.RegisterStartupScript(this.GetType(), "DelSuccess", "<script>alert('删除成功!')</script>");
gv_Vaccinate.DataBind();
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "DelError", "<script>alert('删除失败!')</script>");
}
}
}
}
...全文
291 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
csz_1987 2011-10-09
  • 打赏
  • 举报
回复
有没有人回答一下我16楼的问题?
wangzhi0321 2011-09-30
  • 打赏
  • 举报
回复

我用ObjectDataSource这样操作也是完全OK,你参考下,
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "agree")
{
ZxSecondOperator.getZxUpdate(1, e.CommandArgument.ToString());
ObjectDataSource1.SelectParameters.Clear();
ObjectDataSource1.TypeName = "ZxSecondOperator";
ObjectDataSource1.SelectMethod = "getZxNewPl";
ObjectDataSource1.SelectParameters.Add("newid",DbType.String,String.Format("{0}",Request.QueryString["ID"]));
ObjectDataSource1.Select();

}
if (e.CommandName == "aginst")
{
ZxSecondOperator.getZxUpdate(2, e.CommandArgument.ToString());
ObjectDataSource1.SelectParameters.Clear();
ObjectDataSource1.TypeName = "ZxSecondOperator";
ObjectDataSource1.SelectMethod = "getZxNewPl";
ObjectDataSource1.SelectParameters.Add("newid", DbType.String, String.Format("{0}", Request.QueryString["ID"]));
ObjectDataSource1.Select();
}
}
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 net_lover 的回复:]

按F5刷新 应该是重复提交了。
建议使用
Response.Redirect( Request.UrlReferrer.ToString());重定向页面
[/Quote]

顺便问一下,我的这种做法是否正确?一般的做法是什么样的?
CalvinR 2011-09-30
  • 打赏
  • 举报
回复
学习了
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
代码如下。留给将来遇到相同问题初学者。
谢谢,孟子E章。


protected void gv_Vaccinate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DataDelete")
{
int ID = 0;
if (int.TryParse(e.CommandArgument.ToString(), out ID))
{
bool result = (new Vaccinate()).Update(ID, 1);
if (result)
{
ClientScript.RegisterStartupScript(this.GetType(), "DelSuccess", "<script>alert('删除成功!')</script>");
gv_Vaccinate.DataBind();
Response.Redirect(Request.UrlReferrer.ToString());
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "DelError", "<script>alert('删除失败!')</script>");
}
}
}
}
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 net_lover 的回复:]

按F5刷新 应该是重复提交了。
建议使用
Response.Redirect( Request.UrlReferrer.ToString());重定向页面
[/Quote]

谢谢。问题解决了。。。
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 net_lover 的回复:]

贴你的代码看看
[/Quote]


//aspx里的代码
<asp:GridView ID="gv_Vaccinate" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="ods_Vaccinate" PageSize="16" OnRowCommand="gv_Vaccinate_RowCommand"
Width="100%" OnRowDataBound="gv_Vaccinate_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# m_Index++%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DEVICEID" HeaderText="设备号" SortExpression="DEVICEID" />
<asp:BoundField DataField="RAISERS" HeaderText="畜主" SortExpression="RAISERS" />
<asp:BoundField DataField="IN_DATE" HeaderText="防疫时间" SortExpression="IN_DATE" />
<asp:BoundField DataField="VACCINATE_NAME" HeaderText="注射疫苗名称" SortExpression="VACCINATE_NAME" />
<asp:BoundField DataField="REGION_NAME" HeaderText="乡镇名称" SortExpression="REGION_NAME" />
<asp:BoundField DataField="EARMARK_SERIAL" HeaderText="耳标号" SortExpression="EARMARK_SERIAL" />
<asp:BoundField DataField="LASTDATE" HeaderText="完成时间" SortExpression="LASTDATE" />
<asp:BoundField DataField="ISINIT" HeaderText="初始化" SortExpression="ISINIT" />
<asp:TemplateField HeaderText="删除" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ib_Delete" runat="server" CausesValidation="False" CommandName="DataDelete"
CommandArgument='<%# Eval("ID") %>' OnClientClick="return confirm('是否删除该条记录?')"
ToolTip="删除" ImageUrl="/images/delete.png" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Right" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>




//aspx.cs里的代码
protected void gv_Vaccinate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DataDelete")
{
int ID = 0;
if (int.TryParse(e.CommandArgument.ToString(), out ID))
{
bool result = (new Vaccinate()).Update(ID, 1);
if (result)
{
ClientScript.RegisterStartupScript(this.GetType(), "DelSuccess", "<script>alert('删除成功!')</script>");
//gv_Vaccinate.DataBind();
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "DelError", "<script>alert('删除失败!')</script>");
}
}
}
}
孟子E章 2011-09-30
  • 打赏
  • 举报
回复
按F5刷新 应该是重复提交了。
建议使用
Response.Redirect( Request.UrlReferrer.ToString());重定向页面
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wangzhi0321 的回复:]

执行RowCommand事件后重新绑定一次数据就不会出现这种情况,我不怎么明白gv_Vaccinate.DataBind();起什么作用,重新绑定一般都会另写一个函数,从没这样绑定过,给代码楼主参考:
protected void getValue()
{
string sql = "select * from Links";
SqlComman……
[/Quote]

我是用 ObjectDataSource + GridView 来做的。
gv_Vaccinate.DataBind(),在删除成功后重新绑定一次数据。

正常是应该改这样做么?
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 net_lover 的回复:]

就是每次刷新都会 进RowCommond事件绑定的函数?
你是按浏览器的刷新吧?或者F5?
[/Quote]

按F5刷新
wangzhi0321 2011-09-30
  • 打赏
  • 举报
回复
执行RowCommand事件后重新绑定一次数据就不会出现这种情况,我不怎么明白gv_Vaccinate.DataBind();起什么作用,重新绑定一般都会另写一个函数,从没这样绑定过,给代码楼主参考:
protected void getValue()
{
string sql = "select * from Links";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "LinkID" };
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
string sql = "delete from Links where LinkID=" + e.CommandArgument.ToString();
int i = MySqlHelper.ExecuteNonQuery(sql, CommandType.Text, false);
if (i > 0)
{
msg_list.InnerText = "删除成功";
}
else
{
msg_list.InnerText = "删除失败";
}
getValue();
}.......
测试完全没问题
孟子E章 2011-09-30
  • 打赏
  • 举报
回复
就是每次刷新都会 进RowCommond事件绑定的函数?
你是按浏览器的刷新吧?或者F5?
孟子E章 2011-09-30
  • 打赏
  • 举报
回复
贴你的代码看看
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 net_lover 的回复:]

出现这样的情况是一般是其他事件影响的。
[/Quote]

我把GridView的事件全移除了,就剩一个RowCommand事件了。
删除数据后,在刷新页面,还是会出现开头所讲的问题。

就是每次刷新都会 进RowCommond事件绑定的函数。并且,参数e.CommandArgument都带有上次删除行的ID值。
孟子E章 2011-09-30
  • 打赏
  • 举报
回复
出现这样的情况是一般是其他事件影响的。
csz_1987 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 net_lover 的回复:]

你还写了分页事件了吧?
[/Quote]


是啊 我启动分页了。
这个要怎么处理?
孟子E章 2011-09-30
  • 打赏
  • 举报
回复
你还写了分页事件了吧?
danceboy_520 2011-09-30
  • 打赏
  • 举报
回复
检查代码

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧