Simple Gridview

(ASP.NET)


This is the simple gridview with templatefield and boundfield column types. I added the checkbox in the gridview column. Whenever user check the checkbox i alert the checked employee name.

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   
<script type="text/javascript">
        function GetEmp(lnk) {
            if(lnk.checked)
                alert('Selected Employee: ' + lnk.dataset.name);
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <h1 style="margin-left:30%;">Employee Details</h1>
    <div style="margin-left:30%;">
        <asp:GridView AutoGenerateColumns="False" ID="grdvw_employees" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
           
<Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <input onclick="GetEmp(this);" data-name="<%# Eval("Name")%>" type="checkbox" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField  DataField="Name" HeaderText=" Name" />
                <asp:BoundField DataField="Age" HeaderText="Age" />
                <asp:BoundField DataField="Salary" HeaderText="Salary" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <a onclick="EmpAddress(this)">Address</a>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>



Default.aspx.cs file

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(!Page.IsPostBack)
                {
                    grdvw_employees.DataSource = GetEmployees();
                    grdvw_employees.DataBind();
                }
            }
            catch(Exception)
            { }
        }


        public List<EmployeeModel> GetEmployees()
        {
            var lst = new List<EmployeeModel>
            {
               new EmployeeModel{Name="Tom",Age=20,Salary=15000},
               new EmployeeModel{Name="Ram",Age=25,Salary=20000},
               new EmployeeModel{Name="John",Age=28,Salary=25000}
            };

            return lst;
        }
    }

    public class EmployeeModel
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public double Salary { get; set; }
    }



Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
5/3/2014
5/3/2014
Download PDF
Download Project

Comments