简体   繁体   English

如何从ASP.Net中的Class获取Listbox.selected项

[英]How to get Listbox.selected item from Class in ASP.Net

I've done a method that returns values from my "Person" class. 我已经完成了一个从我的“Person”类返回值的方法。 I'm now trying to get a specific value from that class by selected item in the list and then be able to use that value. 我现在试图通过列表中的选定项从该类中获取特定值,然后能够使用该值。 This is my code and the error i get is "Cannot convert type 'System.Web.UI.WebControls.ListItem to". 这是我的代码,我得到的错误是“无法将类型'System.Web.UI.WebControls.ListItem转换为”。 This is my Code: 这是我的代码:

       private Person aktuellPerson;

    protected void Page_Load(object sender, EventArgs e)
    {
        ListBoxPersoner.DataSource = Databasfunktioner.getPersoner();
        ListBoxPersoner.DataBind();
    }

    protected void ListBoxPersoner_SelectedIndexChanged(object sender, EventArgs e)
    {
      //Error below Cannot convert  type 'System.Web.UI.WebControls.ListItem to 'Testgrupp2.Person'
        aktuellPerson = (Person)ListBoxPersoner.SelectedItem;       
        TextBoxFornamn.Text = aktuellPerson.Fornamn;
    }

}

And this is myclass 这是myclass

public class Person
{
    public int PersonId { get; set; }
    public string Personnummer { get; set; }
    public string Fornamn { get; set; }
    public string Efternamn { get; set; }

    public override string ToString()
    {
        return Personnummer + " " + Fornamn + " " + Efternamn;
    }

}

The error is quite clear - SelectedItem returns a ListItem, not a Person. 错误很明显 - SelectedItem返回ListItem,而不是Person。 You should post the code for the ListBoxPersoner class but i suspect you have the person name in the ListItem Text property and the person ID in the Value property. 您应该发布ListBoxPersoner类的代码,但我怀疑您在ListItem Text属性中有人名,而Value属性中有人ID。 If you don't have the person name in the ListItem you could use the Value to find the corresponding Person. 如果ListItem中没有人名,则可以使用Value查找相应的Person。 Again this is just speculation since i don't know the implementation of ListBoxPersoner. 再次这只是推测,因为我不知道ListBoxPersoner的实现。

You cannot convert like this. 你不能这样转换。

you need to create a Person Object and assign to some property. 您需要创建一个Person对象并分配给某个属性。

protected void ListBoxPersoner_SelectedIndexChanged(object sender, EventArgs e)
    {
       Person p = new Person();
       p.Fornamn = ListBoxPersoner.SelectedItem.ToString();       
        TextBoxFornamn.Text = p.Fornamn;
    }

You have also to make some changes in your binding mecanism 您还必须对绑定机制进行一些更改

 private Person aktuellPerson;

protected void Page_Load(object sender, EventArgs e)
{
    ListBoxPersoner.DataSource = Databasfunktioner.getPersoner();
    ListBoxPersoner.DataValueField="PersonID";
    ListBoxPersoner.DataTextField="Fornamn";
    ListBoxPersoner.DataBind();
}

    protected void ListBoxPersoner_SelectedIndexChanged(object sender, EventArgs )
   {
       var temItem= sender as DropDownList;  // if you are talking about DropDownList
        TextBoxFornamn.Text = temItem.SelectedItem.Text;
    }

} }

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

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