Edit ,Update , Delete in Repeater in asp.net

Posted on May 12, 2008. Filed under: Repeater |

<script>
function isconfirm()
{
var ck=confirm(‘Are you sure to delete’);
if(ck==true)
return true;
else
return false;
}
</script>
< asp:Repeater ID=”Rp” OnItemCommand=”Action” runat=”server”>

<ItemTemplate>

<div>

<asp:Label ID=”sno” Text=’<%#Eval(“sno”) %> runat=server Visible=false></asp:Label>

<asp:Label ID=”lblname” Text=’<%#Eval(“name1″) %> runat=server></asp:Label><br />

<asp:Label ID=”lblamt” Text=’ <%#Eval(“amt”) %> runat=server></asp:Label>

<asp:TextBox ID=”txtname” Text=’<%#Eval(“name1″) %> runat=server Visible=false></asp:TextBox><br />

<asp:TextBox ID=”txtamt” Text=’<%#Eval(“amt”) %> runat=server Visible=false></asp:TextBox>

<asp:Button ID=”btn” runat=server Text=”Edit” CommandArgument=”ek” CommandName=”Action” />

<asp:Button ID=”btnd” runat=server Text=”Delete” CommandArgument=”dk” CommandName=”Action” OnClientClick=”return isconfirm()” />

</div>

</ItemTemplate>

<SeparatorTemplate><hr/></SeparatorTemplate>

</asp:Repeater>

In codebehind code

public

partial class imageclick : System.Web.UI.Page

{SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

getdata();

}

}

void getdata()

{

SqlDataAdapter sda = new SqlDataAdapter(“select * from emp”, con);

DataSet ds = new DataSet();

sda.Fill(ds);

Rp.DataSource = ds;

Rp.DataBind();

}

protected void Action(object o, RepeaterCommandEventArgs e)

{

int i = Rp.Items.Count;

int n = e.Item.ItemIndex;

for (int chk=0; chk< i; chk++)

{

if (chk != n)

{

((Button)Rp.Items[chk].FindControl(“btn”)).Enabled = false;

((Button)Rp.Items[chk].FindControl(“btnd”)).Enabled = false;

}

}

if (Convert.ToString(e.CommandArgument) == “ek”)

{

if (((Button)e.Item.FindControl(“btn”)).Text == “Edit”)

{

((Label)e.Item.FindControl(“lblname”)).Visible = false;

((Label)e.Item.FindControl(“lblamt”)).Visible = false;

((TextBox)e.Item.FindControl(“txtname”)).Visible = true;

((TextBox)e.Item.FindControl(“txtamt”)).Visible = true;

((Button)e.Item.FindControl(“btnd”)).Text = “Cencel”;

((Button)e.Item.FindControl(“btn”)).Text = “Update”;

// ((Button)e.Item.FindControl(“btn”)).CommandName = “Update”;

}

else

{

string nm = ((TextBox)e.Item.FindControl(“txtname”)).Text;

int amt = Convert.ToInt32(((TextBox)e.Item.FindControl(“txtamt”)).Text);

int sno = Convert.ToInt32(((Label)e.Item.FindControl(“sno”)).Text);

update(sno, nm, amt);

getdata();

((Label)e.Item.FindControl(“lblname”)).Visible = true;

((Label)e.Item.FindControl(“lblamt”)).Visible = true;

((TextBox)e.Item.FindControl(“txtname”)).Visible = false;

((TextBox)e.Item.FindControl(“txtamt”)).Visible = false;

((Button)e.Item.FindControl(“btn”)).Text = “Edit”;

}

}

else

{

if (((Button)e.Item.FindControl(“btnd”)).Text == “Delete”)

{

int sno = Convert.ToInt32(((Label)e.Item.FindControl(“sno”)).Text);

delete(sno);

getdata();

}

else

{

getdata();

}

}

}

void delete(int sno)

{

SqlCommand cmd = new SqlCommand(“delete from emp where sno=@sno”, con);

cmd.Connection.Open();

cmd.Parameters.Add(“@sno”, SqlDbType.Int).Value = sno;

cmd.ExecuteNonQuery();

cmd.Connection.Close();

}

void update( int sno,string nm,int amt)

{

SqlCommand cmd = new SqlCommand(“update emp set name1=@nm,amt=@am where sno=@sno”, con);

cmd.Connection.Open();

cmd.Parameters.Add(“@sno”, SqlDbType.Int).Value=sno;

cmd.Parameters.Add(“@nm”, SqlDbType.VarChar).Value = nm;

cmd.Parameters.Add(“@am”, SqlDbType.Int).Value = amt;

cmd.ExecuteNonQuery();

cmd.Connection.Close();

}

}

Make a Comment

Make a Comment: ( 2 so far )

blockquote and a tags work here.

2 Responses to “Edit ,Update , Delete in Repeater in asp.net”

RSS Feed for Dream Asp.net Comments RSS Feed

Please tell me How to Popup Delete Confirmation?

When i build this demo, i get error at row int amt = Convert.ToInt32(((TextBox)e.Item.FindControl(“txtamt”)).Text);
Can you help me?


Where's The Comment Form?

    About

    Hi,I am Suresh Sharma from Gorakhpur , UP, India.I have work on .net technology from 2 years. Key skills-Asp.net 2.0,3.5, Sql 2005,2000. Ajax.net 1.1,C#.net

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS
    • Subscribe in Rojo

    Meta

Liked it here?
Why not try sites on the blogroll...