我正在通过 gridview 显示一组记录,以及edit
它们delete
旁边的按钮。我在记录删除部分遇到问题。我想要的行为如下:用户单击按钮,调用 JavaScript 验证函数,单击按钮后记录被删除,但用户与其余记录保持在同一页面上。如何在保持在同一页面上的同时执行此操作?
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<br />
<asp:HyperLink NavigateUrl="~/Entry.aspx" runat="server" text="Add New Record" />
</div>
<div>
<asp:GridView runat="server" ID="grdView" AutoGenerateColumns="false" Height="100%"
Width="100%" onselectedindexchanged="grdView_SelectedIndexChanged" >
<Columns>
<asp:BoundField DataField="Prod_Id" HeaderText="product id " HeaderStyle-BackColor="Azure" />
<asp:BoundField DataField="Prod_Name" HeaderText="Product Name" HeaderStyle-BackColor="Azure" />
<asp:BoundField DataField="Unit_Price" HeaderText="Unit Price " HeaderStyle-BackColor="Azure" />
<asp:BoundField DataField="In_Hand" HeaderText="In Hand" HeaderStyle-BackColor="Azure" />
<asp:BoundField DataField="Fixed" HeaderText="Fixed" HeaderStyle-BackColor="Azure" />
<asp:BoundField DataField="Status" HeaderText="Status" HeaderStyle-BackColor="Azure" />
<asp:HyperLinkField DataNavigateUrlFields="Prod_Id" DataNavigateUrlFormatString="edit.aspx?Prod_Id={0}" Text="Edit" />
<asp:ButtonField ButtonType="Link" Text="Delete" />
</Columns>
</asp:GridView>
</div>
</asp:Content>
部分代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
void binddata()
{
con = new SqlConnection("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
da = new SqlDataAdapter("Select * from Products", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
grdView.DataSource = ds;
grdView.DataBind();
}
protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
{
var P_id = ds.Tables[0].Rows[0]["Prod_Id"].ToString();
SqlConnection con = new SqlConnection();
con.ConnectionString = ("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
string qry = "DELETE FROM PRODUCTS WHERE Prod_Id='" +P_id+ "'";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
谢谢