Tuesday, February 24, 2009

Saving value in view state and populating the text box from view state

Saving value on Button Click in view state

protected void Copy_Click(object sender, EventArgs e)
{
foreach(HtmlTableRow row in table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
ViewState[control.ID] = ((TextBox)control).Text;
}
}
}
}


and

Populating the text box from view state on Button Click

protected void Paste_Click(object sender, EventArgs e)
{

foreach (HtmlTableRow row in table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
string value = (string) ViewState[control.ID];
if(value != null)
{
((TextBox) control).Text = value;
}

}
}
}

}

No comments:

Post a Comment