前台: <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">删除</asp:LinkButton>
后台: protected void LinkButton1_Click(object sender, EventArgs e) { LinkButton myB = (LinkButton)sender; int myIndex = ((GridViewRow)myB.NamingContainer).RowIndex;//获得行号 int myId = Convert.ToInt16(GridView1.DataKeys[myIndex].Value);//获得id Conn conn = new Conn(); SqlConnection con = conn.getConn(); SqlCommand sqlcom; string sqlstr = "delete from news where id='" + myId + "'"; sqlcom = new SqlCommand(sqlstr, con); sqlcom.ExecuteNonQuery(); con.Close(); bind(); }
|