【问题标题】:ASP textbox template controlled from drop down从下拉菜单控制的 ASP 文本框模板
【发布时间】:2014-09-30 15:23:08
【问题描述】:

我正在尝试获取一个下拉菜单以将模板插入文本框,理想情况下不要使用数据库,只是通过事件或类似的硬代码,除非我可以通过数据库保持样式(字体颜色等)。

文本框和下拉列表位于updatepanel 中的listview 中。我想添加更多功能,这样他们就不必在文本框中手动输入文本,当他们从下拉列表中选择一个选项时,它会自动使用相关选项填充文本框。

我不知道如何将下拉列表中的onClick 功能与文本框相关联。

任何指南或帮助将不胜感激。

我的asp代码:

<asp:panel runat="server" ID="Panel1">
 <table id="insert" runat="server">
    <tr style="">
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
        </td>
    </tr>
    <tr >
        <td>
            <asp:TextBox ID="TxtBx" runat="server" Text='<%# Bind("Details")%>' Width="100%" TextMode="MultiLine" Rows="20" />
        </td>
        <td>
            <asp:DropDownList ID="DDL" runat="server" AutoPostBack="True">
                <asp:ListItem>Select</asp:ListItem>
                <asp:ListItem> Template One </asp:ListItem>
                <asp:ListItem> Template Two </asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
</table>

背后的VB.NET代码:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If DDL.Text <> "Select" Then
        TxtBx.Text = DDL.Text
    End If
End Sub

    Protected Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDL.SelectedIndexChanged
    TxtBx.Text = DDL.Text
End Sub

更新:

我已经使用 VB 将 ddl 粘贴到文本框中,是否可以获取带有模板的 .txt 文件(里面只有文本)并将其粘贴到文本框中。那么,DDL Item 模板 1 链接到 textfile1.txt,如果选择模板 1,会粘贴对应的文本文件的文本吗?

更新 2:

当单击 ddl 时,我已将事件处理程序粘贴到文本框中,但是我不知道如何从下拉列表中引用一个项目以引用特定文件,因此它会在项目选择上上传正确的文件.

处理程序代码:

    Protected Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDL.SelectedIndexChanged
    'TxtBx.Text = DDL.Text
    TxtBx.Text = My.Computer.FileSystem.ReadAllText("E:\Users\me\DummyTemplate.txt")
End Sub

【问题讨论】:

  • 请更清楚地解释您的问题,您想要做什么,而这段代码中没有完成?例如,你想在OnSelectedIndexChanged 上更改TextBox 的背景颜色吗?
  • 我正在尝试实现的:与listItems关联的模板,选中后将填充与所述模板的文本框。 span>

标签: asp.net vb.net drop-down-menu


【解决方案1】:

您需要在 ASP 中使用值,以便后面的代码可以将其与此关联并选择模板文件,但是我不知道这是否会提供您需要的布局。

使用 VS2013 在 Chrome 上进行了尝试和测试

平均售价:

        <asp:DropDownList ID="DDL" runat="server" Width="80px" AutoPostBack="true" >
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem Value="1">Option 1</asp:ListItem>
            <asp:ListItem Value="2">Option 2</asp:ListItem>
        </asp:DropDownList>

VB 背后的代码:

        Protected Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDL.SelectedIndexChanged
    If DDL.SelectedValue = 1 Then
        TxtBx.Text = My.Computer.FileSystem.ReadAllText("E:\Users\ben\DummyTemplate.txt")
    ElseIf DDL.SelectedValue = 2 Then
        TxtBx.Text = My.Computer.FileSystem.ReadAllText("E:\Users\ben\DifferentTemplate.txt")
    End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多