views:

52

answers:

1

I have a gridview in a page and it have a template field:

        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtReturn" runat="server" Text="0"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

And I wrote some code in a command button Click Event to read TextValue of this text box :

int i = 0;
        foreach (GridViewRow row in grdFactor.Rows)
        {
            TextBox txt = (TextBox)(row.FindControl("txtReturn"));
            int ret = 0;
            try
            {
                ret = Int32.Parse(txt.Text);
                if (ret > 0 && ret < factor.Orders[i].Entity)
                {
                    factor.Orders[i].updateReturn(ret);
                }
            }
            catch (Exception ex) { }

            i++;
        }

But the value of txt.Text is always Zero. Could you help me please? Thanks.

+1  A: 

When are you calling DataBind() on the Grid or Page? Often times developers will Bind the data twice and override the data recieved from Request.Form.

Be sure to check the Page.IsPostBack boolean.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.myGrid.DataSource = list;
        this.myGrid.DataBind();
    }
}
bendewey