Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Oct 23, 2009 08:46 PM by Deepayan Nandi
Member
169 Points
356 Posts
Sep 18, 2008 11:56 AM|LINK
hi all,
i'm trying to use a formview control to display the data, does anyone know how this can be done,
i've search the web, but just keep coming back to the wizard of binding to datasource from 2.0
cheers
lindows
All-Star
98783 Points
9691 Posts
Moderator
Sep 18, 2008 12:31 PM|LINK
It's a lot more work, but here's an ADO CRUD example for you utilizing a FormView:
ASPX
<%@ page autoeventwireup="true" codefile="CRUDWithADO.aspx.cs" inherits="FormView_CRUDWithADO" language="C#" masterpagefile="~/MasterPages/Default.master" title="FormView: CRUD With ADO" %> <asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1"> <asp:label id="lblMessage" runat="server" enableviewstate="false" /> <asp:formview id="fvShipper" runat="server" allowpaging="True" datakeynames="ShipperID" onitemdeleting="fvShipper_ItemDeleting" oniteminserting="fvShipper_ItemInserting" onitemupdating="fvShipper_ItemUpdating" onmodechanging="fvShipper_ModeChanging" onpageindexchanging="fvShipper_PageIndexChanging" style="margin-top: 20px;"> <edititemtemplate> ShipperID: <asp:label id="lblShipperID" runat="server" text='<%# Eval("ShipperID") %>' /><br /> CompanyName: <asp:textbox id="txtCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:textbox id="txtPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="btnUpdate" runat="server" causesvalidation="True" commandname="Update" text="Update" /> <asp:linkbutton id="btnCancel" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" /> </edititemtemplate> <insertitemtemplate> CompanyName: <asp:textbox id="txtCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:textbox id="txtPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="btnInsert" runat="server" causesvalidation="True" commandname="Insert" text="Insert" /> <asp:linkbutton id="btnCancel" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" /> </insertitemtemplate> <itemtemplate> ShipperID: <asp:label id="lblShipperID" runat="server" text='<%# Eval("ShipperID") %>' /><br /> CompanyName: <asp:label id="lblCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:label id="lblPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="EditButton" runat="server" causesvalidation="False" commandname="Edit" text="Edit" /> <asp:linkbutton id="btnDelete" runat="server" causesvalidation="False" commandname="Delete" text="Delete" /> <asp:linkbutton id="btnNew" runat="server" causesvalidation="False" commandname="New" text="New" /> </itemtemplate> </asp:formview> </asp:content>
CODE-BEHIND
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.UI.WebControls; public partial class FormView_CRUDWithADO : System.Web.UI.Page { private void SetData() { SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM [Shippers]", conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); try { conn.Open(); DataSet ds = new DataSet(); adapter.Fill(ds); fvShipper.DataSource = ds; fvShipper.DataBind(); } finally { if (conn != null) { conn.Close(); } } } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.SetData(); } } protected void fvShipper_ModeChanging(object sender, FormViewModeEventArgs e) { fvShipper.ChangeMode(e.NewMode); this.SetData(); } protected void fvShipper_PageIndexChanging(object sender, FormViewPageEventArgs e) { fvShipper.PageIndex = e.NewPageIndex; this.SetData(); } protected void fvShipper_ItemInserting(object sender, FormViewInsertEventArgs e) { TextBox txtCompanyName = fvShipper.FindControl("txtCompanyName") as TextBox; TextBox txtPhone = fvShipper.FindControl("txtPhone") as TextBox; if (txtCompanyName == null) { return; } if (txtPhone == null) { return; } SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand( "INSERT INTO [Shippers] ([CompanyName], [Phone]) VALUES (@CompanyName, @Phone); SELECT @ShipperID = SCOPE_IDENTITY()", conn); cmd.Parameters.AddWithValue("CompanyName", txtCompanyName.Text); cmd.Parameters.AddWithValue("Phone", txtPhone.Text); cmd.Parameters.Add("ShipperID", SqlDbType.Int); cmd.Parameters["ShipperID"].Direction = ParameterDirection.Output; try { conn.Open(); if (cmd.ExecuteNonQuery().Equals(1)) { lblMessage.Text = String.Format( "Shipper '{0}' successfully added.", cmd.Parameters["ShipperID"].Value); fvShipper.ChangeMode(FormViewMode.ReadOnly); this.SetData(); } } finally { if (conn != null) { conn.Close(); } } } protected void fvShipper_ItemUpdating(object sender, FormViewUpdateEventArgs e) { TextBox txtCompanyName = fvShipper.FindControl("txtCompanyName") as TextBox; TextBox txtPhone = fvShipper.FindControl("txtPhone") as TextBox; if (txtCompanyName == null) { return; } if (txtPhone == null) { return; } SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand( "UPDATE [Shippers] SET [CompanyName] = @CompanyName, [Phone] = @Phone WHERE (ShipperID = @ShipperID)", conn); cmd.Parameters.AddWithValue("ShipperID", fvShipper.DataKey["ShipperID"]); cmd.Parameters.AddWithValue("CompanyName", txtCompanyName.Text); cmd.Parameters.AddWithValue("Phone", txtPhone.Text); try { conn.Open(); if (cmd.ExecuteNonQuery().Equals(1)) { lblMessage.Text = String.Format( "Shipper '{0}' successfully updated.", cmd.Parameters["ShipperID"].Value); fvShipper.ChangeMode(FormViewMode.ReadOnly); this.SetData(); } } finally { if (conn != null) { conn.Close(); } } } protected void fvShipper_ItemDeleting(object sender, FormViewDeleteEventArgs e) { SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand( "DELETE FROM [Shippers] WHERE (ShipperID = @ShipperID)", conn); cmd.Parameters.AddWithValue("ShipperID", fvShipper.DataKey["ShipperID"]); try { conn.Open(); if (cmd.ExecuteNonQuery().Equals(1)) { lblMessage.Text = String.Format( "Shipper '{0}' successfully deleted.", cmd.Parameters["ShipperID"].Value); fvShipper.ChangeMode(FormViewMode.ReadOnly); this.SetData(); } } finally { if (conn != null) { conn.Close(); } } } }
2 Points
2 Posts
Oct 23, 2009 08:46 PM|LINK
thanx lot for the code behind ....
thanx a million times
lindows
Member
169 Points
356 Posts
how to populate formview control with only codebind - vb.net
Sep 18, 2008 11:56 AM|LINK
hi all,
i'm trying to use a formview control to display the data, does anyone know how this can be done,
i've search the web, but just keep coming back to the wizard of binding to datasource from 2.0
cheers
lindows
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: how to populate formview control with only codebind - vb.net
Sep 18, 2008 12:31 PM|LINK
It's a lot more work, but here's an ADO CRUD example for you utilizing a FormView:
ASPX
<%@ page autoeventwireup="true" codefile="CRUDWithADO.aspx.cs" inherits="FormView_CRUDWithADO" language="C#" masterpagefile="~/MasterPages/Default.master" title="FormView: CRUD With ADO" %> <asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1"> <asp:label id="lblMessage" runat="server" enableviewstate="false" /> <asp:formview id="fvShipper" runat="server" allowpaging="True" datakeynames="ShipperID" onitemdeleting="fvShipper_ItemDeleting" oniteminserting="fvShipper_ItemInserting" onitemupdating="fvShipper_ItemUpdating" onmodechanging="fvShipper_ModeChanging" onpageindexchanging="fvShipper_PageIndexChanging" style="margin-top: 20px;"> <edititemtemplate> ShipperID: <asp:label id="lblShipperID" runat="server" text='<%# Eval("ShipperID") %>' /><br /> CompanyName: <asp:textbox id="txtCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:textbox id="txtPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="btnUpdate" runat="server" causesvalidation="True" commandname="Update" text="Update" /> <asp:linkbutton id="btnCancel" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" /> </edititemtemplate> <insertitemtemplate> CompanyName: <asp:textbox id="txtCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:textbox id="txtPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="btnInsert" runat="server" causesvalidation="True" commandname="Insert" text="Insert" /> <asp:linkbutton id="btnCancel" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" /> </insertitemtemplate> <itemtemplate> ShipperID: <asp:label id="lblShipperID" runat="server" text='<%# Eval("ShipperID") %>' /><br /> CompanyName: <asp:label id="lblCompanyName" runat="server" text='<%# Bind("CompanyName") %>' /><br /> Phone: <asp:label id="lblPhone" runat="server" text='<%# Bind("Phone") %>' /><br /> <asp:linkbutton id="EditButton" runat="server" causesvalidation="False" commandname="Edit" text="Edit" /> <asp:linkbutton id="btnDelete" runat="server" causesvalidation="False" commandname="Delete" text="Delete" /> <asp:linkbutton id="btnNew" runat="server" causesvalidation="False" commandname="New" text="New" /> </itemtemplate> </asp:formview> </asp:content>CODE-BEHIND
Microsoft MVP - ASP.NET
Deepayan Nan...
Member
2 Points
2 Posts
Re: how to populate formview control with only codebind - vb.net
Oct 23, 2009 08:46 PM|LINK
thanx lot for the code behind ....
thanx a million times