First we need to design web page . I added the template fields and label controls as well . Inside the web page we need to drag and drop the text boxes and the button . Here is the HTML Code .Also make sure you add the reference this project from web service project .
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
width: 450px;
margin-bottom: -1px;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Student_Id"
OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns>
<asp:TemplateField HeaderText="StudentId" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblStudentId" runat="server" Text='<%# Eval("Student_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStudentId" runat="server" Text='<%# Eval("Student_Id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("Address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("EmailID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("EmailID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblMobile" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMobile" runat="server" Text='<%# Eval("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true"
ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="width: 150px">
Name:<br />
<asp:TextBox ID="Name" runat="server" Width="140" />
</td>
<td style="width: 150px">
Address:<br />
<asp:TextBox ID="Address" runat="server" Width="140" />
</td>
<td style="width: 150px">
Email:<br />
<asp:TextBox ID="Email" runat="server" Width="140" />
</td>
<td style="width: 150px">
Mobile:<br />
<asp:TextBox ID="Mobile" runat="server" Width="140" />
</td>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
</form>
</body>
</html>
Here is the code in code behind file . Just copy and paste it ....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
StudentServiceReference.StudentServiceSoapClient service = new StudentServiceReference.StudentServiceSoapClient();
GridView1.DataSource = service.Get();
GridView1.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
StudentServiceReference.StudentServiceSoapClient service = new StudentServiceReference.StudentServiceSoapClient();
service.Insert(Name.Text.Trim(),Address.Text.Trim(),Email.Text.Trim(),Mobile.Text.Trim());
this.BindGrid();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int StudentId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string name = (row.FindControl("txtName") as TextBox).Text;
string Address = (row.FindControl("txtAddress") as TextBox).Text;
string Email = (row.FindControl("txtEmail") as TextBox).Text;
string Mobile = (row.FindControl("txtMobile") as TextBox).Text;
StudentServiceReference.StudentServiceSoapClient service = new StudentServiceReference.StudentServiceSoapClient();
service.Update(StudentId, name, Address,Email,Mobile);
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int StudentId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
StudentServiceReference.StudentServiceSoapClient service = new StudentServiceReference.StudentServiceSoapClient();
service.Delete(StudentId);
this.BindGrid();
}
protected void OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
}
}
Here is the screen shot when you run the application into local host .
No comments:
Post a Comment