我正在制作一个网站(这方面的菜鸟),并试图将搜索结果添加到三个 div。但是从数据库向 div 添加文本时会出现问题。它正确获取。但我想将一个字符串(总共三个 div,id 分别为 div1、div2、div3。字符串为“div”+ DivCount.ToString,DivCount 为 0(循环并在每个循环中添加 +1)转换为命令。这是代码,任何其他方法也将不胜感激。谢谢。代码在下面,顺便说一句。
默认.aspx.vb
Imports Microsoft.VisualBasic
Imports System.Data.SqlServerCe
Partial Class Pages_Default
Inherits System.Web.UI.Page
'Project Variables
Dim ProjectDirectory As String = AppDomain.CurrentDomain.BaseDirectory.ToString
'ConnectAndAddItemsToDropdownList Dim
Dim SQLCommandString As String
Dim SQLConnectionString As String = "Data Source=" + ProjectDirectory + "App_Data\MainDatabase.sdf; Persist Security Info=False;"
Dim SQLConnection As New SqlCeConnection(SQLConnectionString)
Dim SQLAdapter As SqlCeDataAdapter
Dim SQLReader As SqlCeDataReader
Dim Executor As New MSScriptControl.ScriptControl
Dim DivCount As Integer = 0
Dim DivCurrent As String
Public Sub ConnectAndAddItemsToDropdownList()
DropDownList1.Items.Clear()
SQLConnection.Open()
SQLCommandString = "SELECT BookName FROM [Anthony Horowitz] WHERE (BookName LIKE '" + SearchTextBox.Text + "%')"
Dim SQLCommand As New SqlCeCommand(SQLCommandString, SQLConnection)
SQLReader = SQLCommand.ExecuteReader()
While SQLReader.Read()
DivCount = +1
DivCurrent = "div" + DivCount.ToString
Executor.Language = "VBScript"
Executor.Eval(DivCurrent.ToString + ".InnerText = SQLReader(""Bookname"")")
End While
SQLConnection.Close()
End Sub
Protected Sub SearchTextChanged(sender As Object, e As EventArgs) Handles SearchTextBox.TextChanged
ConnectAndAddItemsToDropdownList()
End Sub
End Class
默认.aspx
<%@ Page Language="VB" CodeFile="Default.aspx.vb" Inherits="Pages_Default" AutoEventWireUp="False"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body id="MainPage" runat="server">
<form id="form1" runat="server">
<asp:TextBox ID="SearchTextBox" Text="Search for a book here..." runat="server" AutoPostBack="true" OnTextChanged="SearchTextChanged"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="105px"></asp:DropDownList>
<div id="div1" runat="server">Nothing Interesting</div>
<div id="div2" runat="server">Nothing Interesting</div>
<div id="div3" runat="server">Nothing Interesting</div>
</form>
</body>
</html>
上面的结果是“需要对象:'div1'”