GridView textbox problem in ASP.NET
I have a gridview on a page and it has a template field:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtReturn" runat="server" Text="0"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
And I wrote the code on the command line. Click "Event" to read the TextValue of this text field:
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 txt.Text value is always zero. Could you help me please? Thank.
+2
source to share
1 answer
When do you call DataBind () on a grid or page? Often, developers will bind data twice and override the data received from Request.Form.
Be sure to check the boolean file Page.IsPostBack.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.myGrid.DataSource = list;
this.myGrid.DataBind();
}
}
+1
source to share