2015-05-30 97 views
2

我想更新Gridview記錄。在下面給出的代碼中,我沒有在GridView1_RowUpdating更新事件中獲得「txtChangeQuantity」 文本框的值。它只顯示一個空字符串 。我在這裏錯過了什麼?無法獲得文本框值OnRowUpdating

TextBox tb = row.Cells[2].FindControl("txtChangeQuantity") as TextBox; 
       string a = tb.Text; 

一個是 「」

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" Width="450px" 
      DataKeyNames="ItemID" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"> 
      <AlternatingRowStyle BackColor="White" /> 
      <Columns> 
       <asp:BoundField DataField="ItemName" HeaderText="Name" ReadOnly="true" /> 
       <asp:BoundField DataField="Price" HeaderText="Unit Price" ReadOnly="true" /> 

       <asp:TemplateField> 
    <ItemTemplate> 
     <asp:Label ID="lbltxtChangeQuantity" runat="server" Text='<%# Eval("Quantity") %>' /> 
    </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtChangeQuantity" runat="server" Text='<%# Eval("Quantity") %>' /> 
        </EditItemTemplate> 
</asp:TemplateField> 
       <asp:TemplateField> 
    <ItemTemplate> 
     <asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:c}", ((double)Eval("Price"))*((int)Eval("Quantity"))) %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 
       <asp:CommandField ShowEditButton="true" /> 


       <asp:TemplateField> 
         <ItemTemplate> 
          <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" >Delete</asp:LinkButton> 
         </ItemTemplate> 
       </asp:TemplateField> 


      </Columns> 
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
      <SortedAscendingCellStyle BackColor="#FDF5AC" /> 
      <SortedAscendingHeaderStyle BackColor="#4D0000" /> 
      <SortedDescendingCellStyle BackColor="#FCF6C0" /> 
      <SortedDescendingHeaderStyle BackColor="#820000" /> 
     </asp:GridView> 


protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
       BindGrid(); 
      } 
     } 

     private void BindGrid() 
     { 

      if (Session["CartItems"] != null) 
      { 
       List<ItemModel> itemList = (List<ItemModel>)Session["CartItems"]; 
       GridView1.DataSource = itemList; 
       GridView1.DataBind(); 
      } 

     } 



     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
     { 
      GridView1.EditIndex = e.NewEditIndex; 
      BindGrid(); 
     } 

     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     { 
      RepItem = new ItemRepository(); 
      string val = e.Keys["ItemID"].ToString(); 
      if (Session["CartItems"] != null) 
      { 

       GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 
       List<ItemModel> itemList = (List<ItemModel>)Session["CartItems"]; 

       TextBox tb = row.Cells[2].FindControl("txtChangeQuantity") as TextBox; 
       string a = tb.Text; 

       GridView1.DataSource = itemList; 
       GridView1.DataBind(); 
      } 
     } 
+0

改變文本框的ID txtChangeQuantity。 –

回答

1

試試這個.....

string test = (GridView1.Rows[row].FindControl("txtChangeQuantity") as TextBox).Text 
2

使用這種findcontrol代碼GridView控件。

string tb = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtChangeQuantity")).Text 

你的問題是文本ID lbltxtChangeQuantity,並改變它txtChangeQuantity

+0

仍顯示一個空值。 – Upendra