Bonjour,
sur une page j'ai une ListBox. Je cherche à récupérer le texte de l'élément sur lequel je clique.
Voici mon code :
Code:
Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
TextBox1.text=ListBox1.SelectedItem.Text
End Sub
Voici la page en HTML :
Code:
<body background="..\images\CreamPbl.jpg">
<form runat="server">
<div align="center">
<asp:ListBox id="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
Lorsque je clique sur un élément, une erreur apparaît :
Citation:
Server Error in '/Annuaire' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 56:
Line 57: Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Line 58: TextBox1.text=ListBox1.SelectedItem.Text
Line 59: End Sub
Line 60:
Source File: D:\Inetpub\Annuaire\fr\ChoixSites.aspx Line: 58
Je pense qu'après le postback, il n'y a pas d'élément sélectionné, d'où l'erreur, car quand je teste avec seulement Code:
TextBox1.text=ListBox1.SelectedIndex
, j'ai le résultat -1 qui apparait.
Alors comment puis-je solutionner ce problème ?
Merci d'avance.
Raph