FormView is a data bound control.
Used to insert, display, edit, update, and delete data in asp.net that renders
a single record at a time. FormView control is similar to DetailsView in
asp.net but the only difference is that DetailsView has a built-in tabular
rendering whereas FormView requires a user-defined template to insert,
display, edit, update and delete data.
The FormView supports the
following templates:
Ø ItemTemplate used to render the
particular record displayed in the FormView
Ø HeaderTemplate used to specify an
optional header row
Ø FooterTemplate used to specify an
optional footer row
Ø EmptyDataTemplate
when
the FormView DataSource lacks any records, the EmptyDataTemplate is used in place of the ItemTemplate for rendering the
control's markup
Ø PagerTemplate
can be
used to customize the paging interface for FormView that have paging enabled
Ø EditItemTemplate
/ InsertItemTemplate used to customize the
editing interface or inserting interface for FormView that support such
functionality.
FormView
Control has following important properties:
Property
|
Description
|
AllowPaging
|
Indicates Whether Paging is enabled
|
DataKeyNames
|
Used to specify names of the primary key columns in the table
|
DataSource
|
Used to specify a dataset or data reader as source
of data
|
DataSourceID
|
Used to specify a DataSource control as source of data
|
DefaultMode
|
Used to specify the default mode that can be read-only,
edit or insert
|
Empty Data Text
|
Used to specify the text display when DataSource is empty.
|
EnablePagingCallbacks
|
Indicates whether paging will postback only details
view or FormView without causing entire page postback
|
Fields
|
Collections fields
|
PageIndex
|
Used to get
or set index of the current page
|
PagerSettings
|
Used to specify the settings related to page
|
DataItemCount
|
Return number of items in underlying DataSource
|
DataltemIndex
|
Used to get index of the Data item displayed
|
FooterRow and
HeaderRow
|
Used to get reference of footer and header row
|
Rows
|
Collection of rows
|
PageCount
|
Returns the number of pages in details view or FormView
|
SelectedValue
|
Returns primary key column value of selected item
|
FormView
Control has following important events:-
Event
|
Description
|
ModeChanging
|
Raised whenever the mode is changing
|
ItemInserting
|
Raised when user click on insert button
|
ItemUpdating
|
Raised when user click on update button
|
ItemDeleting
|
Raised when user click on delete button
|
ItemCommand
|
Raised when user click on any button
|
PageIndexChanging
|
Raised when user change from one page to another
|
Example: - the following example demonstrates how to work with FormView
control.
1. Add a page to the website, take a
FormView control on it, and set its id as FormView1 and within the html source
of the page design layout for the FormView as follows.
<asp:FormView ID="FormView1" runat="server" AllowPaging="True" EnableModelValidation="True" OnItemDeleting="FormView1_ItemDeleting" OnItemUpdating="FormView1_ItemUpdating" OnModeChanging="FormView1_ModeChanging" OnPageIndexChanging="FormView1_PageIndexChanging"
BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Left" OnItemCommand="FormView1_ItemCommand">
<ItemTemplate>
<table border="2">
<tr>
<td>
</td>
<td>
ID <asp:Label ID="lbl1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"username") %>'></asp:Label>
</td>
<td>
Password <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"password") %>'></asp:Label>
</td>
<td>
NAME <asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"email") %>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="lnkbtn1" runat="server" CommandName="edit">edit</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="delete">delete</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"username") %>'></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"password") %>'></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"email") %>'></asp:TextBox>
<asp:LinkButton ID="lnkbtn2" runat="server" CommandName="update">update</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="cancel">cancel</asp:LinkButton>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="ADD">ADD</asp:LinkButton>
</FooterTemplate>
</asp:FormView>
2. Now In the web.config file create the
connection string as:
<connectionStrings><add name="constr"
connectionString="uid=sa;pwd=nagesh;database=nagesh;" providerName="System.Data.SqlClient"/>
</connectionStrings>
3. Within the code create a method
with name Binddata as follows and call it in page
load event.
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["constr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Binddata();// this function is
called when the page is loaded.
}
}
public void Binddata()
{
string cmdtxt = "select *
from account";
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdtxt, con);
da.Fill(ds);
FormView1.DataSource = ds;
FormView1.DataBind();
}
4. Write the following code
within the ItemCommand, ItemDeleting, ItemUpdating, ModeChanging and PageIndexChanging events of the FormView.
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "ADD")
{
// Here FindControl will find the TextboxId in which
adding will be done.
TextBox
l = FormView1.FindControl("TextBox3")
as TextBox;
string
i = l.Text;
TextBox
a = FormView1.FindControl("TextBox4")
as TextBox;
string
p = a.Text;
TextBox
b = FormView1.FindControl("TextBox5")
as TextBox;
string
m = b.Text;
SqlCommand
cmd = new SqlCommand();
cmd.CommandText = "insert into
account(username,password,email)values(@name,@pass,@email)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = i;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = p;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = m;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("/FormView.aspx");
Binddata();
}
}
protected
void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
Label
l = FormView1.FindControl("lbl1") as Label;
string
i = l.Text;
SqlCommand
cmd = new SqlCommand();
cmd.CommandText = "delete from account
where username=@id";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = i;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Binddata();
}
protected
void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
TextBox
l = FormView1.FindControl("txt1") as TextBox;
string
i = l.Text;
TextBox a = FormView1.FindControl("TextBox1") as TextBox;
string
p = a.Text;
TextBox
b = FormView1.FindControl("TextBox2")
as TextBox;
string
m = b.Text;
SqlCommand
cmd = new SqlCommand();
cmd.CommandText = "insert into
account(username,password,email)values(@name,@pass,@email)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = i;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = p;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = m;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("/FormView.aspx");
Binddata();
}
protected
void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
FormView1.ChangeMode(FormViewMode.Edit);
Binddata();
if (e.CancelingEdit ==
true)
{
Response.Redirect("/FormView.aspx");
}
}
protected
void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
Binddata();
}
post other controls also
ReplyDelete