Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!!

i have a text box inside a grid-view. what i have done is that a user can change the value inside the text box and then click on update button to do see the changes in the subsequent columns. The problem that i am facing is that my code is retrieving the default value not the value set my the user. For instance if i change the quantity text box value to 3 from 1 , it still is retrieving the value 1 from the text box on update button click.I applied breakpoints and found out that value retrieved from the text box is not the new value which i entered.


here is the code for button click event

C#
protected void btnUpdateCart_Click(object sender, EventArgs e)
  {
      foreach (GridViewRow row in GridView1.Rows)
      {
          if (row.RowType == DataControlRowType.DataRow)
          {
              // We'll use a try catch block in case something other than a number is typed in
              // If so, we'll just ignore it.
              try
              {
                  // Get the productId from the GridView's datakeys
                  string  productId =(GridView1.DataKeys[row.RowIndex].Value).ToString();
                  // Find the quantity TextBox and retrieve the value
                  int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
                  ShoppingCart.Instance.SetItemQuantity(productId, quantity);
              }
              catch (FormatException) { }
          }
      }

      BindData();
  }



here is the source of the gridview

XML
<asp:GridView ID="GridView1" runat="server" DataKeyNames="pid" Width="100%" AutoGenerateColumns="False"
              BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
              CellPadding="4" CellSpacing="2" ForeColor="Black"
              onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound" ShowFooter="true" EmptyDataText="There is no object in your cart">
              <RowStyle BackColor="White" />
              <Columns>
                  <asp:BoundField DataField="Description" HeaderText="Description" />
                  <asp:TemplateField HeaderText="Quantity">
                      <ItemTemplate>
                          <asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
                          <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove"
                              CommandArgument='<%# Eval("pid") %>' Style="font-size: 12px;"></asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right"
                      HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}">
                      <HeaderStyle HorizontalAlign="Right" />
                      <ItemStyle HorizontalAlign="Right" />
                  </asp:BoundField>
                  <asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right"
                      HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}">
                      <HeaderStyle HorizontalAlign="Right" />
                      <ItemStyle HorizontalAlign="Right" />
                  </asp:BoundField>
              </Columns>
              <FooterStyle BackColor="#CCCCCC" />
              <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
              <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
              <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
          </asp:GridView>
Posted

 
Share this answer
 
Comments
ujjwal uniyal 11-Jan-12 5:39am    
Abhinav i am able to find the control inside gridview. that is not the problem. the problem is that it's not retrieving the changed value , instead it's retrieving the previous value.
if you fill the grid in page_load event

you should use IsPostBack to determine that page is new loaded or post back

use:

C#
if (!IsPostBack)
     FillGrid();
 
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