从gridview内的文本框中检索值时出现问题 [英] Problem in retreving the value from a textbox inside a gridview

查看:48
本文介绍了从gridview内的文本框中检索值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我在网格视图中有一个文本框.我所做的是,用户可以更改文本框中的值,然后单击更新"按钮以查看后续列中的更改.我面临的问题是我的代码正在检索默认值,而不是为用户设置的值.例如,如果我将数量文本框的值从1更改为3,则仍然在更新按钮单击时从文本框中检索值1.我应用了断点,发现从文本框中检索到的值不是新值我进入了.


这是按钮单击事件的代码

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

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




这是gridview的来源




here is the source of the gridview

<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>

推荐答案

CodeProject常见问题问题系列1:ASP.NET GridView [ ^ ]应该会帮助您.
CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] should help you out.


如果您在page_load事件中填充网格

您应该使用IsPostBack确定该页面是新加载的还是回发

用途:

if you fill the grid in page_load event

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

use:

if (!IsPostBack)
     FillGrid();


这篇关于从gridview内的文本框中检索值时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆