CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Posts
    7

    confirm delete in gride view

    //file1.aspx
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TxtJobID" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtJobTitle" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    Text="Update" OnClientClick="return confirm('Are You Sure');" Font-Bold="True" />
    <br />
    <br />
    <asp:Label ID="lblMessage" runat="server" Text="Label" Width="474px"
    style="background-color: #00cccc"></asp:Label><br />
    <br />
    <aspataGrid id="DataGrid1" OnItemCommand="ItemCommand"
    style="Z-INDEX: 101; LEFT: 175px; TOP: 105px"
    runat="server" CssClass="tx-tbl-Gr-lml" Width="474px" AllowPaging="true" >
    <Columns>
    <asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit">
    </asp:ButtonColumn>
    <asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete">
    </asp:ButtonColumn>
    </Columns>
    </aspataGrid><br />
    </div>
    </form>
    </body>

    //file1.aspx.cs

    public partial class _Default : System.Web.UI.Page
    {
    OracleConnection cn;
    OracleCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
    cn = new OracleConnection("data source=dev;user id=hr;password=hr");
    if (!Page.IsPostBack)
    {
    BindData();
    }
    }
    void BindData()
    {
    string strsql = "select job_id,job_title from jobs";
    OracleDataAdapter da = new OracleDataAdapter(strsql, cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    DataGrid1.DataSource = dt;
    DataGrid1.DataBind();
    }
    protected void ItemCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (e.CommandName == "Edit")
    {
    FillTheData(e.Item.Cells[2].Text, e.Item.Cells[3].Text);
    lblMessage.Text = "";
    }
    if (e.CommandName == "Delete")
    {
    lblMessage.Text = "Need Code to call confirm() frunction ";
    }
    }
    void FillTheData(string job_id, string job_title)
    {
    TxtJobID.Text = job_id;
    TxtJobTitle.Text = job_title;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    string strsql = "Update jobs set job_title="+"'"+TxtJobTitle.Text+"'"+" where job_id="+"'"+TxtJobID.Text+"'";
    cmd = new OracleCommand(strsql, cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    BindData();
    lblMessage.Text = "Updated Successfully";
    }
    catch (Exception ex)
    {
    lblMessage.Text = ex.Message;
    }
    finally
    {
    cn.Close();
    }
    }
    }

  2. #2
    Join Date
    Jan 2009
    Location
    Cochin, India
    Posts
    40

    Smile Re: confirm delete in gride view

    You can use javascript for confirmation. Just convert your delete button to a template fieldin the grid. Replace your data grid code with

    Code:
    <asp:DataGrid id="DataGrid1" OnItemCommand="ItemCommand"
    style="Z-INDEX: 101; LEFT: 175px; TOP: 105px"
    runat="server" CssClass="tx-tbl-Gr-lml" Width="474px" AllowPaging="true" >
    <Columns>
    <asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit">
    </asp:ButtonColumn>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirmdelete();" CausesValidation="false" CommandName="Delete" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    and put this javascript function in the head element of your page after the title.

    Code:
        <script language="javascript" type="text/javascript">
        function confirmdelete()
        { 
          var ok = confirm("Should I delete");
          return ok;
        }
        </script>
    Now if you click on the delete button you will be asked for confirmation. And the page will post back only if you click on the ok button.

    Warm Regards,
    Jay
    Support Resort
    http://www.supportresort.com
    Bringing offshore expertise to the world

  3. #3
    Join Date
    Aug 2005
    Posts
    7

    Re: confirm delete in gride view

    Thanks alot the code you provided worked for me.
    Once again thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured