Hi chetan,
Refer below sample.
HTML
<asp:ListView ID="ListView1" runat="server" AllowSorting="true" DataKeyNames="ID"
OnPagePropertiesChanging="news_PagePropertiesChanging" OnItemEditing="news_EditListViewItem"
OnItemCanceling="news_CancelListViewItem" OnItemUpdating="OnItemUpdating">
<LayoutTemplate>
<table>
<tr class="header">
<th>
</th>
<th>
News ID
</th>
<th>
News Title
</th>
<th>
News Description
</th>
<th>
News
</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="alt2">
<td class="command">
<asp:LinkButton ID="btnEdit" runat="server" Text="View Details" CommandName="Edit"
CausesValidation="False" />
</td>
<td>
<%# Eval("ID") ?? " "%>
</td>
<td>
<%# Eval("title") ?? " "%>
</td>
<td>
<%# Eval("newsdescription") ?? " "%>
</td>
<td>
<%# Eval("news") ?? " "%>
</td>
<td>
<asp:Button ID="btDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('This will permanently delete this customer
and all of their orders. Are you sure you want to do this!?!?');" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<td class="command">
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</td>
<div class="header">
View Details for '<%# Eval("ID")%>'</div>
</tr>
<tr>
<td class="edit" colspan="6">
<div class="details">
<table>
<tr>
<td align="right">
News tittle :
</td>
<td>
<asp:TextBox ID="txtnewstitle" runat="server" Text='<%# Eval("title").ToString() %>'></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* News Title Required"
ControlToValidate="txtnewstitle"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" valign="top">
News Description :
</td>
<td>
<asp:TextBox ID="txtdescription" runat="server" Text='<%# HttpUtility.HtmlDecode(Eval("newsdescription").ToString()) %>'
TextMode="MultiLine"></asp:TextBox>
</td>
<td valign="top">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="* Description Required"
ControlToValidate="txtdescription"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" valign="top">
News Keywords :
</td>
<td>
<asp:TextBox ID="txtkeywords" runat="server" Text='<%# HttpUtility.HtmlDecode(Eval("newskeywords").ToString()) %>'
TextMode="MultiLine"></asp:TextBox>
</td>
<td valign="top">
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="* Keywords Required"
ControlToValidate="txtkeywords"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" valign="top">
News Details :
</td>
<td>
<asp:TextBox ID="txtnewsbrief" runat="server" Text='<%# HttpUtility.HtmlDecode(Eval("news").ToString()) %>'
TextMode="MultiLine"></asp:TextBox>
</td>
<td valign="top">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* News in Brief Required"
ControlToValidate="txtnewsbrief"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
<div class="footer command">
<asp:LinkButton ID="btnSave" runat="server" Text="Save" CommandName="Update" />
<asp:LinkButton ID="LinkButton1" runat="server" Text="Cancel" CommandName="Cancel"
CausesValidation="False" />
</div>
</div>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<div style="text-align: center; font-weight: bold;">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="1">
<Fields>
<asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="«" PreviousPageText="‹"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowPreviousPageButton="true"
ShowLastPageButton="false" ShowNextPageButton="false" />
<asp:NumericPagerField ButtonCount="7" NumericButtonCssClass="command" CurrentPageLabelCssClass="current"
NextPreviousButtonCssClass="command" />
<asp:NextPreviousPagerField ButtonCssClass="command" LastPageText="»" NextPageText="›"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowPreviousPageButton="false"
ShowLastPageButton="true" ShowNextPageButton="true" />
</Fields>
</asp:DataPager>
</div>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private DataTable NewsUpdate
{
get { return ViewState["NewsUpdate"] != null ? (DataTable)ViewState["NewsUpdate"] : null; }
set { ViewState["NewsUpdate"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindListView();
}
}
private void BindListView()
{
DataTable dt = NewsUpdate;
if (dt == null)
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID", typeof(int)),
new DataColumn("title", typeof(string)),
new DataColumn("newsdescription", typeof(string)),
new DataColumn("newskeywords", typeof(string)),
new DataColumn("news", typeof(string)) });
dt.Rows.Add(1, "A", "alphabets", "a", "Apple");
dt.Rows.Add(2, "B", "alphabets", "b", "Ball");
dt.Rows.Add(3, "C", "alphabets", "c", "Cat");
NewsUpdate = dt;
}
this.ListView1.DataSource = dt;
this.ListView1.DataBind();
}
protected void news_CancelListViewItem(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -1;
BindListView();
}
protected void news_EditListViewItem(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
BindListView();
}
protected void OnItemUpdating(object sender, ListViewUpdateEventArgs e)
{
ListViewItem item = ListView1.Items[e.ItemIndex];
int id = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
string txtnewstitle = (ListView1.Items[e.ItemIndex].FindControl("txtnewstitle") as TextBox).Text;
string txtdescription = (ListView1.Items[e.ItemIndex].FindControl("txtdescription") as TextBox).Text;
string txtkeywords = (ListView1.Items[e.ItemIndex].FindControl("txtkeywords") as TextBox).Text;
string txtnewsbrief = (ListView1.Items[e.ItemIndex].FindControl("txtnewsbrief") as TextBox).Text;
foreach (DataRow row in NewsUpdate.Rows)
{
if (row["ID"].ToString() == id.ToString())
{
row["newsdescription"] = txtdescription;
row["title"] = txtnewstitle;
row["news"] = txtnewsbrief;
break;
}
}
ListView1.EditIndex = -1;
BindListView();
}
protected void news_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindListView();
}
VB.Net
Private Property NewsUpdate As DataTable
Get
Return If(ViewState("NewsUpdate") IsNot Nothing, CType(ViewState("NewsUpdate"), DataTable), Nothing)
End Get
Set(ByVal value As DataTable)
ViewState("NewsUpdate") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindListView()
End If
End Sub
Private Sub BindListView()
Dim dt As DataTable = NewsUpdate
If dt Is Nothing Then
dt = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ID", GetType(Integer)), New DataColumn("title", GetType(String)), New DataColumn("newsdescription", GetType(String)), New DataColumn("newskeywords", GetType(String)), New DataColumn("news", GetType(String))})
dt.Rows.Add(1, "A", "alphabets", "a", "Apple")
dt.Rows.Add(2, "B", "alphabets", "b", "Ball")
dt.Rows.Add(3, "C", "alphabets", "c", "Cat")
NewsUpdate = dt
End If
Me.ListView1.DataSource = dt
Me.ListView1.DataBind()
End Sub
Protected Sub news_CancelListViewItem(ByVal sender As Object, ByVal e As ListViewCancelEventArgs)
ListView1.EditIndex = -1
BindListView()
End Sub
Protected Sub news_EditListViewItem(ByVal sender As Object, ByVal e As ListViewEditEventArgs)
ListView1.EditIndex = e.NewEditIndex
BindListView()
End Sub
Protected Sub OnItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
Dim item As ListViewItem = ListView1.Items(e.ItemIndex)
Dim id As Integer = Integer.Parse(ListView1.DataKeys(e.ItemIndex).Value.ToString())
Dim txtnewstitle As String = (TryCast(ListView1.Items(e.ItemIndex).FindControl("txtnewstitle"), TextBox)).Text
Dim txtdescription As String = (TryCast(ListView1.Items(e.ItemIndex).FindControl("txtdescription"), TextBox)).Text
Dim txtkeywords As String = (TryCast(ListView1.Items(e.ItemIndex).FindControl("txtkeywords"), TextBox)).Text
Dim txtnewsbrief As String = (TryCast(ListView1.Items(e.ItemIndex).FindControl("txtnewsbrief"), TextBox)).Text
For Each row As DataRow In NewsUpdate.Rows
If row("ID").ToString() = id.ToString() Then
row("newsdescription") = txtdescription
row("title") = txtnewstitle
row("news") = txtnewsbrief
Exit For
End If
Next
ListView1.EditIndex = -1
BindListView()
End Sub
Protected Sub news_PagePropertiesChanging(ByVal sender As Object, ByVal e As PagePropertiesChangingEventArgs)
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
BindListView()
End Sub
Screenshot