{"id":548,"date":"2003-12-19T08:06:31","date_gmt":"2003-12-19T12:06:31","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=548"},"modified":"2003-12-19T08:06:31","modified_gmt":"2003-12-19T12:06:31","slug":"accessing-aspnet-dropdownlist-items-from-code-behind","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/12\/19\/accessing-aspnet-dropdownlist-items-from-code-behind\/","title":{"rendered":"Accessing ASP.NET DropDownList Items from Code behind"},"content":{"rendered":"<p>Couple days ago I was writing an ASP.NET webform and I wanted to programmatically inspect the items in the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemWebUIWebControlsDropDownListClassTopic.asp?frame=true\">drop down<\/a>.  The drop down list in the ASP.NET page looks something like this:<br \/>\n<code><br \/>\n&lt;asp:dropdownlist id=\"mydropdown\" class=\"dropbox\" runat=\"server\"&gt;<br \/>\n&nbsp;&nbsp;&lt;asp:listitem text=\" -- select -- \" value=\"\" \/&gt;<br \/>\n&nbsp;&nbsp;&lt;asp:listitem text=\"Text 1\" value=\"1\" \/&gt;<br \/>\n&nbsp;&nbsp;&lt;asp:listitem text=\"Text 2\" value=\"2\" \/&gt;<br \/>\n&nbsp;&nbsp;&lt;asp:listitem text=\"Text 3\" value=\"3\" \/&gt;<br \/>\n&lt;\/asp:dropdownlist&gt;<br \/>\n<\/code><br \/>\nand then in the code behind (like <a href=\"http:\/\/cephas.net\/blog\/2003\/12\/04\/why_doesnt_aspnet_include_an_aspquery_tag.html#000603\">I mentioned before<\/a>) you can do a bunch of useful things.  First, if I wanted to set the default selected item in the form, I&#8217;d write this:<br \/>\n<code><br \/>\npublic void Page_Load(Object sender, EventArgs e) {<br \/>\n&nbsp;&nbsp;\/\/ initialize form<br \/>\n&nbsp;&nbsp;if (!IsPostBack) {<br \/>\n&nbsp;&nbsp;&nbsp;mydropdown.SelectedValue = \"1\";<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nThat&#8217;s relatively trivial, it gets more interesting when you consider that you can access the DropDownList <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemwebuiwebcontrolslistcontrolclassitemstopic.asp\">items<\/a> programatically:<br \/>\n<code><br \/>\nforeach (ListItem l in mydropdown.Items) {<br \/>\n&nbsp;&nbsp;if (l.Value == 1) {<br \/>\n&nbsp;&nbsp;&nbsp;l.Selected = true;<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nIn the above example I use the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfTheForeachStatement.asp\">foreach<\/a> statement to iterate through the collection of ListItems in the Items property.  If the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemWebUIWebControlsListItemClassValueTopic.asp?frame=true\">Value property<\/a> of the ListItem is 1, then I set that to be selected.  You can also access the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemWebUIWebControlsListItemClassTextTopic.asp?frame=true\">Text property<\/a> of the List Item.<\/p>\n<p>The C# foreach construct jogged my memory about Java 1.5 (Tiger) which is supposed to have <a href=\"http:\/\/java.sun.com\/features\/2003\/05\/bloch_qa.html\">enhanced &#8216;for&#8217; loops<\/a> (also read <a href=\"http:\/\/www.onjava.com\/pub\/a\/onjava\/2003\/09\/24\/readable_java.html\">this<\/a>); for example this:<br \/>\n<code><br \/>\nfor (Iterator i = c.iterator(); i.hasNext(); ) {<br \/>\n&nbsp;&nbsp;\/\/ do stuff with 'i'<br \/>\n}<br \/>\n<\/code><br \/>\nbecomes this:<br \/>\n<code><br \/>\nfor (Object o : c) {<br \/>\n&nbsp;&nbsp;\/\/ do stuff w\/ 'o'<br \/>\n}<br \/>\n<\/code><br \/>\nWhen is 1.5 gonna arrive?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Couple days ago I was writing an ASP.NET webform and I wanted to programmatically inspect the items in the drop down. The drop down list in the ASP.NET page looks something like this: &lt;asp:dropdownlist id=&#8221;mydropdown&#8221; class=&#8221;dropbox&#8221; runat=&#8221;server&#8221;&gt; &nbsp;&nbsp;&lt;asp:listitem text=&#8221; &#8212; select &#8212; &#8221; value=&#8221;&#8221; \/&gt; &nbsp;&nbsp;&lt;asp:listitem text=&#8221;Text 1&#8243; value=&#8221;1&#8243; \/&gt; &nbsp;&nbsp;&lt;asp:listitem text=&#8221;Text 2&#8243; value=&#8221;2&#8243; \/&gt; &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/12\/19\/accessing-aspnet-dropdownlist-items-from-code-behind\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Accessing ASP.NET DropDownList Items from Code behind<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/548"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=548"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/548\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}