在ASP&中动态地将文本文件添加到DDL VB

时间:2014-08-26 12:47:57

标签: asp.net vb.net

我希望通过动态更新来更新我的DDL功能之一,所以如果用户添加更多文件,下拉菜单会选择这个。

目前我的下拉列表正在从VB代码中拉出来,如下所示:

    Public Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim ddl As DropDownList = CType(sender, DropDownList) 'item is already dropdownlist
    Dim ctl As TextBox = DirectCast(ddl.NamingContainer.FindControl("eTemplate"), TextBox)
    If ddl.SelectedValue = 1 Then
        ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Down.txt")
    ElseIf ddl.SelectedValue = 2 Then
        ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Up.txt")

    Else
        ctl.Text = ""
    End If
End Sub

目前我已经在VB的功能中硬编码以获取特定的.txt文件,如何从.txt文件的文件夹中动态更新?

感谢您的光临。

1 个答案:

答案 0 :(得分:3)

以下是一些示例代码。此演示使用UpdatePanelTimer每5秒刷新一次DropdownList。

将新的aspx文件添加到Web应用程序和以下代码:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Zpk_Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Asynchronous Update Demo</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1" />
        <asp:UpdatePanel runat="server" ID="UpdatePanel1">
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" /><br />
                <asp:Timer runat="server" ID="Timer1" Interval="5000" Enabled="true" />
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="DropDownList1" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300" Height="250"  />
    </form>
</body>
</html>

这是代码隐藏:

Partial Class Demo
    Inherits System.Web.UI.Page

    Private Const FolderName As String = "C:\Temp"   '<-- replace with your folder name

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            RefreshDropDownList()
            OpenSelectedFile()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' this event is fired everytime a timer ticks. 
        ' refresh your dropdown list here.
        RefreshDropDownList()
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        OpenSelectedFile()
    End Sub

    Private Sub RefreshDropDownList()
        Dim currentSelected As String = DropDownList1.SelectedValue
        DropDownList1.DataSource = IO.Directory.GetFiles(FolderName, "*.txt").Select(Function(f) IO.Path.GetFileName(f)).ToList
        DropDownList1.DataBind()
        DropDownList1.SelectedValue = currentSelected
    End Sub

    Private Sub OpenSelectedFile()
        Dim fileName As String = IO.Path.Combine(FolderName, DropDownList1.SelectedValue)
        TextBox1.Text = IO.File.ReadAllText(fileName)
    End Sub
End Class