Click here to Skip to main content
12,887,779 members (46,897 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
hello,
I want to delete the single row in gridview with using template control.I am using oracle database .when i click the delete i want to show the confirmation message box.
Thanks
Posted 17-Feb-12 23:15pm
Updated 17-Feb-12 23:17pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<asp:gridview id="GridView1" runat="server" allowpaging="True" xmlns:asp="#unknown">
        AutoGenerateColumns="False" 
        DataSourceID="cmmentsdatasource" Width="992px" onrowcommand="rowcmd" 
        DataKeyNames='id' CellPadding="0" CssClass="style51" ForeColor="#333333">
        
        
 
        <rowstyle backcolor="#EFF3FB" font-bold="False" height="20px"></rowstyle>
        <columns>
        <asp:templatefield>
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandargument="<%#Eval("id")%>"></asp:linkbutton>
                 </itemtemplate>
                 
                 </asp:templatefield>
                
         <asp:boundfield datafield="id" headertext="ID" sortexpression="id" />
            <asp:boundfield datafield="senderid" headertext="SenderId">
                SortExpression="senderid" />
            <asp:boundfield datafield="sendername" headertext="SenderName">
                SortExpression="sendername" />
            <asp:boundfield datafield="comments" headertext="Comments">
                SortExpression="comments" />
            <asp:boundfield datafield="date" headertext="Date" sortexpression="date" />
        </asp:boundfield></asp:boundfield></asp:boundfield></columns>
       
        
        <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
       </footerstyle>
       
        
 
        <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center">
       </pagerstyle>
       
 
        <selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333">
        </selectedrowstyle>
       
        
 
        <headerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
            Font-Size="Small" Height="20px">
        </headerstyle>
       
        
        <editrowstyle backcolor="#2461BF"></editrowstyle>
        
 
        <alternatingrowstyle backcolor="White"></alternatingrowstyle>
    </asp:gridview>
 

on row command event
 
protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
      // create and open DB connection
        try
        {
           
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
           GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
 
    }
 


O
  Permalink  
Comments
Ramadurai U 18-Feb-12 6:05am
   
thank u sir but here when i click the cancel .the data is deleting . what i to do?
Uma Shankar Patel 18-Feb-12 6:10am
   
<asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandname="delete" commandargument="<%#Eval("id")%>">

and
use
protected void rowcmd(object sender, GridViewCommandEventArgs e)
{
// create and open DB connection
try
{
if(e.Commandname=="delete")
{
Int32 id = Convert.ToInt32(e.CommandArgument);
string comm = "Delete from tblComments where id=@id";
SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
cmd.Parameters.AddWithValue("id", id);
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
}
catch (Exception ex)
{
}

}

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 18 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100