ASP.NET 2.0 VB Dropdown items disappear

1 view
Skip to first unread message

Rick

unread,
Apr 11, 2008, 7:42:47 PM4/11/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I'm using AS.NET 2.0 with VB.
I have a page with a dropdown on it, which is populated as follows:

When the page first loads, the dropdown is empty (no items).
When another dropdown on the same page changes the selection, a
Javascript function is called.

The JS function creates an IFrame and a Form, and posts (using POST)
to the code behind page, passing the parameters in the url.

When the VB code behind page receives those especific parameters, is
calls a procedure that retrieves the data from the database, and
generates a string containing Javascript code.

This JS code iterates thru the retrieved data, creating an "OPTION"
HTML element (a DOM object), and asigning to each OPTION the value
from the corresponding database record. The created OPTION DOM object
is added to the dropdown object.

Then the string containing the JS is written to the page using
Response.Write(myString).

The page is displayed, and the dropdown does show the items, and the
user can select.

When the page's Save button is clicked, its onclientclick event calls
a JS validation function that checks that an item is selected from the
dropdown.

If I step thru the code using Visual Studio debugger, I can inspect
the dropdown in the JS validation function, and it does have all the
items, and the one selected by the user is in the dropdown's value
member.

Then the flow of control enters the Load() function in the page's code
behind.
Inside Load(), if I inspect the dropdown again, it has 0 items.

I just don't know how or why does this happen.
After the Validate JS function ends, I cannot step through the code
any further, so I have a breakpoint in the page Load event handler.
Somewhere between these 2 points the dropdown loses all its options.

If I look at the HTM generated while debugging, the dropdown appears
as an empty <select ...></select> with no <option> in between, and the
Javascript that was generated in a string and written to the page, is
not there... but somehow it should have been at some point, b/c it
populated the dropdown.

Your helpwith this will be greatly appreciated,

Sincerely,

Rick


Cerebrus

unread,
Apr 12, 2008, 10:37:41 AM4/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
After starting so well, did you really have to spoil it by saying this
(Ugh !) ? :

> Then the string containing the JS is written to the page using
> Response.Write(myString).

As for the problem, this is a typical scenario. When you create DOM
objects dynamically in this manner, the server is unaware of it and
the ViewState of the page does not change. Therefore when the page
posts back, the old state is retained.

One of the easiest ways to solve your problem would be have a Hidden
field (runat="server") on the page which gets a copy of the value of
the HTML select as soon as a selection is made. You could do this in
your Validate function. This value will then be retrievable in your
Code behind.

Rick

unread,
Apr 14, 2008, 9:50:18 AM4/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Cerebrus,

You're right, that's an ugly practice. I inherited this code from some
consultants. Their intent was probably to populate the dropdown using
client code, but it doesn't make sense, b/c there's a postback to the
codebehind in order to add the JS code to the page. This has made me
waste a bunch of time, and should certainly be redesigned.

Your solution did work, and I appreciate your help. Thank you!
> > Rick- Hide quoted text -
>
> - Show quoted text -

Cerebrus

unread,
Apr 14, 2008, 12:30:23 PM4/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Most welcome ! Glad to assist. ;-)

Rick

unread,
Apr 14, 2008, 2:44:09 PM4/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
One final thought, if I change the code to issue an XML Http Request
using JS call from the client, in order to get the data, and then, in
the JS function get the data, and add the items to the dropdown.
Would the items stay there when the form posts back?

Thanks in advance,

Rick
> > Your solution did work, and I appreciate your help. Thank you!- Hide quoted text -

Rick

unread,
Apr 15, 2008, 11:34:17 AM4/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
One thought. does that mean that if from Javascript I issue a HTTP XML
Request, which returns the data to populate the dropdown, and in the
JS function create and insert the items ('option's) in the dropdown
('select'), and then the user clicks "Save", and the page is posted
back, the items in the dropdown will disappear?

Thanks in advance,

Rick

On Apr 14, 12:30 pm, Cerebrus <zorg...@sify.com> wrote:
> > Your solution did work, and I appreciate your help. Thank you!- Hide quoted text -

Cerebrus

unread,
Apr 15, 2008, 12:22:42 PM4/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
No. How you retrieve data is irrelevant to the situation. What is
relevant is Viewstate.
> > - Show quoted text -- Hide quoted text -

Rick

unread,
Apr 15, 2008, 3:29:28 PM4/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Then how can items be added safely (meaning the added items persist
when posting back) to a dropdown list using Javascript?

Thanks in advance
Reply all
Reply to author
Forward
0 new messages