Thursday, 5 January 2012

Select,Edit,Delete,Sort In Gridview and And Ajax Progressbar in Asp.net

Design:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewanipulation.aspx.cs" Inherits="GridViewanipulation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table width="100%">
     <tr>
       <td width="25%" align="right">Name:</td>
        <td width="25%">
            <asp:TextBox ID="TxtName" runat="server"></asp:TextBox></td><td width="25%"></td><td width="25%"></td>
     </tr>
       <tr>
       <td width="25%" align="right">Address:</td>
        <td width="25%">
            <asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox></td><td width="25%"></td><td width="25%"></td>
     </tr>
       <tr>
       <td colspan="2" align="center">
           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
           <asp:Button ID="BtnSave" runat="server" Text="Save" onclick="BtnSave_Click" />
           </ContentTemplate>
           </asp:UpdatePanel>
           </td>
        <td width="25%"></td><td width="25%"></td><td width="25%"></td>
     </tr>
    </table>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
        AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="RollNo"
        DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
        onrowdeleting="GridView1_RowDeleting"
        onrowupdating="GridView1_RowUpdating">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
         
            <asp:BoundField DataField="RollNo" HeaderText="RollNo" InsertVisible="False"
                ReadOnly="True" SortExpression="RollNo" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Address" HeaderText="Address"
                SortExpression="Address" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>
   
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
    <ProgressTemplate>
         <div style="top:200px;left:400px;right:697px; position:absolute;">
           <img alt="" src="Image/progress_bar.gif" />
   `     </div>
    </ProgressTemplate>
    </asp:UpdateProgress>
 
    </form>
</body>
</html>


Code(C#):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GridViewanipulation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rollno = Convert.ToInt32((GridView1.Rows[e.RowIndex].Cells[1]).Text);
        SqlDataSource1.DeleteCommand = "delete from Student where RollNo='" + rollno + "'";
        SqlDataSource1.Delete();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int rollno = Convert.ToInt32((GridView1.Rows[e.RowIndex].Cells[1]).Text);
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        string address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        SqlDataSource1.UpdateCommand = "update Student set Name='" + name + "',Address='" + address + "' where RollNo='" + rollno + "'";
        SqlDataSource1.Update();
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        SqlDataSource1.InsertCommand = "insert into Student(Name,Address) values ('" + TxtName.Text + "','" + TxtAddress.Text + "')";
        SqlDataSource1.Insert();
        System.Threading.Thread.Sleep(5000);
    }
}

PageView:


No comments:

Post a Comment