<asp:FormView ID="fv" runat="server" Height="206px" Width="347px" AllowPaging=true
OnModeChanging="ChangMode"
OnPageIndexChanging="pagechanged"
OnItemDeleting="Delete"
OnItemInserting="Insert"
OnItemUpdating="Update"
CellPadding="4" ForeColor="#333333"
>
<
FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditItemTemplate>
<table style="width: 100%; height: 100%">
<tr>
<td align="right" style="width: 100px">ID:</td>
<td align="left" style="width: 100px">
<asp:Label ID="theIDLabel1" runat="server"
Text='<%# Eval("companyID") %>'></asp:Label></td>
</tr>
<tr>
<td align="right" style="width: 100px">Name:</td>
<td align="left" style="width: 100px">
<asp:TextBox ID="theNameTextBox" runat="server"
Text='<%# Bind("companyName") %>'>
</asp:TextBox></td>
</tr>
<tr>
<td align="right" style="width: 100px">City:</td>
<td align="left" style="width: 100px">
<asp:TextBox ID="theCityTextBox" runat="server"
Text='<%# Bind("companyCity") %>'>
</asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton> - <asp:LinkButton
ID="UpdateCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</td>
</tr>
</table>
<br />
</EditItemTemplate>
<InsertItemTemplate>
<table style="width: 100%; height: 100%">
<tr>
<td align="right" style="width: 100px">Name:</td>
<td align="left" style="width: 100px">
<asp:TextBox ID="theNameTextBox" runat="server"
Text='<%# Bind("companyName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" style="width: 100px">City:</td>
<td align="left" style="width: 100px">
<asp:TextBox ID="theCityTextBox" runat="server"
Text='<%# Bind("companyCity") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:LinkButton ID="InsertButton"
runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert"></asp:LinkButton> -
<asp:LinkButton ID="InsertCancelButton"
runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</td>
</tr>
</table>
<br />
</InsertItemTemplate>
<ItemTemplate>Sample Database<br />
<table style="width: 150px; height: 150px">
<tr>
<td align="right" style="width: 100px">ID:</td>
<td align="left" style="width: 100px">
<asp:Label ID="theIDLabel" runat="server" Text='<%# Eval("companyID") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="right" style="width: 100px">Name:</td>
<td align="left" style="width: 100px">
<asp:Label ID="theNameLabel" runat="server" Text='<%# Bind("companyName") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="right" style="width: 100px">City:</td>
<td align="left" style="width: 100px">
<asp:Label ID="theCityLabel" runat="server"
Text='<%# Bind("companyCity") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:LinkButton ID="NewButton"
runat="server" CausesValidation="False" CommandName="New" Text="New"></asp:LinkButton> -
<asp:LinkButton ID="EditButton"
runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> -
<asp:LinkButton ID="DeleteButton"
runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<PagerTemplate>
Page :<%=fv.PageIndex+1%>
<asp:LinkButton ID="likp" runat=server
CommandName="Page" CommandArgument="Prev" >« Prev</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat=server
CommandName="Page" CommandArgument="Next" >Next »</asp:LinkButton>
</PagerTemplate>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
</asp:FormView>
public partial class formvieweditupdate : System.Web.UI.Page
{
SqlConnection dbConn = null;
SqlDataAdapter da = null;
DataTable dTable = null;
String strConnection = null;
String strSQL = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindData();
}
}
private void bindData()
{
try
{
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
strSQL = "SELECT companyid, companyname,companycity from companydetail
order by companyname";
da = new SqlDataAdapter(strSQL, dbConn);
dTable = new DataTable();
da.Fill(dTable);
fv.DataSource = dTable;
fv.DataBind();
}
finally
{
if (dbConn != null)
{
dbConn.Close();
}
}
}
protected void Update(object sender, FormViewUpdateEventArgs e)
{
try
{
int cid = Convert.ToInt32((fv.Row.FindControl("theIDLabel1") as Label).Text);
string cname = (fv.Row.FindControl("theNameTextBox") as TextBox).Text;
string ccity = (fv.Row.FindControl("theCityTextBox") as TextBox).Text;
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand da = new SqlCommand( "Update companydetail
set
<a href="mailto:companyname=@comp,
companycity=@comcity">
companyname=@comp,companycity=@comcity</a> where <a href="mailto:companyid=@cid">companyid=@cid</a>", dbConn);
da.Parameters.Add("@cid", SqlDbType.Int).Value = cid;
da.Parameters.Add("@comp", SqlDbType.VarChar).Value = cname;
da.Parameters.Add("@comcity", SqlDbType.VarChar).Value = ccity;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}
finally
{
if (dbConn != null)
{
dbConn.Close();
bindData();
}
}
}
protected void Insert(object sender, FormViewInsertEventArgs e)
{
try
{
string cname = (fv.Row.FindControl("theNameTextBox") as TextBox).Text;
string ccity = (fv.Row.FindControl("theCityTextBox") as TextBox).Text;
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand da =
new SqlCommand("insert into companydetail values(@comp,@add,@comcity)", dbConn);
da.Parameters.Add("@add", SqlDbType.VarChar).Value ="";
da.Parameters.Add("@comp", SqlDbType.VarChar).Value = cname;
da.Parameters.Add("@comcity", SqlDbType.VarChar).Value = ccity;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}
finally
{
if (dbConn != null)
{
dbConn.Close();
bindData();
}
}
}
protected void Delete(object sender, FormViewDeleteEventArgs e)
{
try
{
int cid = Convert.ToInt32((fv.Row.FindControl("theIDLabel") as Label).Text);
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand da = new SqlCommand("delete from companydetail
where <a href="mailto:companyid=@cid">
companyid=@cid</a>", dbConn);
da.Parameters.Add("@cid", SqlDbType.Int).Value = cid;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}
finally
{
if (dbConn != null)
{
dbConn.Close();
bindData();
}
}
}
protected void ChangMode(object sender, FormViewModeEventArgs e)
{
fv.ChangeMode(e.NewMode);
if (e.NewMode.ToString() != "Edit" && e.NewMode.ToString()!="Insert")
{
bindData();
}
}
protected void pagechanged(object sender, FormViewPageEventArgs e)
{
fv.PageIndex = e.NewPageIndex;
bindData();
}
}