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?
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
http://forums.asp.net/thread/1623270.aspx
Hope it helps,
Jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
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?
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
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();
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
Marked as answer by Mokles on May 15, 2007 02:33 PM
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.
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.
Mokles
Member
42 Points
41 Posts
Table is losing its rows that was added dynamically, on PostBack
May 11, 2007 04:17 PM|LINK
Dear All,
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?
Thanks,
Mokles
Table definition:
<asp:Table ID="tblForecastPreview" runat="server"> </asp:Table>
Adding the rows when the user clicks a button:
while (ForecastDate <= ToDate)
{
//Add row to table.
tr =
new TableRow();tc =
new TableCell();tc.Text = ForecastDate.ToShortDateString();
tr.Cells.Add(tc);
tc =
new TableCell();tbxTextBox =
new TextBox();tbxTextBox.ID =
"tbxAnimalNum" + TableRowNum.ToString();tc.Controls.Add(tbxTextBox);
tr.Cells.Add(tc);
tblForecastPreview.Rows.Add(tr);
TableRowNum++;
ForecastDate = ForecastDate.AddDays(SkipDays);
}
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Table is losing its rows that was added dynamically, on PostBack
May 11, 2007 04:49 PM|LINK
Microsoft MVP - ASP.NET
Jessica Cao ...
All-Star
25328 Points
2567 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
May 14, 2007 03:33 AM|LINK
Hello,
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 http://forums.asp.net/thread/1623270.aspx
Hope it helps,
Jessica
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
Mokles
Member
42 Points
41 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
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 =
new TextBox();tbxTextBox.ID =
"tbxAnimalNum" + TableRowNum.ToString();tbxTextBox.Text = AnimalNum.ToString();
tc.Controls.Add(tbxTextBox);
tr.Cells.Add(tc);
tblForecastPreview.Rows.Add(tr);
TableRowNum++;
Thanks for you reply.
Mokles
adding dynamic controls accessing controls 2.0
Haissam
All-Star
37421 Points
5632 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
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;
HC
MCAD.NET
| Blog |
Jessica Cao ...
All-Star
25328 Points
2567 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
May 15, 2007 02:16 AM|LINK
Hello,
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();
tc.Controls.Add(tb);
tr.Cells.Add(tc);
}
Table1.Rows.Add(tr);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (TableRow tr in Table1.Rows)
{
foreach (TableCell tc in tr.Cells)
{
TextBox tb = tc.Controls[0] as TextBox;
Response.Write(tb.Text + "<br>");
}
}
}
}
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Table ID="Table1" runat="server">
</asp:Table>
Hope it helps,
Jessica
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
Mokles
Member
42 Points
41 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
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
Mokles
Mokles
Member
42 Points
41 Posts
Re: Table is losing its rows that was added dynamically, on PostBack
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.
Anyway thanks all for the support
Mokles