简体   繁体   English

在列表框中的文本框中显示文本-ASP.net&VB.net

[英]Displaying Text in a Text Box From Listbox - ASP.net & VB.net

ASP.net code (ListBox) ASP.net代码(列表框)

<asp:ListBox class="mailList" ID="ListBox1" runat="server" 
          SelectionMode="Multiple" >
          <asp:ListItem Value="0">Welcome to PVAL Portal</asp:ListItem>
          <asp:ListItem Value="1">Reminder</asp:ListItem>
          <asp:ListItem Value="2">User2</asp:ListItem>
</asp:ListBox>

ASP.net code (TextBox) ASP.net代码(文本框)

<asp:Label ID="Label3" runat="server" Text="Title:"></asp:Label>
<asp:TextBox ID="messageTitle" runat="server"></asp:TextBox>
<asp:TextBox ID="messageText" runat="server"TextMode="MultiLine" ></asp:TextBox>

VB.net code VB.net代码

Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim curItem As String = ListBox1.SelectedIndex()

    If curItem = 0 Then
        messageTitle.Text = "Testing1"
        messageText.Text = "Hello"
    ElseIf curItem = 1 Then
        messageTitle.Text = "Testing2"
        messageText.Text = "Hello2"
    ElseIf curItem = 2 Then
         messageTitle.Text = "Testing3"
        messageText.Text = "Hello3"
    End If
End Sub

When I run the program and select any item from Listbox, but it didn't show the text in text box. 当我运行程序并从列表框中选择任何项目时,它没有在文本框中显示文本。 I figure out a way to fix this problem by adding a button for it. 我想出一种方法来解决此问题,方法是为其添加一个按钮。 But i do't really like it. 但是我真的不喜欢。 Just want to know is there any method to show the text in text box right after i select one of the item from ListBox without using button? 我只是想知道有什么方法可以在不使用按钮的情况下从ListBox中选择一项后立即在文本框中显示文本?

thank you in advance and appreciate it. 在此先感谢您,并感谢您。

Add Autopostback =true in your html of asp.net listbox. 在asp.net的HTML列表框中添加Autopostback = true。 Because right now your listbox does not postback when you change selection. 因为现在您的列表框在更改选择时不会回发。

<asp:ListBox class="mailList" ID="ListBox1" runat="server" 
      SelectionMode="Multiple" AutoPostBack="true">
      <asp:ListItem Value="0">Welcome to PVAL Portal</asp:ListItem>
      <asp:ListItem Value="1">Reminder</asp:ListItem>
      <asp:ListItem Value="2">User2</asp:ListItem>
</asp:ListBox>

您应该在ListBox中将AutoPostBack属性设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM