asp.net - Asp 将查询放入数组并在不刷新页面的情况下通过它

标签 asp.net sql database vb.net informix

我将如何着手在 asp 中运行查询并将其存储在数组中。然后用数组遍历每条记录,而不必每次都刷新页面。

例如

Sub loaddata()
    Dim conn As New IfxConnection(connectionstring)

    Dim results() As String
    Dim i As Integer = 0

    conn.ConnectionString = connectionstring
    Try
        conn.Open()
        MsgBox("Made connection!")

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Dim cmd As New IfxCommand("Select name from table", conn)
    Dim reader As IfxDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    While reader.Read()
        results(i) = reader("name")
        i += 1

    End While
    reader.Close()
    conn.Close()
End Sub

结果存储在results(i)中

假设我有一个按钮,每次我都希望它转到结果数组中的下一个项目。我不希望页面刷新。我该怎么做?

最佳答案

您需要考虑如何设置您的页面。有很多方法可以做到这一点。

如果您获得页面加载上的所有记录,则可以将它们分配给各自的控件。如果它们在许多页面上,您可以使用 MultiView 控件来使其显示。

标记

<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
   <asp:View ID="View1" runat="server"> 
      <asp:Label ID="lblRecordOne" runat="server" />
   </asp:View>
   <asp:View ID="View2" runat="server"> 
      <asp:Label ID="lblRecordTwo" runat="server" />
   </asp:View>         
</asp:MultiView>
<asp:Button ID="btNext" runat="server">Next</asp:Button>

代码隐藏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     'Call your method that loads the data to results
     Dim results() As String = loaddata()
     'Set the data
     lblRecordOne.Text = results(0)
     lblRecordTwo.Text = results(1)
End Sub

Protected Sub btNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btNext.Click

 'There is a much better way to go about this, but for this 
 'demos sake here is the basic idea
 MultiView1.SetActiveView(View2)

End Sub

您要记住的是,每个 View 控件在 MultiView 中都有一个索引。因此,您需要一种方法来跟踪事件 View 是什么。当用户单击下一步按钮时,您将 View 前进一个。

在 MVC 应用程序方面

Controller

public class HomeController 
{
   // /Home/GetRecord/{id}
   public ViewResult GetRecord(int? id)
   {
        int modelID;
        if (id == null) { modelID = 0; }
        ViewBag.CurrentID = modelID;
        // This will send the value to the view
        // I would refactor loaddata to only return one record at a time
        // based on an id or an index
        return View(loaddata(modelID));
   }
}

查看

<label>@Model.Result</label>
@Html.ActionLink("GetRecord", "Home", new { id = ViewBag.CurrentID + 1 })

每次您单击 ActionLink 时,它都会通过一个人设置的记录。

如果第一条记录是Hello,第二条记录是World:

/Home/GetRecord/0 - 你好

/Home/GetRecord/1 - 世界

抱歉,我将其切换为 C#,我更喜欢徒手编写 C# 而不是 VB。 VB 中的代码非常相似。如果需要转换器就去http://converter.telerik.com/ .

关于asp.net - Asp 将查询放入数组并在不刷新页面的情况下通过它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017041/

相关文章:

asp.net - 启用并强制执行 SSL 但允许独立的 ASPX 页面使用 SSL

c# - 无法将 "void"隐式转换为 "bool"

c# - ASP.NET 5、MVC 6、Web API -> ModelState.IsValid 始终返回 true

sql - DBMS_XPLAN.DISPLAY_CURSOR 与未使用gather_plan_statistics 提示的解释计划

java - 计算 SQLite 数据库中表的数量

mysql - 使用哪种数据类型来存储用户数据?

asp.net - 未将对象引用设置为构建网站上的对象实例

javascript - 传递 while 循环内部的输入类型标记的值,其中 while 循环位于 php 内部

database - Cassandra 中的映射冗余

database - 如何在 Flutter Floor 中实际查看数据库?