如何在网格视图中编辑和更新行值?

时间:2011-02-15 06:21:10

标签: c# asp.net

我有这样的网格视图:

<asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False"
                OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label ID="lblPID" runat="server" Text="Product ID"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

,这是页面背后的代码

    protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvProducts.EditIndex = e.NewEditIndex;
    }

    protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        int i = e.RowIndex;
        object control = gvProducts.Rows[i].FindControl("txtProdID");
        //i want to access the new value from the object "control" but i m getting the previous value only
    }

2 个答案:

答案 0 :(得分:2)

试试这个

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ 
TextBox txtProdID = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdID");
TextBox txtProdName = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdName");


//Call update method
Product.Update(txtProdId,txtProdName);

gvProducts.EditIndex = -1;
//Refresh the gridviwe
BindGrid(); 
}

答案 1 :(得分:0)

您需要查看e.NewValues字典以获取更新的数据。

我在下面有一个示例,GridView模板绑定到CategoryName。 单击“编辑”按钮时会触发OnRowUpdating。 在RowUpdating事件处理程序中,它获取文本框绑定到的CategoryName数据。

注意:不要在正常模式下使用TextBox,而是使用标签。

<asp:GridView ID='GridView1' runat='server' DataKeyNames='CategoryID' OnRowUpdating='HandleOnGridViewRowUpdating'
        DataSourceID='ObjectDataSource1' AutoGenerateColumns='false'>
        <Columns>
            <asp:CommandField ShowEditButton="true" />
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID='CategoryName' Text='Category' runat='server'></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID='CategoryNameTextbox' Text='<%# Bind("CategoryName") %>' runat='server'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

代码隐藏:

public void HandleOnGridViewRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.NewValues["CategoryName"] != null)
        {
            String newCategoryName = e.NewValues["CategoryName"].ToString();
            // process the data; 
        }
    }