Monday, May 12, 2008

Edit ,Update , Delete in Repeater in asp.net

<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();

}

}

9 comments :

  1. Please tell me How to Popup Delete Confirmation?

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

    ReplyDelete
  3. Hi,
    i am sure this would be an easy code for professional to understand, but for me as a beginner i am having a very hard time understanding the material above, so could you please simplify it for the likes of me.
    Thanks

    ReplyDelete
  4. Thank you so much. Good job!

    ReplyDelete
  5. pusing bgt w g ngerti ASP n g da yg ngajarin lg

    ReplyDelete
  6. Hi,
    manish use javascript confirm method on OnClientClick of any asp.net server control to popup confirmation box on client side.

    ReplyDelete
  7. Is window application static or dynamic application

    ReplyDelete
  8. Hi your article is very self explanatory.Thanks for it.

    ReplyDelete
  9. This is great oneee ☺. simple onee... But it helped me alot... thankxxx

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath