ワンクリックで更新したいObjectDataSourceのグリッドビューがあります。したがって、gridviewは編集可能で固定されたフィールドでロードされます。グリッドビューをループして、すべてを一度に更新します。固定フィールド(Cells [0])からProductsオブジェクト(p.ProductID)に値をロードできますが、編集可能フィールド(Cells [1])からProductsオブジェクトに値をロードできません。 (p.Productname)。
オブジェクトを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>