我在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>");
}
}
}
}
第一次做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>");
}
}
}
}
解决方案 »
- SQl asp.net C# context.Response.Write(ret);返回
- cookie有效时间出现重大问题,求助!!!
- 求一个网页从运行到结束的所有事件按顺序来(谢谢大家)
- 高分是手段,求助是目的,散分是结果:用户Calendar控件的问题(月份转换时的bug)
- 绑定后的Datalist不能用单选,页面的结果是复选的
- datagrid编辑框的问题
- 急求Popup Calendar组件?
- 怎么将本机配置成smtp服务器?
- 为什么iis6.0下面会出现“HTTP 错误 404 - 文件或目录未找到。”这个错误???
- ashx如何处理资料文件(resx)
- 对像存入Session的问题
- 求助:MVC3.0参数为空是怎么回事
我把GridView的事件全移除了,就剩一个RowCommand事件了。
删除数据后,在刷新页面,还是会出现开头所讲的问题。就是每次刷新都会 进RowCommond事件绑定的函数。并且,参数e.CommandArgument都带有上次删除行的ID值。
你是按浏览器的刷新吧?或者F5?
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();
}.......
测试完全没问题
我是用 ObjectDataSource + GridView 来做的。
gv_Vaccinate.DataBind(),在删除成功后重新绑定一次数据。正常是应该改这样做么?
建议使用
Response.Redirect( Request.UrlReferrer.ToString());重定向页面
<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章。
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>");
}
}
}
}
我用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();
}
}