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> 展开
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> 展开
展开全部
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是为了避免给插入按钮也增加了确认提示
效果:
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是为了避免给插入按钮也增加了确认提示
效果:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在处理事件的时候,你做一下处理
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
{
其他处理代码。。。。
}
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
{
其他处理代码。。。。
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询