Listbox does not appear in Request.Form

M

Mr Newbie

DropDown lists and Listboxes do not appear in the list of controls and
values passed back to the server on PostBack in Request.Form object. Can
someone confirm this to be correct and possibly answer why ?

The impact of this is that values added via javascript will never make it
back to the server side code unless one does some jiggery pokery with hidden
fields etc. If this is true, then I would like to understand the reason
behind the design decision and if this has been addressed in VS2005 ?


Cheers - Mr N
 
J

Juan T. Llibre

Hello, Mr. Newbie.

re:
The impact of this is that values added via javascript will never make it back to the
server side code unless one does some jiggery pokery with hidden fields

JavaScript runs client-side and forms run server-side.

You need to pass the values to the server from your javascript,
which is running where the server will never see it.

re:
If this is true, then I would like to understand the reason behind the design decision
and if this has been addressed in VS2005 ?
..
The reason is "by design", per the explanation above.

What I wonder is why you are still sticking to Request.Form,
instead of using the .NET native Request.Params, which returns
a combined collection of QueryString, Form, ServerVariables, and Cookies.

Request.Params works the same way in ASP.NET 1.x, 2.0 and WinFx :

http://msdn.microsoft.com/library/d...frlrfsystemwebhttprequestclassparamstopic.asp

http://msdn2.microsoft.com/en-us/library/dt24b615

http://winfx.msdn.microsoft.com/lib...pref/html/P_System_Web_HttpRequest_Params.asp
 
K

Karl Seguin

This isn't an ASP.Net issue, it's how HTML and HTTP work. For list
controls, such as <input type="radio/checkbox" and <select> only the
SELECTED value is passed in the Request.Form. This is how the first form
worked in HTML 1.0 and how it works now.

ViewState tries to resolve this problem by taking all the values before the
page is rendered and serializing said values to a hidden field, which can
then be used to recreate the controls on postback. If you have a listbox
with 10 items, then those 10 items will be serialized to a hidden field. if
you dynamically add an 11th item using javascript to the control, the value
still won't be added to the hidden field and thus won't appear during
postback. This is neither a bug nor a limitation of ASP.Net.

You need to do some hidden field trick. It's often easier to copy the
selected value to a hidden field (assuming you need to know what the
selected value was) and then select all the values in the
Select/radio/checkbox It results in less code...

Karl
 
M

Mr Newbie

I dont think you have understood me, perhaps I did not make myself clear.

I am well aware that Javascript runs client side and web forms run server
side. The question is regarding ListBox and DropDown boxes. Why does ASP.NET
not preserve the 'STATE' of those controls ?

The point being is that controls such as TextBox's and Buttons etc have
their state preserved by using the Viewstate. For example if I add client
side a charavter to a textbox, ASP.NET will update the viewstate and whent
the form is posted back to the client. That change is reflected. However,
with ListBox and DropDown, this is not the case ( Apparently ) , the
question is why ?

This is not really anything to do with Javascript other than this is how I
would add items client side if I wanted to.

Does this make my question clearer ?
 
M

Mr Newbie

This isn't an ASP.Net issue, it's how HTML and HTTP work. For list
controls, such as <input type="radio/checkbox" and <select> only the
SELECTED value is passed in the Request.Form. This is how the first form
worked in HTML 1.0 and how it works now.

I tried selecting a value which was added using Javascript, but this did
not get passed back either. So are you saying then that only the selected
value or values are passed back if the values were generated by asp.net ?
 
K

Karl Seguin

Just to clarify in addition to my post...TextBoxes don't work like you think
they do with ViewState. The value of a textbox isn't preserved via
ViewState. Since there's only 1 value and HTML specifications say that the
value has to be passed via REquest.Form, it would be foolish for ASP.Net to
also maintain it in ViewState (waste of space).

ViewState is used where/when Request.Form can't. Namely for controls that
don't participate in the form submission (datagrid for example) as well as
controls who only partially participate in form submission (only the
selected radio input gets passed).

Karl
 
M

Mr Newbie

OK, that makes sense I suppose. Presumably though viewstate is used with
textboxes to determine and fire the text changed event by comparing the text
before and that which is preserved in viewstate, or do I have that wrong as
well.

I dont seem to be doing very well today!

Cheers Mr N
 
K

Karl Seguin

No, I think you are right about that part :) i could look into it, but
let's say you are...can't think of how else it would do it ...nice catch!

Karl
 
K

Karl Seguin

No, you should be seeing the value in Request.Form

Request.Form is totally independant of ViewState or server-side this or
client-side that...it's much more basic than all of that. Whatever value is
selected, however it got there, should show up. The only thing I can think
of, without seeing code, is that ASP.Net does generate unique clientId's so
if you have

<asp:dropdownlist id="SomeId" runat="server" />

it might get rendered as

<select name="clt0:SomeId" > ... </select>

Form submission works on Name not Id, so I only showed the Name.

Anyways, you might be doing Request.Form["SomeId"] when really you should
be doing Request.Form[someId.ClientId] but that'll only happen when your
controls are nested.

Two things you can do. Turn tracing on in the page <%@ Page trace="True"
.... %> and look at the output, or debug, add a breakpoint in Page_Load and
Add a Watch to REquest.Form

karl
 
M

Mr Newbie

I found another way around this. If you hijack the form submit, you can
select all members of the ListBox and then submit the code, this is only any
good if you dont want to select a single item from the list. Otherwise your
hidden field idea is the only way forward.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If IsPostBack Then

Dim col As System.Collections.Specialized.NameValueCollection

Dim listItemsArray() As String

Dim strListItems As String

col = Request.Form

strListItems = col.Item("ListBox1")

listItemsArray = strListItems.Split(",")

ListBox1.Items.Clear()

For Each s As String In listItemsArray

ListBox1.Items.Add(s)

Next









End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top