<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dev&#124;sushi &#187; annoyances</title>
	<atom:link href="http://devsushi.com/tag/annoyances/feed/" rel="self" type="application/rss+xml" />
	<link>http://devsushi.com</link>
	<description>Discussion on Development in Several Different Flavours</description>
	<lastBuildDate>Fri, 16 Apr 2010 01:06:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.Net DropDownList annoyance</title>
		<link>http://devsushi.com/2007/10/19/aspnet-dropdownlist-annoyance/</link>
		<comments>http://devsushi.com/2007/10/19/aspnet-dropdownlist-annoyance/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 21:26:58 +0000</pubDate>
		<dc:creator>Adam</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[annoyances]]></category>
		<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://adamhewgill.com/devsushi/2007/10/19/aspnet-dropdownlist-annoyance/</guid>
		<description><![CDATA[I recently ran into a problem with using the ASP.Net DropDownList for toggling the status of an element. The problem stems from how the ASP.Net page object works. In this article I present the problem and then show a solution that seems to work with some caveats. My goal is to create a toggle for [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a problem with using the ASP.Net DropDownList for toggling the status of an element. The problem stems from how the ASP.Net page object works. In this article I present the problem and then show a solution that seems to work with some caveats.<span id="more-51"></span></p>
<p>My goal is to create a toggle for the status of a page. For example I have a report and it has the possible status of <em>Open</em> or <em>Closed</em>. The DropDownList contains only the opposite of the current status of the report so when the report is <em>Open</em> the DropDownList contains an empty item (no change) and <em>Closed</em>. Here is the code.</p>
<pre class="brush: vb">&lt;script runat="server"&gt;
  Sub Page_Load()
    If Not Page.IsPostBack Then
      Session("bool") = "Open"
    End If

    ' Setup drop down
    ddlItems.Items.Clear()
    ddlItems.Items.Add("")
    If Not Session("bool") = "Open" Then ddlItems.Items.Add("Open")
    If Not Session("bool") = "Closed" Then ddlItems.Items.Add("Closed")
  End Sub

  Sub Click_Submit(ByVal s As Object, ByVal e As EventArgs)
    If ddlItems.SelectedValue = "Open" Then
      Session("bool") = "Closed"
    ElseIf ddlItems.SelectedValue = "Open" Then
      Session("bool") = "Closed"
    End If
  End Sub
&lt;/script&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Drop Down Annoyance&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;form runat="server"&gt;
  &lt;h1&gt;Drop Down Annoyance&lt;/h1&gt;
  &lt;p&gt;Current Status: &lt;%= Session("bool") %&gt;&lt;/p&gt;
  &lt;p&gt;Update to: &lt;asp:DropDownList ID="ddlItems" runat="server" /&gt;&lt;/p&gt;
  &lt;asp:Button ID="btnSubmit" Text="Update" OnClick="Click_Submit" runat="server" /&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>There are two problems at work here that cause a complete failure of the intended action. Firstly, during <em>Page_Load</em> the <em>Click_Submit</em> event has not yet fired so the Session variable still contains the old value of <em>Open</em>. The code then rebuilds the DropDownList based on the Session variable being unchanged. Secondly, doing the change of the DropDownList resets which option is selected loosing the users input.</p>
<p>We can fix both of these problems with one change to the code. Simply move the DropDownList change to the very last moment right before its declaration. Make sure to add all possible options to the declaration using ListItems, this is a must for it to work.</p>
<pre class="brush: vb">&lt;%@ Page Language="VB" Debug="True" %&gt;

&lt;script runat="server"&gt;

  Sub Page_Load()
    If Not Page.IsPostBack Then
      Session("bool") = "Open"
    End If
  End Sub

  Sub Click_Submit(ByVal s As Object, ByVal e As EventArgs)
    If ddlItems.SelectedValue = "Open" Then
      Session("bool") = "Open"
    ElseIf ddlItems.SelectedValue = "Closed" Then
      Session("bool") = "Closed"
    End If
  End Sub

&lt;/script&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Drop Down Annoyance&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;form runat="server"&gt;
  &lt;h1&gt;Drop Down Annoyance&lt;/h1&gt;
  &lt;p&gt;Current Status: &lt;%= Session("bool") %&gt;&lt;/p&gt;
  &lt;%
    ' Setup drop down
    ddlItems.Items.Clear()
    ddlItems.Items.Add("")
    If Not Session("bool") = "Open" Then ddlItems.Items.Add("Open")
    If Not Session("bool") = "Closed" Then ddlItems.Items.Add("Closed")
  %&gt;
  &lt;p&gt;Update to: &lt;asp:DropDownList ID="ddlItems" runat="server"&gt;
    &lt;asp:ListItem Text="" Selected="True" /&gt;
    &lt;asp:ListItem Text="Open" /&gt;
    &lt;asp:ListItem Text="Closed" /&gt;
  &lt;/asp:DropDownList&gt;&lt;/p&gt;
  &lt;asp:Button ID="btnSubmit" Text="Update" OnClick="Click_Submit" runat="server" /&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>What is going on here is that when ASP.Net creates the ddlItems object it loads it up with all the ListItems given in the declaration. These items are available throughout the code right up until the point where we Clear and then change the DropDownList. The Session variable has been changed by this time and so everything works out. One very important caveat of this method is that you must not use ddlItems.SelectedIndex since the number of options in the list changes, instead use ddlItems.SelectedValue.</p>
<p>Does anybody else have any novel solutions for working with DropDownList controls?</p>
]]></content:encoded>
			<wfw:commentRss>http://devsushi.com/2007/10/19/aspnet-dropdownlist-annoyance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ASP.Net CheckBoxes should be able to have values</title>
		<link>http://devsushi.com/2007/07/16/aspnet-checkboxes-should-be-able-to-have-values/</link>
		<comments>http://devsushi.com/2007/07/16/aspnet-checkboxes-should-be-able-to-have-values/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 19:06:33 +0000</pubDate>
		<dc:creator>Adam</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[annoyances]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[css/xhtml]]></category>
		<category><![CDATA[web standards]]></category>

		<guid isPermaLink="false">http://adamhewgill.com/devsushi/2007/07/16/aspnet-checkboxes-should-be-able-to-have-values/</guid>
		<description><![CDATA[The ASP.Net CheckBox and CheckBoxList control don&#8217;t allow for the use of the full W3C HTML standard. For some reason Microsoft decided to implement a reduced functionality version that creates more work for the programmer. Come on Microsoft why can&#8217;t you follow even the simplest of standards? In this case it would make coding the [...]]]></description>
			<content:encoded><![CDATA[<p></p>
<p>The ASP.Net <em>CheckBox</em> and <em>CheckBoxList</em> control don&#8217;t allow for the use of the full W3C HTML standard. For some reason Microsoft decided to implement a reduced functionality version that creates more work for the programmer. Come on Microsoft why can&#8217;t you follow even the simplest of standards? In this case it would make coding the .Net framework easier for you not to mention us!<span id="more-47"></span></p>
<p>The W3C HTML 4.01 specification specifically states for the <em>value</em> attribute to:</p>
<blockquote><p>
&#8211; Specify for radio buttons and checkboxes &#8211;<br />
<cite><a href="http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT">W3C HTML 4.01 Specification &#8211; INPUT Element</a></cite>
</p></blockquote>
<p>To me this quote reads as a strong recommendation to specify the <em>value</em> attribute rather than leave it up to the browsers default (generally &quot;On&quot;). Microsoft decided to not even allow the value attribute to be set. They went so far as to have a validation message built into Visual Studio that says:</p>
<blockquote><p>
Attribute &#8216;Value&#8217; is not a valid attribute of element &#8216;CheckBox&#8217;
</p></blockquote>
<p>If you ignore the warning and run the page, the <em>value</em> attribute is missing. I have even tried circumventing the ASP.Net HTML-izer by adding <em>value</em> to the attributes collection with no success. Microsoft intentionally strips it, so I can only assume they have some reasoning behind this behavior and I imagine they will say something about security in defense.</p>
<p>There is one way to circumvent the behavior but it requires not using ASP.Net controls. Simply insert traditional <em>input type=&#8221;checkbox&#8221;</em> elements into your page and collect their values using <em>Request.Form</em>.</p>
<pre class="brush: vb">Sub Page_Load ()
  Dim value As String
  value = Request.Form(&quot;chkItem&quot;)
End Sub
&hellip;
&lt;input type=&quot;checkbox&quot; id=&quot;chkItem&quot; name=&quot;chkItem&quot; /&gt;
&lt;label for=&quot;chkItem&quot;&gt;CheckBox Item Text&lt;/label&gt;
</pre>
<p>I made a big deal out of this issue simply because to me it doesn&#8217;t make any sense. You can get around the issue and still use ASP.Net controls but it requires extra coding.</p>
<pre class="brush: vb">Sub Page_Load ()
  Dim value As String
  If chkItem.Checked Then
    value = &quot;Some Value&quot;
  Else
    value = &quot;Some Other Value&quot;
  End If
End Sub
&hellip;
&lt;asp:CheckBox ID=&quot;chkItem&quot; Text=&quot;CheckBox Item Text&quot; runat=&quot;server&quot; /&gt;
</pre>
<p>When using the <em>CheckBoxList</em> control you will notice ASP.Net uses tables to layout the list. To keep it from doing this you simply need to add a few attributes. It should then use much more standards compliant code. I also show how to access which boxes are checked after postback in a <em>CheckBoxList</em>.</p>
<pre class="brush: vb">Sub Page_Load ()
  ' Find out which boxes were checked
  Dim value As String = &quot;&quot;
  For Each item As ListItem In cblTest.Items
    If item.Selected Then
      value &amp;= item.Value
    End If
  Next
End Sub
&hellip;
&lt;asp:CheckBoxList ID=&quot;cblList&quot; RepeatLayout=&quot;Flow&quot; RepeatDirection=&quot;Horizontal&quot; runat=&quot;server&quot;&gt;
  &lt;asp:ListItem Text=&quot;Item 1&quot; Value=&quot;1&quot; /&gt;
  &lt;asp:ListItem Text=&quot;Item 2&quot; Value=&quot;2&quot; /&gt;
  &lt;asp:ListItem Text=&quot;Item 3&quot; Value=&quot;3&quot; /&gt;
&lt;/asp:CheckBoxList&gt;
</pre>
<p>As always let me know if you have any questions.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://devsushi.com/2007/07/16/aspnet-checkboxes-should-be-able-to-have-values/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

