Click here to Skip to main content
15,902,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frnds...

In my project i am trying to delete the gridview data with the use of Ajax Modal Popup extender... I designed it but i dont know how to write the code for Delete button Click event...

XML
<asp:TemplateField HeaderText="Delete">
                           <ItemTemplate>
                               <asp:ImageButton ID="Imgdelete" runat="server" CommandName="Delete" OnClientClick="showConfirm(this); return false;"
                                   ImageUrl="~/admin/Images/Delete.gif" Width="15px" />
                           </ItemTemplate>
                       </asp:TemplateField>

<script language="javascript" type="text/javascript">
    var _source;
    var _popup;
    
    function showConfirm(source)
    {
        this._source = source;
        this._popup=$find('mdlpopup');         
        this._popup.show();
    }
    
    function okClick()
    {
        this._popup.hide();                
        __doPostBack(this._source.name, '');        
    }
    
    function cancelClick()
    {
        this._popup.hide();
        this._source=null;
        this._popup=null;
    }

i designed the above code... can anyone help me to write code in button click event...

i wrote this code.. but i am getting argument out of range exception//
C#
protected void Imgdelete_Click(object sender, EventArgs e)
    {
        ImageButton btndelete = sender as ImageButton;
        GridViewRow row = (GridViewRow)btndelete.NamingContainer;

        //string productname = Convert.ToString(btndelete.CommandArgument);
        string productname = Convert.ToString(Quote_Grid.DataKeys[row.RowIndex].Value.ToString());
        string Productname = row.Cells[0].Text;

        con.Open();
        cmd = new SqlCommand("Delete from Quote_pro_details where p_name='" + productname + "' and Quote_ID='" + txtquoteid.Text + "'", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Quote_Grid_Bind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + productname + "details deleted successfully')", true);
        }

    }
Posted
Updated 27-Jul-11 21:54pm
v4

1 solution

Hi,

Try this:

In your client code:

   <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Delete">
  <HeaderTemplate>Delete</HeaderTemplate>
     <ItemTemplate>
        <asp:ImageButton ID="Imgdelete" runat="server" 
        ImageUrl="~/admin/Images/Delete.gif" ToolTip="Delete product" 
        onclick="Imgdelete_Click" OnClientClick="return confirm('Delete this record?');"  />
     </ItemTemplate>
  <FooterTemplate></FooterTemplate>
</asp:TemplateField>


In your code behind:

   protected void Imgdelete_Click(object sender, ImageClickEventArgs e)
{
    GridViewRow row = (GridViewRow)Imgdelete.NamingContainer;
    string productName = row.Cells[0].Text;
    string quoteID  = txtquoteid.Text;
    
    //...Some code here...
    cmd = new SqlCommand("Delete from Quote_pro_details where p_name='" + 
          productName + "' and Quote_ID='" + quoteID + "'", con);
    //... Some Code here...
     
}

Hope this could help:

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 
Comments
nithibs 28-Jul-11 4:38am    
Hi...

Thanks... now i can able to delete... but another problem is it is showing the modalpopup extender for first time deletion only if i delete the data in same grid, onclientclick is not firing... Have any solution for this pblm.......
Al Moje 28-Jul-11 4:48am    
Hi,
I think you must have an UpdatePanel to update your grid. On the click event trigger
that panel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900