CrudGrid.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrudGrid.aspx.cs" Inherits="CrudGrid" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ShowFooter="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton OnClick="LinkButton1_Click" ValidationGroup="Insert" ID="LinkButton1" runat="server">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox1" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator1" ErrorMessage="Name is a required field" runat="server" ></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="FooterName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="Insert" ControlToValidate="FooterName" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator2" ErrorMessage="Name is a required field" runat="server" ></asp:RequiredFieldValidator>
</EditItemTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="gender" SortExpression="gender">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Text='<%# Bind("gender") %>'>
<asp:ListItem>Select Gender</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator InitialValue="Select Gender" ControlToValidate="DropDownList1" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator3" ErrorMessage="Gender is a required field" runat="server" ></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="FooterddlGender" runat="server" Text='<%# Bind("gender") %>'>
<asp:ListItem>Select Gender</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ValidationGroup="Insert" InitialValue="Select Gender" ControlToValidate="FooterddlGender" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator4" ErrorMessage="Gender is a required field" runat="server" ></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="class" SortExpression="class">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("class") %>'></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox3" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator5" ErrorMessage="Class is a required field" runat="server" ></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("class") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="FooterClass" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="Insert" ControlToValidate="FooterClass" ForeColor="Red" Text="*" SetFocusOnError="true" ID="RequiredFieldValidator6" ErrorMessage="Class is a required field" runat="server" ></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="Insert" runat="server" BackColor="#CCCCCC" ForeColor="#FF3300" />
<asp:ValidationSummary ID="ValidationSummary2" runat="server" BackColor="#CCCCCC" ForeColor="#FF3300" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbcs %>" DeleteCommand="DELETE FROM [student] WHERE [id] = @id" InsertCommand="INSERT INTO [student] ([name], [gender], [class]) VALUES (@name, @gender, @class)" SelectCommand="SELECT * FROM [student]" UpdateCommand="UPDATE [student] SET [name] = @name, [gender] = @gender, [class] = @class WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="gender" Type="String" />
<asp:Parameter Name="class" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="gender" Type="String" />
<asp:Parameter Name="class" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
CrudGrid.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CrudGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["name"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("FooterName")).Text;
SqlDataSource1.InsertParameters["gender"].DefaultValue =
((DropDownList)GridView1.FooterRow.FindControl("FooterDdlGender")).SelectedValue;
SqlDataSource1.InsertParameters["class"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("FooterClass")).Text;
int a = SqlDataSource1.Insert();
if(a>0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts","<script>alert('Insertion Successfull')</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Insertion Failed')</script>");
}
}
}
0 Comments