Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 15, 2007 02:40 PM by Mokles
May 11, 2007 04:17 PM|LINK
I have a table control on the page, with no rows. I am adding rows to the table when a user clicks a button. Then when the user clicks another button, I would like to process the content of the table, but the table loses its rows. How do I make the table
keep its contents on postback?
Adding the rows when the user clicks a button:
while (ForecastDate <= ToDate)
//Add row to table.
tc.Text = ForecastDate.ToShortDateString();
ForecastDate = ForecastDate.AddDays(SkipDays);
May 11, 2007 04:49 PM|LINK
May 14, 2007 03:33 AM|LINK
ASP.NET page is stateless, it is created every time from scratch. When the Page does post back to the server, it will lose everything except you manage the state manually by yourself. Since you are creating the table at run time, you have to create everytime
the page load or the page init. otherwise, the page will lose the table after postback. If you want to add the control at run time in the button click event, please refer to this previous post
Hope it helps,
May 14, 2007 05:05 PM|LINK
Thank Ed and Jessica for the reply.
I guess I was not clear with my question. As you can see from my posted code, I am not creating the table dynamically, I am only adding rows to the table from code behind. Also, I am adding a text box to one of the columns of the table. I want the user to
be able to change the value of the text box in that column and when the form is posted, I want to process in the code behind what the user input in the text box. But in realilty, as soon as the form is posted, the table becomes empty. The row count of the
table ( value of tblForecastPreview.Rows.Count) becomes zero. Should all the data on the form not be available to the code behind, even it was generated dynamically at run time?
tbxTextBox.Text = AnimalNum.ToString();
Thanks for you reply.
adding dynamic controls
May 14, 2007 07:40 PM|LINK
Because you are adding the row dynamically, to find the refere to the textbox control you have to use the FindControl() of the table control which will get you the textbox and in which you can get its text
Textbox tx = (TextBox)tblForecastPreview.FindControl("TextBoxID");
string enteredValue = tx.Text;
May 15, 2007 02:16 AM|LINK
Please see the following sample code
public partial class Report : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
for (int i = 0; i < 3; i++)
TableRow tr = new TableRow();
for (int j = 0; j < 2; j++)
TableCell tc = new TableCell();
TextBox tb = new TextBox();
tb.ID = "tb"+i.ToString() + j.ToString();
protected void Button1_Click(object sender, EventArgs e)
foreach (TableRow tr in Table1.Rows)
foreach (TableCell tc in tr.Cells)
TextBox tb = tc.Controls as TextBox;
Response.Write(tb.Text + "<br>");
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Table ID="Table1" runat="server">
May 15, 2007 02:38 PM|LINK
What I learned from it is that on every Page_Load I have to create and add the rows and columns again before being able to extract their values and even if I do not want to display the table again.
Anyway thanks all for the support
May 15, 2007 02:40 PM|LINK
What I learned from it is that on every Page_Load I have to create and add the rows and columns again before being able to extract their values, even if I do not want to display the dynamically created rows and columns of the table again.