Gridview rowupdating不会获取新值

时间:2011-08-10 15:56:59

标签: asp.net gridview

gridview的代码:

<asp:GridView ID="uxItemsGV" runat="server" AllowPaging="True" CellPadding="4" 
                ForeColor="#333333" GridLines="None" PageSize="20" 
                AutoGenerateColumns="False" OnRowEditing="uxItemsGV_RowEditing"
                onpageindexchanging="uxItemsGV_PageIndexChanging" 
                onpageindexchanged="uxItemsGV_PageIndexChanged" AutoGenerateEditButton="True"
                onrowupdated="uxItemsGV_RowUpdated" onrowupdating="uxItemsGV_RowUpdating" 
                onrowcancelingedit="uxItemsGV_RowCancelingEdit">
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:BoundField DataField="ItemID" HeaderText="Item ID" ReadOnly="true" />
                    <asp:BoundField DataField="ItemName" HeaderText="Title" />
                    <asp:BoundField DataField="QuantityOnHand" HeaderText="Quantity" />
                    <asp:BoundField DataField="Totalsold" HeaderText="Total Sold" />
                    <asp:BoundField DataField="RetailPrice" HeaderText="SR Price" />
                    <asp:BoundField DataField="Dateofadd" HeaderText="Added Date" ReadOnly="true" />
                </Columns>
                <PagerSettings Position="TopAndBottom" FirstPageText="First" 
                    LastPageText="Last" Mode="NextPreviousFirstLast" NextPageText="&gt;&gt;" 
                    PreviousPageText="&lt;&lt;" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

背后的代码:

protected void uxItemsGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = uxItemsGV.Rows[e.RowIndex];
        try
        {
            BVController.Updateitem(int.Parse(row.Cells[1].Text), Session["CategoryID"].ToString(), ((TextBox)row.Cells[2].Controls[0]).Text.Trim(),
                int.Parse(((TextBox)row.Cells[3].Controls[0]).Text.Trim()), decimal.Parse(((TextBox)row.Cells[5].Controls[0]).Text.Trim()),
                int.Parse(((TextBox)row.Cells[4].Controls[0]).Text.Trim()));
            uxUserMessage.Text = "Item " + row.Cells[1].Text + " has been updated.";
        }
        catch (Exception ex)
        {
            uxUserMessage.Text = "The following error occured: " + ex.Message;
            MsgBlock.Visible = true;
        }
    }

当我点击更新链接时,代码表示更新已完成,但未进行任何更改。我设置了断点,发现代码只传递旧值来提交更新。没有任何新的价值。有什么建议吗?谢谢!

3 个答案:

答案 0 :(得分:0)

您需要定义编辑模板,例如:

<asp:GridView ID="grd" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%#Eval(<Column Name>)%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval(<Column Name>)%>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>                                                
    </Columns>
</asp:GridView>

完成此操作后,您需要更改OnRowUpdating事件以从模板化编辑控件中提取值。

答案 1 :(得分:0)

只需使用DataField(列名)访问新值,如下例所示:

int categoryID = Convert.ToInt32(e.NewValues["CategoryID"]);
string firstName = Convert.ToString(e.NewValues["FirstName"]);

答案 2 :(得分:0)

我遇到了同样的问题,这是由于在Page_Load中错误地调用this.BindGrid()引起的。