msdn上有示例 private void listBox1_DoubleClick(object sender, System.EventArgs e) { // Get the name of the file to open from the ListBox. String file = listBox1.SelectedItem.ToString(); try { // Determine if the file exists before loading. if (System.IO.File.Exists(file)) { // Open the file and use a TextReader to read the contents into the TextBox. System.IO.FileInfo myFile = new System.IO.FileInfo(listBox1.SelectedItem.ToString()); System.IO.TextReader myData = myFile.OpenText();; textBox1.Text = myData.ReadToEnd(); myData.Close(); } } // Exception is thrown by the OpenText method of the FileInfo class. catch(System.IO.FileNotFoundException) { MessageBox.Show("The file you specified does not exist."); } // Exception is thrown by the ReadToEnd method of the TextReader class. catch(System.IO.IOException) { MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file."); } }
vb代码中 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListBox1.Attributes.Add("ondblclick", "lstdbclick()") End Subaspx中 <script> function lstdbclick() { alert(document.Form1.ListBox1.options[document.Form1.ListBox1.getAttribute("selectedIndex")].value); } </script>
{
string data = this.listBox1.SelectedItem.ToString();
MessageBox.Show(data); }
错误如下:
“System.Web.UI.WebControls.ListBox”并不包含对“DoubleClick”的定义
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
// Get the name of the file to open from the ListBox.
String file = listBox1.SelectedItem.ToString(); try
{
// Determine if the file exists before loading.
if (System.IO.File.Exists(file))
{
// Open the file and use a TextReader to read the contents into the TextBox.
System.IO.FileInfo myFile = new System.IO.FileInfo(listBox1.SelectedItem.ToString());
System.IO.TextReader myData = myFile.OpenText();; textBox1.Text = myData.ReadToEnd();
myData.Close();
}
}
// Exception is thrown by the OpenText method of the FileInfo class.
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("The file you specified does not exist.");
}
// Exception is thrown by the ReadToEnd method of the TextReader class.
catch(System.IO.IOException)
{
MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file.");
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
<!--
function Url(obj)
{
var sel=obj.options[obj.selectedIndex].text;
window.open("IEPrint/main.aspx?param="+sel);
}
//-->
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<SELECT id="Select1" ondblclick="Url(this)" style="Z-INDEX: 101; LEFT: 176px; WIDTH: 184px; POSITION: absolute; TOP: 112px; HEIGHT: 96px"
size="6" name="Select1" runat="server">
<OPTION>aaaaaaaaaa</OPTION>
<OPTION>bbbbbbbbbb</OPTION>
</SELECT>
</FONT>
</form>
</body>
</HTML>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Attributes.Add("ondblclick", "lstdbclick()")
End Subaspx中
<script>
function lstdbclick()
{
alert(document.Form1.ListBox1.options[document.Form1.ListBox1.getAttribute("selectedIndex")].value);
}
</script>
conghui(月是故乡明) 的方法,完全正确!