就是生成一个listbox后,怎么样做才可以,让我们点选listbox的item之后,这个item的值放入textbox 并且 listbox 消失附上源码:<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="cbpanel.aspx.vb" Inherits="test.cbpanel" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="show_message" runat="server"></asp:Label>
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="153px">
</asp:Panel>
</div>
</form>
</body>
</html>
Imports System.Data.SqlClient
Partial Public Class cbpanel
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Dim conn As SqlConnection = New SqlConnection("server=172.18.130.46;database=SMSstudent;uid=sa;pwd=123456789") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cbr As String
cbr = Page.ClientScript.GetCallbackEventReference(Me, "document.all." & Me.TextBox1.ClientID & ".value", "ReceivedData", "")
TextBox1.Attributes("onkeyup") = cbr Dim Script As String = ""
Script += vbCrLf + "<script>"
Script += vbCrLf + "function ReceivedData(dat)"
Script += vbCrLf + "{"
Script += vbCrLf + "document.all." & Me.Panel1.ClientID & ".innerHTML = dat;"
Script += vbCrLf + "}"
Script += vbCrLf + "</script>"
Page.ClientScript.RegisterClientScriptBlock(GetType(String), "Script", Script)
End Sub Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return ret
End Function
Dim ret As String
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
Dim sel_sql As String = "select studentID,studentName from student where studentID like '" & eventArgument & "%' order by studentID"
Dim myadapter As SqlDataAdapter = New SqlDataAdapter(sel_sql, conn)
Dim ds As New DataSet
Try myadapter.Fill(ds, "biao")
If ds.Tables(0).Rows.Count > 0 Then
Dim ListBox As New ListBox
ListBox.Style.Item("z-index") = 110
ListBox.Style.Item("position") = "right"
ListBox.Width = Unit.Pixel(150)
ListBox.Height = Unit.Pixel(150) For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
ListBox.Items.Add(New ListItem(ds.Tables(0).Rows(i).Item("studentID") & ds.Tables(0).Rows(i).Item("studentName"), ds.Tables(0).Rows(i).Item("studentID")))
Next
Dim stringwriter As New System.IO.StringWriter
Dim htmltextwriter As New HtmlTextWriter(stringwriter)
ListBox.RenderControl(htmltextwriter)
ret = stringwriter.ToString
End If
Catch ex As Exception
show_message.Text = ex.Message
End Try
End Sub
End Class
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="show_message" runat="server"></asp:Label>
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="153px">
</asp:Panel>
</div>
</form>
</body>
</html>
Imports System.Data.SqlClient
Partial Public Class cbpanel
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Dim conn As SqlConnection = New SqlConnection("server=172.18.130.46;database=SMSstudent;uid=sa;pwd=123456789") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cbr As String
cbr = Page.ClientScript.GetCallbackEventReference(Me, "document.all." & Me.TextBox1.ClientID & ".value", "ReceivedData", "")
TextBox1.Attributes("onkeyup") = cbr Dim Script As String = ""
Script += vbCrLf + "<script>"
Script += vbCrLf + "function ReceivedData(dat)"
Script += vbCrLf + "{"
Script += vbCrLf + "document.all." & Me.Panel1.ClientID & ".innerHTML = dat;"
Script += vbCrLf + "}"
Script += vbCrLf + "</script>"
Page.ClientScript.RegisterClientScriptBlock(GetType(String), "Script", Script)
End Sub Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return ret
End Function
Dim ret As String
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
Dim sel_sql As String = "select studentID,studentName from student where studentID like '" & eventArgument & "%' order by studentID"
Dim myadapter As SqlDataAdapter = New SqlDataAdapter(sel_sql, conn)
Dim ds As New DataSet
Try myadapter.Fill(ds, "biao")
If ds.Tables(0).Rows.Count > 0 Then
Dim ListBox As New ListBox
ListBox.Style.Item("z-index") = 110
ListBox.Style.Item("position") = "right"
ListBox.Width = Unit.Pixel(150)
ListBox.Height = Unit.Pixel(150) For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
ListBox.Items.Add(New ListItem(ds.Tables(0).Rows(i).Item("studentID") & ds.Tables(0).Rows(i).Item("studentName"), ds.Tables(0).Rows(i).Item("studentID")))
Next
Dim stringwriter As New System.IO.StringWriter
Dim htmltextwriter As New HtmlTextWriter(stringwriter)
ListBox.RenderControl(htmltextwriter)
ret = stringwriter.ToString
End If
Catch ex As Exception
show_message.Text = ex.Message
End Try
End Sub
End Class
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src="js/System.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript"> function AddItemToTextBox() {
var selectElement = document.getElementById("select1");
document.getElementById("textbox1").value = selectElement.options[selectElement.selectedIndex].value;
selectElement.style.display = "none";
}
</script>
</head>
<body>
<form id="form1">
<select id="select1" size="5" onclick="AddItemToTextBox();">
<option value="0">aa</option>
<option value="1">bb</option>
<option value="2">cc</option>
<option value="3">dd</option>
<option value="4">ee</option>
<option value="5">ff</option>
</select>
<input type="text" id="textbox1" />
</form>
</body>
</html>