GridView删除操作弹出确认对话框不好用,怎么回事啊?
页面的html 代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="griddelete.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="border:solid 1px green;width:40%">
<tr>
<td style="background-color:Green">
<span style="font-size: 14pt; color: #ffffff"><strong>
GridView控件进行删除数据</strong></span></td>
</tr>
<tr>
<td>
<asp:GridView Width="100%" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="orderid">
<Columns>
<asp:BoundField DataField="orderid" HeaderText="订单号" />
<asp:BoundField DataField="shipname" HeaderText="订单名称" />
<asp:BoundField DataField="shipaddress" HeaderText="订单地址" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除操作" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
页面的后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
banddelete();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + e.Row.Cells[1].Text + "\"订单吗?')");
}
}
}
private void banddelete()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["griddelete"].ConnectionString);
con.Open();
string cmdtext = "select top 5 * from orders ";
SqlDataAdapter sda = new SqlDataAdapter(cmdtext, con);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["griddelete"].ConnectionString);
con.Open();
string deleteid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string cmdtext = "delete from orders where orderid='" + deleteid + "'";
SqlCommand cmd = new SqlCommand(cmdtext, con);
try
{
cmd.ExecuteNonQuery();
Response.Write("<script>alert('删除成功');</script>");
}
catch
{
return;
}
banddelete();
con.Close();
}
}
请各位高手大哥帮帮小弟吧