0

我正在尝试制作一个简单的程序,将数据库中的数据放入 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>

我不知道我还能用这个做什么。

4

1 回答 1

1

因此,您想在最初显示您选择的 20 的名字,TextBox并且可能通过 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 函数。您可以使用Stringsplit然后:

var allNames = documentGetElementById("Names").value.split(","); 
于 2012-06-20T13:39:08.927 回答