使用选定索引上的项目填充组合框已更改 [英] filling a combobox with items on selected index changed

查看:53
本文介绍了使用选定索引上的项目填充组合框已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨好朋友,我用一个方法在事件选择索引上填充了一个组合框,但是我得到错误输入字符串的格式不正确而且它不能填充tectbox,

这里是我的代码

Hi pals,am filling a combobox with items from a method on event select index changed but i get the error input string was not in correct format and it cannot populate the tectbox,
here is my code

protected void StudentNameCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ManageStudents manageStudents = new ManageStdents(WebUtility.GetConnectionString);

            int StudentId =int.Parse(StudentNameCombobox.SelectedValue.ToString());
            var query =manageStudents.GetStudentsByStudentId(studentId);
            foreach (Students students in query)
            {
                Source3 nestudents = new Source3();
                newStudents.student = (students.StydentId).ToString();
                StudentIdCombobox.DataSource=manageStudents.GetStudentsByStudentId(StudentId);
                studentIdCombobox.DataTextField = "StudentId";
                studentIdCombobox.DataBind();
            }





组合框不会选择由该特定学生添加的学生ID,

Source3是一个包含getn set的newStudents的类,它是一个字符串



The combobox does not pick the students added by that particular studentId,,
Source3 is a class which contains newStudents by Get n set which is a string

推荐答案

你好b $ b

试试这样,



你不会得到错误,但会检查有效的选择ID。



如果所选学生id有效,它将执行剩下的代码,否则它将退出检查条件。







Hi
Try like this,

you wont get the error but it will check for the valid selected id.

if the selected student id is valid, it will execute the rest of code, else it will come out of the check condition.



int StudentId = 0;
            if (int.TryParse(StudentNameCombobox.SelectedValue + "", out StudentId))
            {
                var query = manageStudents.GetStudentsByStudentId(studentId);
                foreach (Students students in query)
                {
                    Source3 nestudents = new Source3();
                    newStudents.student = (students.StydentId).ToString();
                    StudentIdCombobox.DataSource = manageStudents.GetStudentsByStudentId(StudentId);
                    studentIdCombobox.DataTextField = "StudentId";
                    studentIdCombobox.DataBind();
                }
            }


我怀疑你选择的值不是一个有效的整数值意味着什么。调试时检查所选值实际是什么。使用int.TryParse()是一种更好的方法,因为它不会抛出异常,如果成功则返回bool,如果成功则返回out参数。
I suspect what it means is that your selected value is not a valid integer value. When debugging check what the selected value actually is. Using int.TryParse() is a better way because it doesn't throw exceptions, it returns a bool if successful and an out parameter has the value if successful.


这篇关于使用选定索引上的项目填充组合框已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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