简体   繁体   中英

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

ASP.net code (ListBox)

<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: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

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?

thank you in advance and appreciate it.

Add Autopostback =true in your html of asp.net listbox. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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