-
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.
-
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.
-
I am a bit confused. So there would be no code behind done for this? Code you draw up the function?
-
Basically what I am doing is sending the selected value of the listbox to another aspx.
-
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 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="[email protected]">Carter, Peezley</asp:ListItem>
<asp:ListItem Value="[email protected]">Carter, Chris</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>
-
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;
-
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.
-
Awesome, I think that will work, thanks!!!