Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to delete a row on gridview with a JavaScript button and remain on the same page
    text
    copied!<p>I am displaying a set of records through gridview, and <code>edit</code> and <code>delete</code> buttons next to them. I am having a problem in the record deletion section. The behavior I want is the following: the user clicks the button, a JavaScript validation function is called and after the button click the records are deleted, but the user remains on the same page with the rest of the records. How can I do this while remaining on the same page?</p> <pre><code>&lt;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt; &lt;div&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt; &amp;nbsp; &lt;asp:HyperLink NavigateUrl="~/Entry.aspx" runat="server" text="Add New Record" /&gt; &lt;/div&gt; &lt;div&gt; &lt;asp:GridView runat="server" ID="grdView" AutoGenerateColumns="false" Height="100%" Width="100%" onselectedindexchanged="grdView_SelectedIndexChanged" &gt; &lt;Columns&gt; &lt;asp:BoundField DataField="Prod_Id" HeaderText="product id " HeaderStyle-BackColor="Azure" /&gt; &lt;asp:BoundField DataField="Prod_Name" HeaderText="Product Name" HeaderStyle-BackColor="Azure" /&gt; &lt;asp:BoundField DataField="Unit_Price" HeaderText="Unit Price " HeaderStyle-BackColor="Azure" /&gt; &lt;asp:BoundField DataField="In_Hand" HeaderText="In Hand" HeaderStyle-BackColor="Azure" /&gt; &lt;asp:BoundField DataField="Fixed" HeaderText="Fixed" HeaderStyle-BackColor="Azure" /&gt; &lt;asp:BoundField DataField="Status" HeaderText="Status" HeaderStyle-BackColor="Azure" /&gt; &lt;asp:HyperLinkField DataNavigateUrlFields="Prod_Id" DataNavigateUrlFormatString="edit.aspx?Prod_Id={0}" Text="Edit" /&gt; &lt;asp:ButtonField ButtonType="Link" Text="Delete" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <blockquote> <p>code behind part </p> </blockquote> <pre><code>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(); } } } </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload