2
votes

My Code for WinForm worked fine:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var Auswahl = listBox1.SelectedItem as Beobachter;
        textBox1.Text = Auswahl.BeobachterID.ToString();
        textBox2.Text = Auswahl.Vorname;
        textBox3.Text = Auswahl.Nachname;
    }

for WebForm it doesnt

    protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        var Auswahl = *ListBox2.SelectedItem as Person*; // This part fails

        txtID.Text = Auswahl.PID.ToString();
        txtVorname.Text = Auswahl.Vorname;
        txtNachname.Text = Auswahl.Nachname;
    }

Error Message: Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'WebApplication4.Person' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

How can I do it?

1
seems to be not possible link - Jan Drees

1 Answers

0
votes

In webforms the value of items in a dropdown can not be complete objects as in Winforms. The SelectedItem property is always of type ListItem and contains the string properties Text and Value. When binding the dropdown you need to set the Value of each item to a string identifier of the object, for example the Primary Key if your dropdown lists rows from a database.