Edit ,Update , Delete in Repeater in asp.net
<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
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();
}
}



Please tell me How to Popup Delete Confirmation?
Manish
August 8, 2009
When i build this demo, i get error at row int amt = Convert.ToInt32(((TextBox)e.Item.FindControl(“txtamt”)).Text);
Can you help me?
snow
September 29, 2009