日期:2014-05-17 浏览次数:20498 次
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Del"
Text="删除" OnClientClick="return confirm('确定删除吗?')" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
protected void gv_User_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Del")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
Response.Write(iIndex);
string deletStuId = gv_User.DataKeys[iIndex].Value.ToString();
Response.Write(string.Format("您要删除的stuID为{0},您要删除的行是{1}", gv_User.DataKeys[iIndex].Value, iIndex + 1));
string connString = "server=(local);database=db_02;integrated security=sspi;";
string sSql = "Delete from tb_student where stuId=@stuId";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sSql, conn))
{
cmd.Parameters.Add("@stuId", deletStuId);
ClientScript.RegisterStartupScript(Page.GetType(), "", string.Format("<script>alert('删除{0}条记录');</script>", cmd.ExecuteNonQuery()));
setBind();
}
}
}
}
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = e.Row.FindControl("Button1") as Button;
if(btn != null)
{
btn.CommandArgument = e.Row.RowIndex;
}
}