开发者

ASP.NET GridView TextBox Problem

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 开发者_JAVA技巧= 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.


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜