将值从列表框传递到对象 [英] Passing the value from a listbox to a object

查看:82
本文介绍了将值从列表框传递到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我很难将值从列表框发送到对象.lboxStudentIDs包含4个值.
1111
2222
3333
4444

当我运行调试器时,我看到"SelectedValue"是正确的,但是aStudent仍然为空.列表框中的值是整数.任何帮助表示赞赏.
谢谢!

Hi All,
I am having a little difficulty sending the value from a listbox to an object.The lboxStudentIDs contains 4 values.
1111
2222
3333
4444

When I run the debugger I see that the "SelectedValue" is correct BUT aStudent remains null. The values in the listbox are integers. Any help is appreciated.
Thanks!

SchoolIDS aStudent  = lboxStudentIDs.SelectedValue as SchoolIDS;

推荐答案

假定ASP.NET ...

SelectedValue是字符串类型.字符串不存储StudentIDS类型的实例.我猜您正在存储ID的字符串(例如"1111").将那个SelectedValue转换为一个整数,然后使用该整数在存储学生的任何地方查找您的学生(我正在数据库中猜测).

仅供参考,如果您尝试使用"as"关键字强制转换的实例类型与您尝试强制转换的实例类型不匹配,则将返回null.这就是为什么您看到null的原因(因为字符串不是StudentIDS类型).
Assuming ASP.NET...

SelectedValue is of type string. Strings do not store instances of type StudentIDS. I''m guessing you are storing strings of the ID''s (e.g., "1111"). Convert that SelectedValue to an integer, then use that integer to look up your student wherever you are storing students (I''m guessing in a database).

FYI, if the type of instance you are attempting to cast using the "as" keyword does not match the type you are trying to cast to, null will be returned. That''s why you are seeing null (because strings aren''t of type StudentIDS).


这篇关于将值从列表框传递到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆