组合框选择值 [英] Combo box selected value

查看:81
本文介绍了组合框选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个名为"cmb1"的组合框.

Hi frnds,

I got a combo box named "cmb1".

cmb1.datasource = dt


dt是我的数据表.

此绑定是在formload()方法中完成的.因此,一旦加载了表单,组合框就会显示在数据表dt中的所有可用项,而且我也得到了
加载形式中名为btn1的按钮控件.

在btn1 click事件中,我将组合框的选定值更改为


where dt is my data table.

This binding is done with in the formload() method. So once my form is loaded combobox is displayed with all items available in the data table dt and also i got a
button control named btn1 in the loaded form.

In btn1 click event i am changing the combo box selected value as,

cmb1.selectedvalue = 2;


此选定值更改已预订为我的cmb1选定值事件方法.在这种方法中,我只是获得选定的值


This selected value change is subscribed my cmb1 selected value event method. Inside this method i am just getting the selected value

int nSelectedValue = (int)cmb1.selectedValue;


现在的问题是,当我单击btn1时,如上所述,将所选值更改为2,然后执行了我的cmb1所选值更改方法.
但是在此选择的值更改方法内部,cmb1.selectedValue显示为-1而不是2.即使我选择的值为2并且我有10个以上的项,所选项目也显示为null.

请帮助解决此问题.


Now the issue is when i click btn1, as explained above selected value is changed to 2 and my cmb1 selected value change method is executed.
But Inside this selected value change method cmb1.selectedValue is showing as -1 instead of 2. Selected item is displayed as null even though my selected value is 2 and i got more than 10 items in it.

Please help to solve this

推荐答案

首次加载表时,它没有选定的项目,因此它会抛出一个索引为-1的SelectedValueChanged事件.如果当时没有该值,则也有可能将此值也返回为该值.

在事件处理程序的开始处,检查索引:
When you first load the table it has no selected items, so it throws a SelectedValueChanged event with an index of -1. It is likely that it also returns this as teh value if there is none at the time.

At the start of the event handler, check the index:
private void cbSource_SelectedValueChanged(object sender, EventArgs e)
    {
    ComboBox cb = sender as ComboBox;
    if (cb != null && cb.SelectedIndex >= 0)
        {
        ...
        }
    }


cockyrabbit!

您需要检查加载时页面是否为回发.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx [ ^ ]

Hi cockyrabbit!

You need to check if page is PostBack at the Load.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx[^]

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         cmb1.datasource = dt;
     }
}



您需要检查的其他内容是组合框EnableViewState属性.设置为true.



Other thing you need to check is the combobox EnableViewState prop. is set to true.

regards.


有时cmb1.selectedValue变为null.所以当我说
int nSelectedValue =(int)cmb1.selectedValue;它给了我错误.

感谢LittleBlackLui和OriginalGriff为您提供宝贵的解决方案.
非常感谢
sometimes cmb1.selectedValue becomes null. So when i say
int nSelectedValue = (int)cmb1.selectedValue; it gives me error.

Thanks LittleBlackLui and OriginalGriff for your valued solutions.
Thank you very much


这篇关于组合框选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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