Search This Blog

Wednesday, March 21, 2012

Confirm on Delete in Gridview using Javascript ?

When You Press on Delete Button in the Gridview At that time confimation is there For Delete Particular Row from the Database .


Press Delete

At that time confirmation block will be generated.

On aspx Page Write Javascript as shown Below.

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onconfirm() {
        return confirm('Are You Sure ???')
        }
    </script>
</head>

And In the Body Part code inside the gridview :

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
Width="754px" AutoGenerateColumns="False" DataKeyNames="country_id" onrowcancelingedit="GridView1_RowCancelingEdit" onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting"  onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"  onsorting="GridView1_Sorting">
   <Columns>
   </asp:TemplateField>                                                                                                       
<ItemTemplate>
   <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return onconfirm();" CommandArgument='<%# Bind("country_id") %>' CommandName="Delete">Delete</asp:LinkButton>
 </ItemTemplate>
   </asp:TemplateField>                                                               
  </Columns>                                                          
 </asp:GridView>

IN aspx.cs File

DAL_Country d1 = new DAL_Country();
BAL_Country b1 = new BAL_Country();
 public void ShowData()
    {
        DataSet ds = new DataSet();
        ds = b1.SelectData();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        b1.DeleteData(id);
        ShowData();
    }
public DataSet SelectData()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_country",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
BAL_Country.cs File
    public void DeleteData(int id)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from tbl_country where country_id='"+id+"'",con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

No comments:

Post a Comment