Showing posts with label LinkButton. Show all posts
Showing posts with label LinkButton. Show all posts

How to add a confirm delete option in ASP.Net Gridview

Add a Confirm delete option in ASP.Net


Display multiple details in Gridview we want to delete some row before confirmation messahe open confirm delete that is onclientclick event Using Asp.Net C#.

DEMO


HTML CODING 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <table
        <tr><td>&nbsp;</td><td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:TemplateField HeaderText="Action" ShowHeader="False">
                        <ItemTemplate>

           <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
              
                OnClientClick="return confirm('Are You Sure Want to you Delete?');" Text="Delete">

           </asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UserId" HeaderText="User Id" />
                    <asp:BoundField DataField="UserName" HeaderText="User Name" />
                    <asp:BoundField DataField="Location" HeaderText="Location" />
                    <asp:BoundField DataField="fromdate" HeaderText="FromDates" />
                </Columns>
                <HeaderStyle ForeColor="#CC3300" />
            </asp:GridView>
            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>



 C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

public partial class Mysql_StoredProcedure : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select * from userinformation", con);

        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        con.Close();

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
     
        // Write Delete condition

        Response.Write("<script>alert('Deleted Successfully')</script>");

    }
}



 Add New Webform - Add Gridview - EditColumn -  Add Command field - Delete 





Next - Add - Delete Buttton - Convert into templatefield






Next - GridView - Edit Template - Add Delete - OnClientclick = return confirm('delete?');






Next - Html source shown below like






Next - Gridview Row events - Double click - RowDeleting



Add delete condition for required in rowdeleting event






How to use Gridview Inside Another Controls using Asp.Net C#

 Gridview Inside Another Controls


Gridview inside access another controls like  Calender,Imagebutton,Gridview,Linkbutton Using in Asp.Net C#.

                                  DEMO

          
                             Download

                       HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use Gridview Inside Another Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h4>Girdview Inside Button,Gridview,Linkbutton,
            ImageButton,Calender
        </h4>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle ForeColor="#999966" />
            <Columns>
                <asp:TemplateField HeaderText="Employee Name">
                    <ItemTemplate>
                        <table><tr><td>
                            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
                            </td><td>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Empname") %>'></asp:Label>
                            </td></tr>
                            <tr><td>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
                                </td><td>
                                    <asp:ImageButton ID="ImageButton1" runat="server" Height="50px" ImageUrl="~/Penguins.jpg" OnClick="ImageButton1_Click" Width="50px" />
                                </td></tr>
                            <tr><td>
                                <h4>Child Gridview</h4>
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Empname">
                                            <ItemTemplate>
                                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Empname") %>'></asp:Label>
                                                <br />
                                                <br />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                                </td><td>
                                    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
                                </td></tr>
                            <tr><td></td><td></td></tr>
                        </table>
                        &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Id" HeaderText="Employee Id">
                <ControlStyle Font-Size="Larger" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
            </Columns>
            <HeaderStyle ForeColor="#3399FF" />
            <RowStyle ForeColor="Gray" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class GridviewEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con=new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString() );
        con.Open();
        SqlDataAdapter adp=new SqlDataAdapter ("select * from Employee",con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Button inside of Griview
        Button btn = (Button)sender;
        GridViewRow gr = (GridViewRow)btn.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this,this.GetType(),"Script","alert('"+lbl.Text+"')",true);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        // LinkButton inside of Griview
        LinkButton lbt = (LinkButton)sender;
        GridViewRow gr = (GridViewRow)lbt.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script1", "alert('" + lbl.Text + "')", true);
       
    }  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // GridView Inside of Another Gridview
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView grChild = e.Row.FindControl("GridView2") as GridView;
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from Employee", con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            grChild.DataSource = ds;
            grChild.DataBind();       
       
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton lbt = (ImageButton)sender;
        GridViewRow gr = (GridViewRow)lbt.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script12", "alert('" + lbl.Text + "')", true);
       
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Calendar cal = (Calendar)sender;
        GridViewRow gr = (GridViewRow)cal.NamingContainer;
        Calendar cal1 = (Calendar)gr.FindControl("Calendar1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script12", "alert('" +"Selected Date is = " + cal1.SelectedDate.ToString("dd/MM/yyyy") + "')", true);
    }

}
                    


Add New Webform - Select Gridview from toolbox - add controls to inside of gridview Calender,Linkbutton,Image button 




Next - Double Click All Controls - Add the Conditions for access gridview values and Child gridview rows




Retrive values from gridview rows events 





Html Coding for Generated all Controls