PDA

Click to See Complete Forum and Search --> : ListBox


brianh
Mar 10th, 2004, 03:39 PM
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.


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.

pvb
Mar 11th, 2004, 07:43 PM
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:
string email = "";
if( lbEmailDis.SelectedItem != null )
{
email = lbEmailDis.SelectedItem.Value;
}
else
{
email = "default@email.com";
}
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.

brianh
Mar 16th, 2004, 11:13 AM
I am a bit confused. So there would be no code behind done for this? Code you draw up the function?

brianh
Mar 16th, 2004, 11:21 AM
Basically what I am doing is sending the selected value of the listbox to another aspx.

pvb
Mar 16th, 2004, 06:43 PM
Well, based on your 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):<%@ Page language="c#" Codebehind="DoubleClick.aspx.cs"
AutoEventWireup="false" Inherits="DeathAngel.DoubleClick" %>
<html>
<script language="javascript">
function getDescrip( sender ){
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="peezley@panteravb.com">Carter, Peezley</asp:ListItem>
<asp:ListItem Value="chris@panteravb.com">Carter, Chris</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>

brianh
Mar 17th, 2004, 10:07 AM
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;

pvb
Mar 17th, 2004, 10:31 AM
try this:<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="peezley@panteravb.com">Carter, Peezley</asp:ListItem>
<asp:ListItem Value="chris@panteravb.com">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.

brianh
Mar 17th, 2004, 11:25 AM
Awesome, I think that will work, thanks!!!