1

ワンクリックで更新したい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>
4

1 に答える 1

0

どの編集可能なフィールドを使用していますか?

テキスト ボックスを使用した場合は、次のような値を取得する必要があります。

       foreach (GridViewRow gvr in GridView2.Rows)  
       {  
           TextBox tb = (TextBox)gvr.FindControl("TextBox1");  
           string txt = tb.Text;              
       }
于 2012-04-11T12:39:04.927 に答える