Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Textbox Losing its Value after Postback on Repeater_ItemCommand.

there is a Linkbutton and a textbox Inside Repeater's ItemTemplate I want to Get the Value of TextBox on Linkbutton Click but it is Showing Empty when im debugging it.

Here's Code-
C#
protected void Page_Load(object sender, EventArgs e)
   {

       if (!Page.IsPostBack)
       {
           if (Session["MyCart"] != null)
           {
               Cart.DataSource = ((DataTable)(Session["MyCart"])).DefaultView;
               Cart.DataBind();
               Cart.Items[0].Visible = false;
           }
           else
           {
               Response.Redirect("Products.aspx");
           }
       }

   }
   protected void Cart_ItemCommand(object source, RepeaterCommandEventArgs e)
   {
       if (e.CommandName == "UpdateCart")
       {
           string Pid = e.CommandArgument.ToString();
           TextBox txtquantity = (TextBox)e.Item.FindControl("txtQuantity");

           DataTable dt = new DataTable();
           dt = (DataTable)(Session["MyCart"]);
           //// Get all DataRows where the name is the name you want.
           IEnumerable<DataRow> rows = dt.Rows.Cast<DataRow>().Where(r => r["Pid"].ToString() == Pid);
           //// Loop through the rows and change the name.
           rows.ToList().ForEach(r => r.SetField("Quantity", int.Parse(txtquantity.Text)));
           Session["MyCart"] = dt;
           Cart.DataSource = ((DataTable)(Session["MyCart"])).DefaultView;
           Cart.DataBind();
           Cart.Items[0].Visible = false;
       }

   }
Posted

1 solution

You are binding your repeater "cart" in "Cart_ItemCommand" event. It cause to recreate the item template. you can bind value back to Textbox in itemdatabound event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900