4
Answers

how do i undo my deleted record in Listview??

Sreenish

Sreenish

9y
357
1
Hello,
 
 
I have listview in that i have to udno my deleted record ..anyone helpme..
 
 
Source Code::
 
<div id="Main" style=" height:100%;width:100%">

<div id="Sub1" style=" height:12%;width:100%">
<div style="float:left;height:100%;width:12%; border:1px solid black; text-align:center">
S.No
<asp:TextBox ID="TextBox1" runat="server" BorderColor="Black" Width="97%" style="text-align:center" ReadOnly="true"></asp:TextBox>
</div>

<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center">
Name
<asp:TextBox ID="TextBox2" runat="server" BorderColor="Black" Width="97%" MaxLength="25" ontextchanged="txtName_TextChanged"></asp:TextBox>

</div>

<div style="float:left;height:100%;width:12%;border:1px solid black;text-align:center">
Designation
<asp:TextBox ID="TextBox3" runat="server" BorderColor="Black" Width="97%"
MaxLength="20" ></asp:TextBox>

</div>

<div style="float:left;height:100%;width:5%;border:1px solid black;text-align:center">
<br />
<asp:Button ID="Add" runat="server" Text="Add" Width="97%" onclick="btnadd_Click" />
</div>

<div style="float:left;height:100%;width:5%;border:1px solid black;text-align:center">
<br />
<asp:Button ID="Cancel" runat="server" Text="Cancel" Width="97%" onclick="btncan_Click"/>
</div>

<div style="float:left;height:100%;width:54%"></div>
</div>

<asp:ListView ID="ListView1" runat="server"
onitemcanceling="ListView1_ItemCanceling" onitemediting="ListView1_ItemEditing" onitemdatabound="ListView1_ItemDataBound"
onitemupdating="ListView1_ItemUpdating"
onitemdeleting="ListView1_ItemDeleting" ItemPlaceholderID="itemplaceholder"
GroupPlaceholderID="groupplaceholder" onitemcommand="ListView1_ItemCommand" >

<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupplaceholder" runat="server"/>
</div>
</LayoutTemplate>

<GroupTemplate>
<div>
<asp:PlaceHolder ID="itemplaceholder" runat="server"/>
</div>
</GroupTemplate>

<ItemTemplate>
<div id="Sub2" style="height:100%;width:100%">

<div style="float:left;height:100%;width:12%; border:1px solid black; text-align:center">
<asp:Label ID="Sno" runat="server" Text=' <%# Eval("SNo")%>' Width="3%" Height="22px"></asp:Label>
</div>

<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center" >
<asp:Label ID="Name" runat="server" Text='<%#Eval("Name")%>' Width="165px" Height="22px"></asp:Label>
</div>

<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center">
<asp:Label ID="Designation" runat="server" Text='<%#Eval("Designation")%>' Width="165px" Height="22px"></asp:Label>
</div>

<div style="float:left;height:100%;width:5%;border:1px solid black; text-align:center">
<asp:Button ID="Edit" runat="server" CommandName="Edit" Text="Edit" Width="97%" />
</div>

<div style="float:left;height:100%;width:5%;border:1px solid black; text-align:center">
<asp:Button ID="Delete" runat="server" CommandName="Delete" Width="97%" OnClientClick="return confirm('Are you sure you want to delete?');" Text="Delete"/>
<asp:Button ID="Undo" runat="server" CommandName="Undo" Visible="false" Text="Undo" OnClientClick="return confirm('Are you sure you want to Undo?');" />
</div>

<div style="float:left;height:100%;width:64%;">
<asp:Label runat="server" ID="lblstatus" Text='<%#Eval("Status")%>' Visible="false" Width="250px"></asp:Label>
</div>

</div>
</ItemTemplate>

</asp:ListView>
</div>
 
 
 
Code Behind::
  protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();

dt.Columns.Add("Sno");
dt.Columns.Add("Name");
dt.Columns.Add("Designation");
dt.Columns.Add("Status");

ViewState["List"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();

TextBox1.Text = (dt.Rows.Count + 1).ToString();
TextBox2.Focus();
}

}

protected void btnadd_Click(object sender, EventArgs e)
{
bool IsRecordExist = false;
DataTable dt = new DataTable();
dt = (DataTable)ViewState["List"];

DataRow dr = dt.NewRow();
IsRecordExist = CheckRecorExist();

if (IsRecordExist == false)
{
dr["Sno"] = (dt.Rows.Count + 1).ToString();
dr["Name"] = TextBox2.Text;
dr["Designation"] = TextBox3.Text;
dr["Status"] = "Saved";
dt.Rows.Add(dr);

ViewState["List"] = ListView1.DataSource = dt;
ListView1.DataBind();
RefreshHeader();
}

else
{
dt = (DataTable)ViewState["List"];
DataRow drUpdate = dt.Select("SNo=" + TextBox1.Text).FirstOrDefault();
drUpdate["Name"] = TextBox2.Text;
drUpdate["Designation"] = TextBox3.Text;
drUpdate["Status"] = ((Label)ListView1.Items[0].FindControl("lblstatus")).Text = "Modified";
dr["Status"] = "Modified";

ViewState["List"] = ListView1.DataSource = dt;
ListView1.DataBind();
RefreshHeader();
}
}


private bool CheckRecorExist()
{
bool IsExistRecord = false;

foreach (ListViewItem item in ListView1.Items)
{
if (((Label)ListView1.Items[item.DataItemIndex].FindControl("Sno")).Text == TextBox1.Text)
{
IsExistRecord = true;
break;
}
}
return IsExistRecord;
}

private void RefreshHeader()
{
DataTable dt = new DataTable();
dt = (DataTable)ViewState["List"];

int Row = dt.Rows.Count + 1;
TextBox1.Text = Row.ToString();
TextBox2.Text = "";
TextBox3.Text = "";

TextBox2.Focus();
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
DataTable dt = (DataTable)ViewState["List"];

string EditNo = ((Label)ListView1.Items[e.NewEditIndex].FindControl("SNo")).Text;
((Label)ListView1.Items[e.NewEditIndex].FindControl("lblstatus")).Text = "Saved";

DataRow dr = dt.Select("SNo=" + EditNo).FirstOrDefault();
dr["Status"] = ((Label)ListView1.Items[0].FindControl("lblstatus")).Text;
dr["Status"] = "Saved";

ViewState["List"] = ListView1.DataSource = dt;

TextBox1.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("SNo")).Text;
TextBox2.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("Name")).Text;
TextBox3.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("Designation")).Text;
TextBox2.Focus();
}

protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["List"];

ViewState["List"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();
}

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
TextBox TextBox1 = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox1"));
TextBox TextBox2 = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox2"));
TextBox TextBox3 = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox3"));

ListView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["List"];

ViewState["List"] = dt;
ListView1.DataSource = dt;

for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[e.ItemIndex]["SNo"] = TextBox1.Text;
dt.Rows[e.ItemIndex]["Name"] = TextBox2.Text;
dt.Rows[e.ItemIndex]["Designation"] = TextBox3.Text;
}

ListView1.DataBind();
}

protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{

((Label)ListView1.Items[e.ItemIndex].FindControl("lblstatus")).Text = "Deleted";
ListViewItem item = this.ListView1.Items[e.ItemIndex];
string cusid = ((Label)ListView1.Items[e.ItemIndex].FindControl("SNo")).Text;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('S.No: " + cusid + " Will be Deleted')", true);

DataTable dt = (DataTable)ViewState["List"];
DataRow dr = dt.Select("SNo=" + cusid).FirstOrDefault();
dr["Status"] = ((Label)ListView1.Items[e.ItemIndex].FindControl("lblstatus")).Text;
dr["Status"] = "Deleted";

ViewState["List"] = ListView1.DataSource = dt;

ListView1.DataBind();
RefreshHeader();
TextBox2.Focus();
}

protected void btncan_Click(object sender, EventArgs e)
{
TextBox2.Text = "";
TextBox3.Text = "";
}

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataTable dt = (DataTable)ViewState["List"];
DataRow dr = ((System.Data.DataRowView)e.Item.DataItem).Row;

switch (dr["Status"].ToString())
{
case "Deleted":
{

Add.Visible = true;
Cancel.Visible = true;

((Button)e.Item.FindControl("Edit")).Visible = false;
((Button)e.Item.FindControl("Delete")).Visible = false;
((Button)e.Item.FindControl("Undo")).Visible = true;
((Label)e.Item.FindControl("SNo")).BackColor = Color.Red;
((Label)e.Item.FindControl("Name")).BackColor = Color.Red;
((Label)e.Item.FindControl("Designation")).BackColor = Color.Red;


break;
}


case "Saved":
{

Add.Visible = true;
Cancel.Visible = true;

((Button)e.Item.FindControl("Edit")).Visible = true;
((Button)e.Item.FindControl("Delete")).Visible = true;
break;
}

case "Modified":
{

Add.Visible = true;
Cancel.Visible = true;

((Button)e.Item.FindControl("Edit")).Visible = true;
((Button)e.Item.FindControl("Delete")).Visible = true;
break;
}

default:
{
break;
}
}
}
}
 
 
Answers (4)