我正在尝试创建一个简单的程序,将数据从数据库放入javascript数组,然后在文本框中显示一个结果。这是我到目前为止所拥有的
Dim cmd As New IfxCommand("select first 20 fname from table", conn)
Dim reader As IfxDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim i As Integer = 0
While reader.Read()
ClientScript.RegisterArrayDeclaration("Names", "'" & reader("fname") & "'")
i += 1
End While
Dim cs As StringBuilder = New StringBuilder()
cs.Append("<script type=""text/javascript\""> function DoIt() {")
cs.Append("var TheTextBox = document.getElementById(""TextBox1"");")
cs.Append("TheTextBox.value = Names[0];")
cs.Append("script>")
TextBox1.Text = cs.ToString
这是asp
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" onclick = "DoIt"/>
</p>
</form>
</body>
</html>
我不确定我还应该做些什么。
答案 0 :(得分:1)
所以你想最初在TextBox
中显示你所选择的20的第一个名字,并且可能通过javascript从客户端延迟加载另一个名字?
试试这个:
Using cmd = New IfxCommand("select first 20 fname from table", conn)
Dim tbl = New DataTable
Using reader = cmd.ExecuteReader()
tbl.Load(reader)
If tbl.Rows.Count <> 0 Then
Dim allNames = From row In tbl
Select row.Field(Of String)("fname")
TextBox1.Text = allNames.First
' embed names with quotes and separate the strings by comma '
Dim embeddedNames = From name In allNames Select String.Format("'{0}'", name)
ClientScript.RegisterArrayDeclaration("Names", String.Join(",", embeddedNames))
End If
End Using
End Using
现在你只需要实现在必要时切换名称的javascript函数。您可以使用String split
:
var allNames = documentGetElementById("Names").value.split(",");