By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,966 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,966 IT Pros & Developers. It's quick & easy.

Default value for dropdownlist databound control

P: 11
I have a dropdownlist databound to table column with string values "n" i "y".

<asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'>
<asp:ListItem Value="n">No</asp:ListItem>
<asp:ListItem Value="y">Yes</asp:ListItem>

How to handle situation when value is not "n" or "y" to avoid exception.
Is it possible to make dropdownlist select some default value when binded value does not fall in range. (e.g. "")

Jul 27 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 2.5K+
P: 4,871
Welcome to TSDN. I believe your choice is to validate it, client or server side, or to test for a null value. HTH.
Jul 28 '07 #2

Expert 100+
P: 759
Set its 'DropDownStyle' property to 'DropDownList'.
Jul 28 '07 #3

P: 11
Well I wanted to make sure that if 'cPrivate' column value different than 'y' or 'n' I don't get binding exception at presentation layer. But decided that I can simply modify my sql query:
(SELECT cPrivate = CASE WHEN cPrivate = 'Y' THEN 'Y' ELSE 'N' END) AS cPrivate

This way possible wrong values in database are corrected at data layer.
Otherwise I should somehow catch binding exception (still don't know how) which would anyway require more code and effort.

Jul 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.