Results 1 to 8 of 8

Thread: ListBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954

    ListBox

    I am trying to add an attribute to allow for some client side scripting ability to be executed on a server side control. Here is what I have in page_load.

    Code:
    this.lbEmailDis.Attributes.Add("onDblclick", "getDescrip(" + lbEmailDis.SelectedItem.Value + ");");
    getDescrip is a javascript function. it bombs on lbEmailDis.SelectedItem.Value. The error is: Object reference not set to an instance of an object.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You might want to check and make sure that you the SelectedItem is not null. Probably need to figure it out before assigning that attribute. Maybe something like:
    PHP Code:
    string email "";
    if( 
    lbEmailDis.SelectedItem != null )
    {
      
    email lbEmailDis.SelectedItem.Value;
    }
    else
    {
      
    email "[email protected]";
    }
    string ondblclick String.Format("getDescrip({0});"email);
    this.lbEmailDis.Attributes.Add("ondblclick"ondblclick ); 
    If it were me? it looks like you could do it all on the client side. You can add any attribute you feel like to the asp.net server controls, even ones you make up, and it'll stick em in there on the clientside. So you could, if it makes sense to what you're doing, add ondblclick to the server control in the aspx page, and assign it a function that can determine the selected item in the list using plain ol javascript. Obviously I have no clue what the big picture is for your project, just throwin an idea out there.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    I am a bit confused. So there would be no code behind done for this? Code you draw up the function?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    Basically what I am doing is sending the selected value of the listbox to another aspx.

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well, based on your code:
    PHP Code:
    this.lbEmailDis.Attributes.Add("onDblclick""getDescrip(" lbEmailDis.SelectedItem.Value ");"); 
    I'm assuming that the getDescrip javascript function is supposed to do something with the item that the user double clicked on, so if that's the case you could try something like this(note the ondblclick attribute on the asp:ListBox):
    PHP Code:
    <%@ Page language="c#" Codebehind="DoubleClick.aspx.cs" 
        
    AutoEventWireup="false" Inherits="DeathAngel.DoubleClick" %>
    <
    html>
        <
    script language="javascript">
        function 
    getDescripsender ){
            
    email sender.options[sender.selectedIndex].value;
            
    alert(email);
        }
        </
    script>
        <
    body>
            <
    form runat="server">
                <
    asp:ListBox ID="lbEmailDis" Runat="server" ondblclick="getDescrip(this);">
                    <
    asp:ListItem Value="[email protected]">CarterPeezley</asp:ListItem>
                    <
    asp:ListItem Value="[email protected]">CarterChris</asp:ListItem>
                </
    asp:ListBox>
            </
    form>
        </
    body>
    </
    html

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    hmmm...it doesn't seem to recognize sender as an object. Here is the error.

    Error: sender has no properties
    Source File: http://bonefish/eaf/eaf.aspx
    Line: 12

    On this line: email = sender.options[sender.selectedIndex].value;

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    try this:
    Code:
    <html> 
        <script language="javascript"> 
        function getDescrip(){ 
    		listbox = document.getElementById("lbEmailDis");
    		if(listbox){
    			email = listbox.options[listbox.selectedIndex].value; 
    			alert(email);
    		}else{
    			alert("Couldn't find the listbox that should have the id: tlbEmailDis");
    		}        
        } 
        </script> 
        <body> 
            <form runat="server" ID="Form1"> 
                <asp:ListBox ID="lbEmailDis" Runat="server" ondblclick="getDescrip(this);"> 
                    <asp:ListItem Value="[email protected]">Carter, Peezley</asp:ListItem> 
                    <asp:ListItem Value="[email protected]">Carter, Chris</asp:ListItem> 
                </asp:ListBox> 
            </form> 
        </body> 
    </html>
    the basic concept i am trying to get across to ya is that you can use clientside javascript to grab the value of the item that gets double clicked.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    Awesome, I think that will work, thanks!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width