Mixing Server and Client Code

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

I am having big time problems seperating the two mention in subject line.

I want to create a list of items (listbox or select) from a button click (on
the server). The button click will ensure that all data required for
viewing the page is within the page and after that selecting items from the
table should cause client processing to modify the look of the page.

The server code is easy. I access data on the server and add it to the the
items collection of the listbox. Now logically I want to switch to client
processing with JavaScript. The problem follows:

If I use a ListBox (<asp:ListBox) then how would I capture a selection
change so that I can process the clientside code? I would need the text of
the selected item as well.

If I use a Select (straight HTML) then how does the server fill the HTML
listbox?

I also am very confused about how Intellisence seems to know about controls
but runtime does not. I have another control defined as <EMBED id="Player"
.... When I am inputing client JavaScript I get full Intellisence help for
this name (Player) but when I call the function which references the control
I get runtime errors (I have script debugging set on) indicating that the
control Player does not exist.
 
I think this is what you're trying to do...

Code-Behind snippet:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

'simulate loading first time in from your database query
Dim alText As New ArrayList
Dim alVals As New ArrayList
alText.Add("Item One")
alVals.Add("1")
alText.Add("Item Two")
alVals.Add("2")
alText.Add("Item Three")
alVals.Add("3")

For i As Integer = 0 To alText.Count - 1
myListBox.Items.Add(New ListItem(alText(i),
alVals(i)))
Next

myListBox.Attributes.Add("onchange", "test(this)")
End If
End Sub

HTML snippet:

<HEAD>
<title>WebForm1</title>
<script language="javascript">
function test(select) {
var i = select.selectedIndex
var opts = select.options
alert(opts.text + " has a value of " +
opts.value)
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:ListBox id="myListBox" runat="server"></asp:ListBox>
<br>
<input type=submit>
</form>
</body>
 
Back
Top