Mixing Server and Client Code

Discussion in 'ASP .Net' started by Lloyd Sheen, Nov 18, 2003.

  1. Lloyd Sheen

    Lloyd Sheen Guest

    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.
    Lloyd Sheen, Nov 18, 2003
    #1
    1. Advertising

  2. Lloyd Sheen

    Dales Guest

    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>
    Dales, Nov 18, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Repphan

    Mixing server and client controls

    Shawn Repphan, Jan 18, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    482
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jan 20, 2006
  2. -
    Replies:
    2
    Views:
    397
    Alan Krueger
    Jul 29, 2005
  3. Replies:
    4
    Views:
    694
  4. Replies:
    1
    Views:
    550
    Greg Schmidt
    Dec 7, 2004
  5. Richard
    Replies:
    2
    Views:
    504
    Richard
    May 12, 2006
Loading...

Share This Page