2012-04-11 9 views
1

私はワンクリックで更新したいObjectDataSourceを持つgridviewを持っています。 したがって、グリッドビューは編集可能なフィールドと固定フィールドで読み込まれます。 グリッドビューをループして、一度にすべてを更新します。 固定フィールド(Cells [0])の値をProductsオブジェクト(p.ProductID)にロードできますが、編集可能なフィールドから値を読み込めません。Cells [1 ])をProductsオブジェクトに追加します(p.Productname)。Gridviewで編集可能なフィールドの値を取得する方法

オブジェクトでこの値を取得すると、オブジェクトをBLLメソッドに送信できます。 どうすればrow.Cells [0] .Textは固定フィールドでは動作しますが、編集可能フィールドでは動作しませんか?

protected void Update_Click(object sender, EventArgs e) 
    { 
     BLLProducts BLLProducts = new BLLProducts(); 
     Products p = new Products(); 
     foreach(GridViewRow row in GridView1.Rows) 
     { 
      p.ProductID = Convert.ToInt16(row.Cells[0].Text); 
     // p.Productname = row.Cells[1].Text; 

      BLLProducts.update(p); 
     } 
    } 

マークアップ:あなたが使用している編集可能なフィールド

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataSourceID="test"> 
      <Columns> 
       <asp:BoundField DataField="ProductID" 
        HeaderText="ProductID" SortExpression="ProductID" /> 
       <asp:TemplateField HeaderText="Productname" SortExpression="Productname"> 
        <EditItemTemplate> 
         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField>    
      </Columns> 
     </asp:GridView> 
     <asp:ObjectDataSource ID="test" runat="server" 
      SelectMethod="SelectAllProducts" TypeName="BLLProducts"> 
     </asp:ObjectDataSource> 
     <asp:Button ID="Button3" runat="server" onclick="Update_Click" 
      Text="Update All" /> 
    </p> 
</asp:Content> 
+0

それはあなたがあなたのマークアップの関連部分が含まれている場合、あなたの質問に答えるためにしようと人々に非常に有用であろう:あなたは、あなたがこのような値を取得する必要がありますテキストボックスを使用している場合

。 – jadarnel27

答えて

0

 foreach (GridViewRow gvr in GridView2.Rows) 
     { 
      TextBox tb = (TextBox)gvr.FindControl("TextBox1"); 
      string txt = tb.Text;    
     } 
+0

ありがとうございました! –

関連する問題