Click to See Complete Forum and Search --> : HTML list box in asp.net


coolvaas
September 9th, 2004, 03:11 PM
I have the following problem.
My asp.net page has one html list box control and I made that as "RUN AS SERVER" so that I could access it in client side script as well as server side script. And also I have a 2 buttons in my form one is an html button and the other a server side button. When I click on the html button using client side script I am adding some data to the list box. And when user clicks on the server side button I want to acess the contents of the list box. But the thing is the moment I click on the server side button it clears the contents of the list box (even though I made it run as server). Then I tried to do the same using an HTML text box and it works fine. It's contents never get's cleared. But the list box always clears it's contents. Could any one tell me why is this happening? How can I access the contents of the html list box from server side or how can I stop clearing of the list box contents.
Here is my code

<script language="vbscript">
Sub LoadListBoxData()
dim oOption
dim i
for i=1 to 10
set oOption = document.createElement("OPTION")
document.Form1.lstTest.options.add(oOption)
oOption.innerText = now() & i
oOption.value =i
next
end sub
</script>

'''Here is my html code which calls the LoadListBoxData procedure
<INPUT style="Z-INDEX: 103; LEFT: 408px; POSITION: absolute; TOP: 104px" onclick="LoadListBoxData()" type="button" value="Button">

''Here is web server control button
<asp:button id="Button1" style="Z-INDEX: 104; LEFT: 120px; POSITION: absolute; TOP: 16px" runat="server" Text="Submit"></asp:button>

Is there a way to save data in an HTML list box made run as server between postbacks
Any help is greatly appriciated

Thanks

cmiskow
September 9th, 2004, 04:11 PM
I just did some playing around with html listboxes marked runat=server, and it seems that ASP.NET doesn't handle dynamically added options very well at all. One workaround would be to add a hidden control that keeps a list of the items you have dynamically added to the listbox, and then on Page_Load you can parse the list and add those items back into the list on the server side.

coolvaas
September 9th, 2004, 09:56 PM
Thanks for your reply. Greatly appriciate it.And would like to ask you one more question. You said that have a hidden control and add it to the list box on form load. So what kind of control should I have as hidden?
Thanks again

cmiskow
September 10th, 2004, 10:01 AM
The preferable control would be the HTML hidden control, as in <input type='hidden'>.

I tried this out and it worked ok:

1) Add a hidden control to the form: <input type='hidden' name='hidNewOptions' id='hidNewOptions' runat='server'>

2) Add script to add new listbox items' values and texts to the hidden control. Here is the revised LoadListBoxData() method:

Sub LoadListBoxData()
Dim oOption
Dim i
Dim str '*** new variable
str = ""

For i = 1 To 10
Set oOption = document.createElement("OPTION")
document.Form1.lstTest.options.add(oOption)
oOption.innerText = now() & i
oOption.value = i

'*** new code for building a list of new items into a string
If str <> "" Then str = str & ";"
str = str & oOption.innerText & ":" & oOption.value
Next

'*** new code for inserting the new string into the hidden control
If document.Form1.hidNewOptions.value <> "" Then str = ";" & str
document.Form1.hidNewOptions.value = document.Form1.hidNewOptions.value & str
End Sub

This will store inside the hidden control a semi-colon-separated list of text/value pairs representing new options. Each option is stored as "innerText:value".

3) In the Form_Load event, add something like this:

Dim arrOptions() As String = hidNewOptions.Value.Split(";")
Dim i As Integer = 0


If hidNewOptions.Value <> "" Then
While i < arrOptions.Length
lstTest.Items.Add(New ListItem(arrOptions(i).Split(":")(0), arrOptions(i).Split(":")(1)))
i += 1
End While
End If
hidNewOptions.Value = ""


I tried this out and it worked for me. I did use javascript instead of vbscript for the client-side part, so it might be a little buggy. Also note that most means of getting the selected value on the server side won't work for the options that were dynamically generated before the postback, but you can still get the value by Request.Form("lstTest").

coolvaas
September 10th, 2004, 11:56 AM
Great!!!!!!. Thanks a million. We had many problems regarding this. And your solution works great. Hats off to you friend. Excellent work. Thanks again. Keep up the good work
:thumb: :cool: