Here I would like to discuss about to insert,edit and delete data in ListView controls in
ASP.Net.
In previous about I discuss about grdiview control and
datalist controls design and the process of update, insert and delete
operation.
But Listview control started to use from the framework
3.5.It has some own advantage to compare with other data controls like insert
new record, delete and update the previous record process.
In design process we can customize listview control for our
own types,it has more advantage when compared with other controls.
Templates in ListView:
InsertItemTemplate:
Defines the presentation for a new item being inserted into the data set.
ItemSeparatorTemplate:
Defines the content to display between individual data items.
ItemTemplate:
Defines the data to display for single items.
LayoutTemplate:
Defines the main presentation features for the control. This root template
contains placeholders that are replaced by what is defined by ItemTemplate and
GroupTemplate.
SelectedItemTemplate:
Specifies the content to display when users select items.
AlternatingItemTemplate:
Defines how alternating items are displayed.
EditItemTemplate:
Defines the presentation for an item when it is edited.
EmptyDataTemplate:
Specifies the content to display for empty individual items.
EmptyItemTemplate:
Specifies the content to display for empty group items.
GroupSeparatorTemplate:
Specifies the content to display between groups.
GroupTemplate:
Defines the content for groups. This template is a placeholder for the data
displayed for individual group items (when data is grouped).
In sample code I have do the process of insert image and
data in to database using ListView controls in asp.net.
Design code:
Note:
Inside listview you have to add placeholder inside
layouttemplate for holding edititemtemlate and inseritemtemplate.
Also you have to mention insert item positon in the
listview.here I have mention insertitempostion as last .so the new item
template have displayed in the last postion.
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1" InsertItemPosition="LastItem"
OnItemCanceling="ListView1_ItemCanceling" OnItemDeleting="ListView1_ItemDeleting"
OnItemEditing="ListView1_ItemEditing" OnItemInserting="ListView1_ItemInserting"
DataKeyNames="Number" OnItemUpdating="ListView1_ItemUpdating">
<LayoutTemplate>
<table cellpadding="0" cellspacing="0" width="100%" class="ItemCSS">
<tr>
<td width="25%" style="border-bottom: 1px solid #A13EA1;">
Number
</td>
<td width="25%" style="border-bottom: 1px solid #A13EA1;">
Name
</td>
<td width="25%" style="border-bottom: 1px solid #A13EA1;">
Address
</td>
<td width="25%" style="border-bottom: 1px solid #A13EA1;">
City
</td>
</tr>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td style="border-bottom: 2px dotted Gray;">
<%#Eval("Number")%>
</td>
<td style="border-bottom: 2px dotted Gray;">
<%#Eval("Name")%>
</td>
<td style="border-bottom: 2px dotted Gray;">
<%#Eval("Address")%>
</td>
<td style="border-bottom: 2px dotted Gray;">
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
<span onclick="return confirm('Are you sure to delete?')">
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
ForeColor="Brown" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td height="50">
<asp:TextBox ID="TextBox3" runat="server" Text=' <%#Eval("Number")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text=' <%#Eval("Name")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("Address")%>'></asp:TextBox>
</td>
<td>
<span onclick="return confirm('Are you sure to update?')">
<asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
</span>
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr>
<td width="25%" height="50">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td width="25%">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td width="25%">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td width="25%">
<span onclick="return confirm('Are you sure to update?')">
<asp:LinkButton ID="btnUpdate" runat="server" Text="Insert" CommandName="Insert" />
</span>
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"
ForeColor="Brown" />
</td>
</tr>
</InsertItemTemplate>
<EmptyDataTemplate>
<hr />
No Records Found.<hr />
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert Records" CommandName="Insert" />
</EmptyDataTemplate>
</asp:ListView>
|
C# Code:
Note:
Initially you must have to assign your connection string to
run sqlcommand on the listview.
Here I have declared connection string on CONSTR variable.
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
loaddata();
}
}
void loaddata()
{
query = "select * from phone";
using (sqlcon = new SqlConnection(constr))
{
sqlcmd = new SqlCommand(query,
sqlcon);
sqlcon.Open();
sqlda = new SqlDataAdapter(sqlcmd);
DataSet
ds=new DataSet();
sqlda.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind();
}
}
protected void
ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
//when press
edit button on listview
ListView1.EditIndex = e.NewEditIndex;
loaddata();
}
protected void
ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
//when press
Cancel button on listview
ListView1.EditIndex = -1;
loaddata();
}
protected void
ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
TextBox
number,name,adress;
number =(TextBox)e.Item.FindControl("TextBox1");
name =(TextBox)e.Item.FindControl("TextBox4");
adress =(TextBox)e.Item.FindControl("TextBox5");
query = "insert into phone (Number,Name,Address) values('" + number.Text + "','" + name.Text + "','" + adress.Text + "')";
using
(sqlcon = new SqlConnection(constr))
{
sqlcmd = new SqlCommand(query,
sqlcon);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
loaddata();
}
}
protected void
ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
string id;
id =
ListView1.DataKeys[e.ItemIndex].Value.ToString();
query ="delete from phone where Number='"+ id +"'";
using (sqlcon = new SqlConnection(constr))
{
sqlcmd = new SqlCommand(query,
sqlcon);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
loaddata();
}
}
protected void
ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int id;
id = (int)ListView1.DataKeys[e.ItemIndex].Value;
TextBox number, name, adress;
number =(TextBox)
ListView1.Items[e.ItemIndex].FindControl("TextBox1");
name = (TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox1");
adress = (TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox1");
//Here you can
write the update qurey to update the value
}
|
I hope you like this article….
Post your valuable comments …
0 comments:
Post a Comment