|
-
Mar 10th, 2004, 04:39 PM
#1
Thread Starter
Fanatic Member
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.
-
Mar 11th, 2004, 08:43 PM
#2
Hyperactive Member
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.
-
Mar 16th, 2004, 12:13 PM
#3
Thread Starter
Fanatic Member
I am a bit confused. So there would be no code behind done for this? Code you draw up the function?
-
Mar 16th, 2004, 12:21 PM
#4
Thread Starter
Fanatic Member
Basically what I am doing is sending the selected value of the listbox to another aspx.
-
Mar 16th, 2004, 07:43 PM
#5
Hyperactive Member
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>
-
Mar 17th, 2004, 11:07 AM
#6
Thread Starter
Fanatic Member
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;
-
Mar 17th, 2004, 11:31 AM
#7
Hyperactive Member
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.
-
Mar 17th, 2004, 12:25 PM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|