Category: Data Controls

Sep 23 2009

Asp.net formview control

Links to Category table used to bind data in Formview click here

Step1: Drag formView control form data controls in Toolbox

<asp:FormView ID=”FormView1″ runat=”server”>

</asp:FormView>

asp.net_formview_control

asp.net_formview_control

Click Edit Template

formview editTemplate

formview editTemplate

ItemTemplate: used to display data (Edit Button,New Button)

EditItemTemplate: used to edit data (update button)

InsertItemTemplate: used to insert new record (Save Button)

ItemTemplate: insert table with 3 rows and 2 columns

Drag 2 labels and 2 Buttons

formview_ItemTemplate

formview_ItemTemplate

Bind Label1 to CategoryName

<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(”CategoryName”) %>’></asp:Label>

Bind Label2 to CategoryDesc

<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(”CategoryDesc”) %>’></asp:Label>

Given CommandName Properties to Edit Button and AddNew Button

<asp:Button CommandName=”Edit” Text=”Edit” ID=”EditFrm” runat=”server” />

<asp:Button CommandName=”Insert” Text=”Add New” ID=”InsertFrm” runat=”server” />

Complete code for ItemTemplate in Formview control

<ItemTemplate>

<table>

<tr>

<td class=”style2″>

CategoryName</td>

<td>

<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(”CategoryName”) %>’> </asp:Label>

</td>

</tr>

<tr>

<td class=”style2″>

CategoryDesc</td>

<td>

<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(”CategoryDesc”) %>’> </asp:Label>

</td>

</tr>

<tr>

<td class=”style2″>

&nbsp;</td>

<td>

<asp:Button CommandName=”Edit” Text=”Edit” ID=”EditFrm” runat=”server” />

<asp:Button CommandName=”Insert” Text=”Add New” ID=”InsertFrm” runat=”server” />

</td>

</tr>

</table>

</ItemTemplate>


FormView Edit Template

Step1: insert table with 3 rows and 2 columns

Drag 2 textbox’s and 2 Buttons

Update Button and Cancel Button

<asp:Button CommandName=”update” Text=”update” ID=”EditFrm” runat=”server” />

<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”EditCancelFrm” runat=”server” />

Complete code for FormView EditTemplate Field

<EditItemTemplate>

<table>

<tr>

<td class=”style2″>

CategoryName</td>

<td>

<asp:TextBox ID=”txtCatName” runat=”server”

Text=’<%# Eval(”CategoryName”) %>‘></asp:TextBox>

</td>

</tr>

<tr>

<td class=”style2″>

CategoryDesc</td>

<td>

<asp:TextBox ID=”txtCatDesc” runat=”server”

Text=’<%# Eval(”CategoryDesc”) %>’></asp:TextBox>

</td>

</tr>

<tr>

<td class=”style2″>

&nbsp;</td>

<td>

<asp:Button CommandName=”update” Text=”update” ID=”EditFrm” runat=”server” />

<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”EditCancelFrm” runat=”server” />

</td>

</tr>

</table>

</EditItemTemplate>

FormView InsertTemplate Field

Step1: insert table with 3 rows and 2 columns

Drag 2 textbox’s and 2 Buttons

Save Button and Cancel Button

<asp:Button CommandName=”Save” Text=”Save” ID=”SaveFrm” runat=”server” />

<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”CancelFrm” runat=”server” />

Complete code for FormView InsertTemplate Field

<InsertItemTemplate>

<table>

<tr>

<td class=”style2″>

CategoryName</td>

<td>

<asp:TextBox ID=”InsCatName” runat=”server”

></asp:TextBox>

</td>

</tr>

<tr>

<td class=”style2″>

CategoryDesc</td>

<td>

<asp:TextBox ID=”InsCatDesc” runat=”server”

></asp:TextBox>

</td>

</tr>

<tr>

<td class=”style2″>

&nbsp;</td>

<td>

<asp:Button CommandName=”Save” Text=”Save” ID=”SaveFrm” runat=”server” />

<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”CancelFrm” runat=”server” />

</td>

</tr>

</table>

</InsertItemTemplate>

Code for Default.aspx.cs

using System.Data;

using System.Data.SqlClient;

#region “ConnectionString”

string ConnectionString = “Data Source=192.168.1.23;Initial Catalog=Purchase;User;Password=abc;”;

#endregion

#region “Page_Load”

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetCategories();

}

}

#endregion

#region “GetCategories()”

public void GetCategories()

{

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select *from tblCategory”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();

SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

//objConn.Open();

objDa.Fill(objDs);

//objCmd.ExecuteNonQuery();

FormView1.DataSource = objDs;

FormView1.DataBind();

//objConn.Close();

}

#endregion

Asp.net FormView Paging

protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)

{

FormView1.PageIndex = e.NewPageIndex;

GetCategories();

FormView1.ChangeMode(FormViewMode.ReadOnly);

}

FormView Edit, Delete, Update and Insert Events

protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)

{

//this commandName from ItemTemplate

if (e.CommandName == “Edit”)

{

FormView1.ChangeMode(FormViewMode.Edit);

GetCategories();

}

//this commandName from ItemTemplate

if (e.CommandName == “Insert”)         {

FormView1.ChangeMode(FormViewMode.Insert);

}

//this commandName from EditItemTemplate

if (e.CommandName == “update”)

{

SqlConnection objConn = new SqlConnection(ConnectionString);

string catName = ((TextBox)FormView1.FindControl(”txtCatName”)).Text;

string catDesc = ((TextBox)FormView1.FindControl(”txtCatDesc”)).Text;

objConn.Open();

string query = “Update tblCategory Set categoryname = ‘” + catName + “‘, CategoryDesc = ‘” + catDesc + “‘ Where categoryID=’” + FormView1.DataKey.Value + “‘”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.ExecuteNonQuery();

objConn.Close();

//FormView1.ChangeMode(FormViewMode.Edit);

GetCategories();

}

//commandName from InsertItemTemplate

if (e.CommandName == “Save”)

{

SqlConnection objConn = new SqlConnection(ConnectionString);

string catName = ((TextBox)FormView1.FindControl(”InsCatName”)).Text;

string catDesc = ((TextBox)FormView1.FindControl(”InsCatDesc”)).Text;

objConn.Open();

string query = “insert into tblCategory(CategoryName,CategoryDesc) ”

+ “values(@CategoryName,@CategoryDesc)”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.Parameters.Add(”@CategoryName”, SqlDbType.VarChar).Value = catName;

objCmd.Parameters.Add(”@CategoryDesc”, SqlDbType.VarChar).Value = catDesc;

objCmd.ExecuteNonQuery();

objConn.Close();

FormView1.ChangeMode(FormViewMode.ReadOnly);

GetCategories();

}

//this commandName from EditItemTemplate and InsertItemTemplate

if (e.CommandName == “Cancel”)

{

FormView1.ChangeMode(FormViewMode.ReadOnly);

GetCategories();

}

}

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)

{

}

protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)

{

}

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)

{

//FormView1.ChangeMode(FormViewMode.ReadOnly);

}

Sep 23 2009

using details view in asp.net

Asp.net Details View control

DetailsView is an asp.net server control. It is a data-bound control that renders a single record at a time from its associated data source.

It can optionally provide paging buttons to navigate between records, and a command bar to execute basic operations on the current record (Insert, Update, and Delete).

DetailsView generates a user interface, and is typically used for updating/deleting any currently displayed record or for inserting new records.

How to use asp.net Details view control

Step1: Drag the Repeater control from Data controls in Toolbox

<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True”

Height=”50px” Width=”125px” >

</asp:DetailsView>

Allow paging property of DetailsView is True

You can change the autoformat of the DetailsView

asp.net detailsview

asp.net detailsview

Asp.net Detailsview control without using BoundFields or TemplateField for this keep AutoGenerateColumns property is TRUE

to view sqlserver category table click here

Step2: then write the code in  (Default.aspx.cs) to get the data into Detailsview

From datasource.

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetCategories();

}

}

#region “GetCategories()”

public void GetCategories()

{

string ConnectionString = “Data Source=192.168.1.23;Initial Catalog=Purchase;User;Password=abc;”;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select CategoryID,CategoryName,CategoryDesc,PostedDate from tblCategory”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();

SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

//objConn.Open();

objDa.Fill(objDs);

//objCmd.ExecuteNonQuery();

DetailsView1.DataSource = objDs;

DetailsView1.DataBind();

//objConn.Close();

}

#endregion

Result look like

asp.net detailsview format

asp.net detailsview format

DetailsView Paging:

To work with details view paging, go to properties of Detailsview control

Paging = true

Double click the event of  DetailsView of DetailsView1_PageIndexChanging

as show in below

<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True”

Height=”50px” Width=”300px” BackColor=”White” BorderColor=”#3366CC”

BorderStyle=”Solid” BorderWidth=”1px” CellPadding=”4″

onpageindexchanging=”DetailsView1_PageIndexChanging”>

protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)

{

DetailsView1.PageIndex = e.NewPageIndex;

GetCategories();

}

Then the run the application click paging in Detailsview

Using Detailview with BoundFields

Remember when using DetailsView with BoundFields then keep property of

AutoGenerateRows is False

<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True”

Height=”50px” Width=”300px” BackColor=”White” BorderColor=”#3366CC”

BorderStyle=”Solid” BorderWidth=”1px” CellPadding=”4″

AutoGenerateRows=”False”

onpageindexchanging=”DetailsView1_PageIndexChanging”>

<FooterStyle BackColor=”#99CCCC” ForeColor=”#003399″ />

<RowStyle BackColor=”White” ForeColor=”#003399″ />

<PagerStyle BackColor=”#99CCCC” ForeColor=”#003399″ HorizontalAlign=”Left” />

<Fields>

<asp:BoundField DataField=”CategoryName” HeaderText=”CategoryName” />

<asp:BoundField DataField=”CategoryDesc” HeaderText=”CategoryDesc” />

<asp:BoundField DataField=”PostedDate” HeaderText=”PostedDate” />

</Fields>

<HeaderStyle BackColor=”#003399″ Font-Bold=”True” ForeColor=”#CCCCFF” />

<EditRowStyle BackColor=”#009999″ Font-Bold=”True” ForeColor=”#CCFF99″ />

</asp:DetailsView>

Then write above PageLoad code and GetCategories function

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetCategories();

}

}

public void GetCategories()

{

//code is above

}

Now Run the application

Insert,Delete and Edit in asp.net DetailsView control

Change the properties of DetailsView to add the links of Insert,Delete and Edit

Goto DetailsView properties set the

AutoGenerateDelete = True

AutoGenerateEdit = True

AutoGenerateInsert = True

Source Code will look like

<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True”

Height=”50px” Width=”300px” AutoGenerateRows=”False” BackColor=”White”

BorderColor=”#3366CC” BorderStyle=”Solid” BorderWidth=”1px” CellPadding=”4″

onpageindexchanging=”DetailsView1_PageIndexChanging”

AutoGenerateDeleteButton=”True”

AutoGenerateEditButton=”True”

AutoGenerateInsertButton=”True”>

…………………

…………………

……………

DetailsView Events

Events of DetailsView: Double click DetailsView events of

onitemdeleting= for deleting record in DetailsView

onmodechanging= for edit,cancel and insert

For editing Cancel and insert write below code in onModeChanging

protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)

{

//string mode = DetailsViewMode.Edit.ToString();

//string add=DetailsViewMode.Insert.ToString();

//for Edit already existed category form

if (e.NewMode == DetailsViewMode.Edit)

{

DetailsView1.ChangeMode(DetailsViewMode.Edit);

GetCategories();

}

//For Canceling edit and update

if(e.CancelingEdit)

{

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);

GetCategories();

}

//For Insert new category form

if (e.NewMode==DetailsViewMode.Insert)

{

DetailsView1.ChangeMode(DetailsViewMode.Insert);

}

}

Adding Inserting Data into Database

public void InsertNewCategory()

{

TextBox catname = (TextBox)DetailsView1.Rows[0].Cells[1].Controls[0];

TextBox catdesc = (TextBox)DetailsView1.Rows[1].Cells[1].Controls[0];

string ConnectionString = “Data Source=192.168.1.58;Initial Catalog=Purchase;User;Password=lotus;”;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “insert into tblCategory(CategoryName,CategoryDesc) ”

+ “values(@CategoryName,@CategoryDesc)”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

objCmd.Parameters.Add(”@CategoryName”, SqlDbType.VarChar).Value = catname.Text;

objCmd.Parameters.Add(”@CategoryDesc”, SqlDbType.VarChar).Value = catdesc.Text;

objConn.Open();

objCmd.ExecuteNonQuery();

objConn.Close();

}

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)

{

InsertNewCategory();

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);

}

For Deleting

#region “Deleting”

protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)

{

int catid = int.Parse(DetailsView1.DataKey.Value.ToString());

string ConnectionString = “Data Source=192.168.1.23;Initial Catalog=Purchase;User;Password=abc;”;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “delete from tblCategory where CategoryID=” +catid;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

objConn.Open();

objCmd.ExecuteNonQuery();

objConn.Close();

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);

GetCategories();

}

#endregion

Alibi3col theme by Themocracy