Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

viewstate and dynamic table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
russ - 18 Jul 2005 00:59 GMT
Hi all,

Here's a problem I'm having with a dynamic table.  Following the
guidelines here
(http://www.codeproject.com/aspnet/dynamiccontrols.asp), which make
perfect sense.  The problem is that the table contains a SELECT box
populated on the initial load.  Every time I postback I'm inserting a
column into the table, the dropdown always remains in the last column.
First time I postback the dropdown is populated okay.  The second time
its empty.

I'm guessing there's something funny going on with the viewstate
population matching up with the control hierarchy.  If I move the
dropdown out of the table everything is okay.  This article has a very
good explanation of the page creation sequence
(http://aspalliance.com/articleViewer.aspx?aId=134&pId=).  I think
first time around the control tree matches up when its attempting to
populate.  In the second postback, the received __viewstate contains
the dynamic cell, inside LoadFromViewState the dynamic cells don't
exist yet, so the select doesn't match up?

Here's some code snippets:
ASPX---------------------------------------------------------------
<form id="Form1" method="post" runat="server">
<table id="tblLines" runat="server">
 <tr>
  <td>Select one:</td>
  <td>
   <select runat="server" id="SelLine" NAME="SelLine">
    <option selected>Select one</option>
   </select>
  </td>
 </tr>
</table>
<asp:Button ID="BtnAdd" Runat="server" Text="click me"></asp:Button>
<hr>
</form>

ASPX.CS------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
 // bind the selectbox to the array to populate it.
 string[] strlines = new string[] {"Circle", "Jubilee", "Hammersmith",
"Metropolitan", "Central", "Northern"};

 SelLine.DataSource = strlines;
 SelLine.DataBind();

 persistedControls = new ArrayList();
 ViewState["persistedControls"] = persistedControls;
}
else
{
 persistedControls = (ArrayList)ViewState["persistedControls"];
 RecreatePersistedControls();
}
}

void RecreatePersistedControls()
{
HtmlTableRow tr = tblLines.Rows[0];
foreach (string line in persistedControls)
{

 HtmlTableCell tc = new HtmlTableCell();
 tc.InnerHtml = line;
 tr.Cells.Insert(tr.Cells.Count-1, tc);
}
}

private void BtnAdd_Click(object sender, System.EventArgs e)
{
if (SelLine.SelectedIndex!=0)
{
 string line = SelLine.Items[SelLine.SelectedIndex].Text;
 HtmlTableRow tr = tblLines.Rows[0];
 HtmlTableCell tc = new HtmlTableCell();
 tc.InnerHtml = line;
 tr.Cells.Insert(tr.Cells.Count-1, tc);
 // add the text to the PersistedControls
 persistedControls.Add(line);
}
}
russ - 20 Jul 2005 00:02 GMT
i think I've figured out what is going wrong here (or at least come up
with a solution to my problem).

the problem occurs when dynamic controls 'push' a viewstate populated
control to another location in the control tree.  to fix this problem,
a better approach would be to RecreatePersistedControls immediately
after LoadViewState (rather than the Page_Load), e.g:

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
RecreatePersistedControls();
}

this way the dynamic controls have been built at the point where
asp.net comes around to populating the select box, and everything
matches. and the viewstate collection is available if you are storing
your data/controls in there.

the aspalliance article I mentioned does have this to say on
LoadViewstate: "The best use of this method is to restore any dynamic
controls you created in events, based on values you must manually save
in ViewState, which is now available to use."

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.