如果textbox已经具有Eval值,我如何获得新的文本框值(我写的新文本)?

时间:2018-06-14 14:28:22

标签: asp.net textbox eval

<!-- This is my ascx code -->
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" OnRowUpdating="grid_RowUpdating">
    <Columns>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:TextBox ID="tb_Name" runat='server' Width="170px" Text='<%# Eval("Name") %>' />
            </ItemTemplate>                  
        </asp:TemplateField>
        <asp:ButtonField ButtonType="Button" HeaderText="Confirm" Text="Confirm" CommandName="Update" />
    </Columns>
</asp:GridView>
//My ascx.cs code
protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int rowIndex = e.RowIndex;

    string productID = grid.DataKeys[rowIndex].Value.ToString();
    int productId = Int32.Parse(productID);
    GridViewRow gridRow;

    gridRow = grid.Rows[rowIndex];

    TextBox tname = (TextBox)gridRow.FindControl("tb_Name");


    bool success = CatalogAccess.ProductUpdateItem(productId, tname.Text);

    statusLabel.Text = success ? "Product successfully updated! " + tname.Text.ToString() : "There was an error updating the product";

    PopulateControls();

}

我的问题是,如何获取我输入的新TextBox值?我无法更新,因为我总是得到Eval值,而不是我输入的新值?

1 个答案:

答案 0 :(得分:0)

尝试在load方法中添加postback方法if(!ispostback)并在其中添加网格绑定方法