asp.net C # combobox SelectedValue detailed

Source: Internet
Author: User
Tags first row

First, test several modules to add the following lines separately:

int id = convert.toint32 (cbocategory.selectedvalue); MessageBox.Show (id.tostring ());
1. Add in Cbocategory_selectedindexchanged () cbocategory_selectedvaluechanged (), the compilation cannot pass, the error is as follows:

An object of type ' myproject.model.category cannot be cast to type ' system.iconvertible '.

2. In normal method or Load event, add, you can normally pass.

(Original content, Elig starting in the blog park, reproduced please indicate the source)

The test code then changes to the following:

Category id = (category) (Cbocategory.selectedvalue); if (id = = null) {MessageBox.Show ("Test");}
1. Call in normal method, the first line can be compiled correctly, id = NULL condition set

2. In the Load event, as well as the Selectionchangecommitted,selectedindexchanged,selectedvaluechanged incident, the error is as follows:

An object of type ' System.Int32 cannot be cast to type ' myproject.model.category '.

Finally, test the following code:

MessageBox.Show (Cbocategory.selectedindex.gettype (). ToString ()); string str = Cbocategory.selectedvalue.tostring () ; MessageBox.Show (str); int id = Convert.ToInt32 (str); MessageBox.Show (id.tostring ());
The normal method of 1.load events and the SelectionChangeCommitted event can be expected.

Type:system.int32

2. And in the Selectionchangecommitted,selectedindexchanged incident

Type:system.int32

str = myproject.model.category

Error after turning int

(Original content, Elig starting in the blog park, reproduced please indicate the source)

Although there was no way to figure out where the problem was, the solution was the one mentioned above, where the code needed to be implemented in the SelectionChangeCommitted event would normally produce the desired results.

After the project spent a little time to study the problem carefully, because the same code in different places but there will be such a rich and colorful results, it is hard to resist.

Finally, we found that if we were to add the test code to the if in either of the selectedindexchanged,selectedvaluechanged events:


View Sourceprint?1


if (Cbocategory.selectedindex >= 1) {//test code}

So all 3 events can be compiled correctly.

Note: The problem is on this Red 1, in the selectedindexchanged,selectedvaluechanged, can only be judged to index >= 1

Some people will ask, ComboBox's index does not start from 0?

Very right!

But as long as the condition is changed to >= 0 then the form load must be an error.

Let's take a look at the difference between these 3 events:

Selectedindexchanged,selectedvaluechanged in any case, the corresponding event is triggered whenever a change is made to the value they are monitoring (index or ValueMember)

As we all know, these 2 values inevitably change when the control is generated. In other words, each change triggers the event.

The SelectionChangeCommitted event, the official translation explanation, occurs when the Drop-down list closes when you select an item from the Drop-down list.

But actually this translation is inaccurate, because I am not in the English vs environment, so I do not know how the original, but in fact, the event of personal understanding, more accurate translation is that the user to the control of the selection to make any changes occurred.

This "Any" includes: the mouse clicks, obtains the focal point the direction key to choose up and down.

(Original content, Elig starting in the blog park, reproduced please indicate the source)

In my question, selectedindexchanged,selectedvaluechanged does not work correctly because the same code cannot handle the item of index = 0 in both events. or the value of selectedvalue is ambiguous. Starting with index = 1, the selectedvalue (int) conversion is normal. But because these two events are monitored by the system's changes to the ComboBox control, it is inevitable that the event will be triggered when the first row is generated, resulting in exposure to the rows of the abnormal index = 0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.