开发者

check if a values has been selected from dropdown in c#

开发者 https://www.devze.com 2023-01-11 13:11 出处:网络
I have 3 dropdown boxes (combo box) in asp.net environment. They are all optional开发者_开发技巧, so if a user has selected anything, i am updating database, if nothing has been selected at all, i am

I have 3 dropdown boxes (combo box) in asp.net environment. They are all optional开发者_开发技巧, so if a user has selected anything, i am updating database, if nothing has been selected at all, i am still updating database with null values.

I tried to do this:

 int? CountryId = Convert.ToInt32(ddCountries.SelectedItem.Value);

I was hoping that if nothing is selected null will be inserted in CountryId, but, instead its throwing an exception.

I tried to search for ddCountries.isSelected (or something like that) but it obviously doesnt exist..

so how do I find out if a selection has been made on a dropdown box? - through c# code.

Many Thanks

ps: I have a thought - i put each dropdown box in a try... catch block and if exception arises, set variables to null manually.. but I am not sure thats the best way to do it!


You're looking for

if(ddCountries.SelectedIndex > -1)

You should never be using exceptions to control program flow.


ddCountries.SelectedIndex > 0;

This worked for me, assuming that you have an empty item on index 0 that you have inserted.

ddCountries.Items.Insert(0, new ListItem(string.Empty, string.Empty));

ddCountries.SelectedIndex = 0;


You can use this:

If ComboBoxChannel.SelectedValue.ToString.ToLower = "system.data.datarowview"
Then Exit Sub

Please note that it is in VB.Net

0

精彩评论

暂无评论...
验证码 换一张
取 消