C# Asp.Net中gridview中的删除按钮怎么加提示而不是直接删除记录,在button中没有找到onclick事件也?

//点击删除所触发的事件publicvoidSTmyGrid_delete(objectsource,System.Web.UI.WebControls.GridView... //点击删除所触发的事件
public void STmyGrid_delete(object source, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
//创建删除指定ID连锁店的sql语句
string STstrsql = "delete from STDep where STDepID = @STDepID";
//创建数据库的SqlCommand对象
SqlCommand STcmd = new SqlCommand(STstrsql,STconn);
//向SqlCommand对象添加参数
STcmd.Parameters.Add(new SqlParameter("@STDepID",SqlDbType.Int,4));
//向参数赋值
STcmd.Parameters["@STDepID"].Value = int.Parse(STmyGrid.DataKeys[(int)e.RowIndex].Value.ToString());
STcmd.Connection.Open();
try
{
//执行sql语句
STcmd.ExecuteNonQuery();
}
catch(SqlException)
{

}
STcmd.Connection.Close();
BindGrid();
}
这个 是后台代码,
前台代码:
onrowdeleting="STmyGrid_delete"

<asp:GridView id="STmyGrid" runat="server" AllowPaging="True"
PageSize="8" AutoGenerateColumns="False"
DataKeyNames="STDepID" Width="100%"
onpageindexchanging="STmyGrid_Page"
onrowcancelingedit="STmyGrid_cancel"
onrowdeleting="STmyGrid_delete"
onrowediting="STmyGrid_edit"
onrowupdating="STmyGrid_update" >
<Columns>
<asp:BoundField DataField="STDepID" HeaderText="连锁店ID"></asp:BoundField>
<asp:BoundField DataField="STDepName" HeaderText="连锁店名字"></asp:BoundField>
<asp:BoundField DataField="STDepMaster" HeaderText="连锁店负责人"></asp:BoundField>
<asp:BoundField DataField="STDepInfo" HeaderText="连锁店信息"></asp:BoundField>
<asp:CommandField ShowEditButton="true" />
<asp:ButtonField Text="删除" CommandName="Delete"></asp:ButtonField>
</Columns>
<PagerSettings Mode="Numeric" />
</asp:GridView>
展开
 我来答
blakli123
推荐于2018-04-12 · TA获得超过272个赞
知道小有建树答主
回答量:235
采纳率:0%
帮助的人:246万
展开全部
<asp:ButtonField Text="删除" CommandName="Delete" OnClientClick="return confirm('确认要删除么');"></asp:ButtonField>
更多追问追答
追问
我在属性窗口找不到OnClientClick属性呢?
追答
事件类型当然找不到了,直接在前台代码里写
zly0304302
2012-05-17 · TA获得超过2462个赞
知道小有建树答主
回答量:744
采纳率:0%
帮助的人:596万
展开全部
1)ButtonField 是没有OnClientClick属性的,这样写会报错吧.你可以使用
TemplateField里面加个LinkButton 就行,
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtDelete" Text="删除" runat="server" OnClientClick="return confirm('确认要删除么');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2)后台获得控件,然后Attributes.Add下事件就行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
int i = 0;
foreach(Control c in e.Row.Cells[0].Controls)
{
Response.Write(c.GetType().BaseType.ToString()+"<br/>");
if (c.GetType().BaseType.ToString() == "System.Web.UI.WebControls.LinkButton" && i == 2)
{
((LinkButton)c).Attributes.Add("onclick", "if (!confirm('确认删除所选吗?')) return false;");

}
i++;
}
}
}
在foreach内部检测类型是否为LinkButton,然后转换,加入i是为了避免给插入按钮也增加了确认提示
效果:
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友9066726
2012-05-17 · TA获得超过559个赞
知道小有建树答主
回答量:152
采纳率:0%
帮助的人:88.9万
展开全部
1.后台
protected void STmyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((Button)e.Row.Cells[删除列索引].Controls[1]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
}
}
2. 前台<asp:Button ID="btnDelete" runat="server" Text="删除" CommandArgument='<%# Eval("GoodsId") %>' CommandName="DeleteGoods" />

3.后台补充:
protected void STmyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
//从页面获得商品Id
string id = e.CommandArgument.ToString();
switch (e.CommandName)
{
case "AddGoods":
break;
case "EditGoods":
break;
case "DeleteGoods":
Goods goods = GoodsManager.GetGoodsById(id);
if (goods != null )
{
//删除相关商品详情

}
break;
}
//绑定数据源
dataBind();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
a4257748
2012-05-21 · 超过19用户采纳过TA的回答
知道答主
回答量:54
采纳率:0%
帮助的人:46万
展开全部
在处理事件的时候,你做一下处理
public void STmyGrid_delete(object source, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
DialogResult dr = MessageBox.Show( "看到提示了吧? ", "提示信息 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
if (dr == DialogResult.OK)
{
执行你的代码.........
}
else
{
其他处理代码。。。。
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式