473,692 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

viewstate and dynamic table

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 LoadFromViewSta te 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(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
// bind the selectbox to the array to populate it.
string[] strlines = new string[] {"Circle", "Jubilee", "Hammersmit h",
"Metropolit an", "Central", "Northern"} ;

SelLine.DataSou rce = strlines;
SelLine.DataBin d();

persistedContro ls = new ArrayList();
ViewState["persistedContr ols"] = persistedContro ls;
}
else
{
persistedContro ls = (ArrayList)View State["persistedContr ols"];
RecreatePersist edControls();
}
}

void RecreatePersist edControls()
{
HtmlTableRow tr = tblLines.Rows[0];
foreach (string line in persistedContro ls)
{

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

private void BtnAdd_Click(ob ject sender, System.EventArg s e)
{
if (SelLine.Select edIndex!=0)
{
string line = SelLine.Items[SelLine.Selecte dIndex].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 PersistedContro ls
persistedContro ls.Add(line);
}
}

Nov 19 '05 #1
1 4832
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 RecreatePersist edControls immediately
after LoadViewState (rather than the Page_Load), e.g:

protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate (savedState);
RecreatePersist edControls();
}

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."

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5648
by: dschectman | last post by:
I have an interesting issue. I need to implement a dynamic table to mimic a select list. Each time you double click from the master list, a row is added to the list of selected items. The list of selected items has a column header and differentiation between odd and even rows. This part is trivial. Clients have the ability to re-order the list of selected items. This is also trivial: I trap the onClick event for a row to select it. ...
2
1204
by: Bernie V | last post by:
Hello group, Can somebody help me to create a dynamic table control from an sql database ? I don't know exacly where I have to start. Any tip, link to the web is ok. Thx in advance!
2
4954
by: dschectman | last post by:
This appears to be a feature of IE JavaScript. I am running IE 6.0 with the latest patches from Microsoft. Are there any workarounds other than re-coding the source HTML to place all the non-visible columns at the front? I have a page with a dynamic table. The table has some visible columns and some non-visible columns. The style "hide" is .hide { display:none; } <table id="SelectedList">
2
599
by: H.c.m. Raaijmaakers | last post by:
Hi, The user can create a dynamic table. In every row are 5 input text boxes. If the second text box gets a onblur event then the value of the third box needs to become the value of the first text box minus the value of the secons text box. I have tried the next statement:
3
52340
by: ryanmhuc | last post by:
Is it possible to have a dynamic table name within a query or a table name that is a variable? This does not work but gives an example: SELECT * FROM concat('table', 'name') - OR - SET @table = 'a'; SELCT * FROM @table Of course the query can be constructed dynamically but does not exactly
1
1850
by: eureka | last post by:
Hi folks, I am working on a webapplication using Jsp and JS. On my main Jsp(Jsp1) I have a table which is created dynamically inside a <divand contains all the backend-table's records as rows, ie as many records in the MySql-table as many rows in my Html- table(say T1).I am creating this dynamic table using a Jsp-scriptlet and I have set a vertical scrollbar on the <divsuch that user can see 5 rows of T1 at a time.Each row in T1 has a...
0
2519
by: Dhanasekaran B | last post by:
Dear I need to create dynamic temproray table for reporting purpose.I did't have permission for DDL commands. Without Database Administrator permission, i need to create temp. Dynamic table. (or) How to give privilage for create temproray table. Yours Sincerly, Dhanasekaran.B
3
2103
by: arunank | last post by:
Hi, The following code for dynamic table creation is not working. Can anyone please help me. The dynamically created rows and columns are not getting populated. CODE: ========= <html>
3
4295
vikas251074
by: vikas251074 | last post by:
I have created dynamic tables whose length depends upon the number of records. Can I use scrollbar in tables? My code for creating dynamic table is as follows - <% dim conn set conn = Server.CreateObject("ADODB.Connection") conn.open = "Provider=MSDAORA.1;dsn=ops;password=op;user id=Outpass;Data Source=Intradb;Persist Security Info=True" set rs = server.CreateObject("ADODB.Recordset") set rs = conn.execute("Select gp_no, per_name, dept...
0
8604
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8961
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5819
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4324
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.