form view

AbiMurugan2 352 views 25 slides Jan 07, 2019
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

FORM VIEW in distributed technologies


Slide Content

WELCOME TO MY PRESENATION

FORM VIEW

The working of Form View control is same as Detail View control but the default UI of Form View control is different. Detail View control displays the records in tabular format. Form View control does not have predefined layout but it depend upon template . According to the need of application, we can display the data with the help of template. Using the Form View Control

We can insert, update, delete and paging the record in Form View. Adding validation controls to a Form View is easier than Detail View control. For displaying data, Item Template is used. Displaying Data with the Form View

using System; using System.Data ; using System.Data.SqlClient ; using System.Configuration ; using System.Web.UI.WebControls ; using System.Web.UI ; public partial class FormViewDemo : System.Web.UI.Page {      SqlConnection conn;      SqlDataAdapter adapter;      DataSet ds;      SqlCommand cmd ;     string cs = ConfigurationManager.ConnectionStrings [" conString "]. ConnectionString ;     protected void Page_Load (object sender, EventArgs e)

 {         if (! IsPostBack )         {              PopulateFormView ();         }     }     protected void PopulateFormView ()     {         try         {              conn = new SqlConnection ( cs );             adapter = new SqlDataAdapter ("select * from tblEmps ", conn );              ds = new DataSet ();              adapter.Fill ( ds );             FormView1.DataSource = ds ;             FormView1.DataBind();         }

 catch (Exception ex)         {             Label1.Text = "ERROR :: " + ex.Message ;         }     }     protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)     {         FormView1.ChangeMode( e.NewMode );          PopulateFormView ();     } } The ItemTemplate supports a databinding expression that is used to bind the database table column. The Eval () or Bind()  method is used to retrieves the values of these columns. 

< ItemTemplate >      < table border="1">             < tr >                 < th ><b> EmpID :</b></ th >                       < td ><%# Eval (" EmpID ") %></ td></ tr >     < tr >                      <td><b>Name:</b></td>                       < td ><%# Eval ("Name") %></ td> </ tr >                < tr >                        < td><b>Gender:</b></td>                        < td ><%# Eval ("Gender") %></td ></ tr >                < tr >                       <td><b>Salary:</b></ td>  <td><%# Eval ("Salary") %></td > </ tr >                 < tr >                  < td><b>Address:</b></td>                         

<td><%# Eval ("Address") %></td> </ tr >                 < tr >                         <td><b> DepID :</b></td>                         <td><%# Eval (" DepID ") %>                 </ tr >         </table>          < asp:LinkButton   ID=" lnkEdit "    Text="Edit"   CommandName ="Edit"    runat ="server" />          < asp:LinkButton   ID=" lnkNew "    Text="New" CommandName ="New" runat ="server" />          < asp:LinkButton ID=" lnkDelete " Text="Delete" CommandName ="Delete" runat ="server" />                          </ ItemTemplate >

We can perform paging with Form View Control by using its AllowPaging property. By default Allow Paging property is false. Set the value of Allow Paging property as true for performing the paging. It will generate the paging link automatically. Write code in Page Index Changing event as follows. Paging Through Data with the Form View Control

protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) {         FormView1.PageIndex = e.NewPageIndex ;          PopulateFormView (); } OUTPUT:

The Form View control uses templates to edit records . It enables us to use Edit ItemTemplate and specify the user interface . If we want simply display the record then use Eval () method otherwise use Bind() method. For editing the record you must have editable control, therefore we have used Text Box control within Edit Item Template and bind the Text property of Text Box Editing Data with the Form View Control

If we click on Edit link, then Update and Cancel link will display in place of Edit Link. By default these links are not available in Form View control. We must provide these links in template fields explicitly. Form View control has a Data Key Names property that is used to hold the name of the primary key from the database table. We have to specify a primary key explicitly when editing records.

This Link Button supports a Command Name property which has the value Edit. When we click on Edit link the Form View mode is changed from Read Only mode to  "Edit"  mode . We can use Button or Image Button control in place of Link Button. The Bind(" EmpID ") method binds the Employee ID column database table to the Text property of the Text Box control.

< EditItemTemplate >         <table border="1">                 < tr >                         < th ><b> EmpID :</b></ th >                         <td>< asp:TextBox ID=" txtEmpID " Text='<%# Bind(" EmpID ") %>' runat ="Server" /></td>                 </ tr >                  < tr >                         <td><b>Name:</b></td>                         <td>< asp:TextBox ID=" txtName " Text='<%# Bind("Name") %>' runat ="Server" /></td>                 </ tr >                 < tr > <td><b>Gender:</b></td>                         <td>< asp:TextBox ID=" txtGender " Text='<%# Bind("Gender") %>' runat ="Server"/> </td>                 </ tr >                  < tr >

<td><b>Salary:</b></td>                         <td>< asp:TextBox ID=" txtSalary " Text='<%# Bind("Salary") %>' runat ="Server" /></td>                 </ tr >                 < tr >                         <td><b>Address:</b></td>                         <td>< asp:TextBox ID=" txtAddress " Text='<%# Bind("Address") %>' runat ="Server" /></td>                 </ tr >                 < tr >                         <td><b> DepID :</b></td>                         <td>< asp:TextBox ID=" txtDeptID " Text='<%# Bind(" DepID ") %>' runat ="Server" /></td>

</ tr >         </table>                           < asp:LinkButton ID=" lnkUpdate "  Text="Update" CommandName ="Update" runat ="server" />         < asp:LinkButton ID=" lnkCancel "  Text="Cancel" CommandName ="Cancel" runat ="server" /> </ EditItemTemplate > When we click on Update link ItemUpdating event fires. We have already added TextBox in EditItemTemplate . Access these textboxes with the help of FindControl method. Write code for updating the record as given below.

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) {          int ID = Convert.ToInt32((FormView1.FindControl(" txtEmpID ") as TextBox ).Text);         string empName = (FormView1.FindControl(" txtName ") as TextBox ).Text;         string empGender = (FormView1.FindControl(" txtGender ") as TextBox ).Text;         double empSalary = Convert.ToDouble ((FormView1.FindControl(" txtSalary ") as TextBox ).Text);         string empAddress = (FormView1.FindControl(" txtAddress ") as TextBox ).Text;          int depID = Convert.ToInt32((FormView1.FindControl(" txtDeptID ") as TextBox ).Text);         string updateQuery = "update tblEmps set name='"+ empName +"',         gender='"+ empGender +"',salary="+ empSalary +",address='"+ empAddress +"',          depid ="+ depID +" where empid ="+ID;          conn = new SqlConnection ( cs );          cmd = new SqlCommand ( updateQuery , conn );

  conn.Open ();          cmd.ExecuteNonQuery ();          conn.Close ();         FormView1.ChangeMode( FormViewMode.ReadOnly );                     PopulateFormView (); } OUTPUT:

We can insert new record into the database with the help of Form View Control. Form View control supports Insert Item Template that is used to insert new record in database. The Item Template contains the "New" Link Button. When you click on "New" Link Button, control goes from Read Only mode to Insert mode. Insert and Cancel Link Button is appeared in place of "New" Link Button. Inserting Data with the Form View Control

< InsertItemTemplate >         <table border="1">                 < tr >                         < th ><b> EmpID :</b></ th >                         <td>< asp:TextBox ID=" txtEmpID " Text='<%# Bind(" EmpID ") %>' runat ="Server" /></td>                 </ tr >                 < tr >                         <td><b>Name:</b></td>                         <td>< asp:TextBox ID=" txtName " Text='<%# Bind("Name") %>' runat ="Server" /></td>                 </ tr >                 < tr >                         <td><b>Gender:</b></td>                         <td>< asp:TextBox ID=" txtGender " Text='<%# Bind("Gender") %>' runat ="Server" /> </td>                 </ tr >                 < tr >

 <td><b>Salary:</b></td>                         <td>< asp:TextBox ID=" txtSalary " Text='<%# Bind("Salary") %>' runat ="Server "/> </ td>                 </ tr >                 < tr >                         <td><b>Address:</b></td>                         <td>< asp:TextBox ID=" txtAddress " Text='<%# Bind("Address") %>' runat ="Server" /> </td>                 </ tr >                 < tr >                         <td><b> DepID :</b></td>                         <td>< asp:TextBox ID=" txtDeptID " Text='<%# Bind(" DepID ") %>' runat ="Server" /></td>                 </ tr >         </table>                           < asp:LinkButton ID=" lnkInsert " Text="Insert" CommandName ="Insert" r

< asp:LinkButton ID=" lnkCancel "  Text="Cancel" CommandName ="Cancel" runat ="server" /> </ InsertItemTemplate > When you click on Insert Link Button, ItemInserting event is fires. protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)     {         string ID = (FormView1.FindControl(" txtEmpID ") as TextBox ).Text;         string empName = (FormView1.FindControl(" txtName ") as TextBox ).Text;         string empGender = (FormView1.FindControl(" txtGender ") as TextBox ).Text;         double empSalary = Convert.ToDouble ((FormView1.FindControl(" txtSalary ") as TextBox ).Text);         string empAddress = (FormView1.FindControl(" txtAddress ") as TextBox ).Text;         string depID = (FormView1.FindControl(" txtDeptID ") as TextBox ).Text;         string insertQuery = "insert into tblEmps values("+ID+",'"+ empName +"','"+ empGender +"',         "+ empSalary +",'"+ empAddress +"',"+ depID +")";          conn = new SqlConnection ( cs );          cmd = new SqlCommand ( insertQuery , conn );

conn.Open ();          cmd.ExecuteNonQuery ();          conn.Close ();         FormView1.ChangeMode( FormViewMode.ReadOnly );                     PopulateFormView (); } OUTPUT: