﻿<?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>Programming By A Tool &#187; UI</title>
	<atom:link href="http://byatool.com/category/ui/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>Voted best site in existence by a top fictious rating site!</description>
	<lastBuildDate>Wed, 28 Jul 2010 18:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</title>
		<link>http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/</link>
		<comments>http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 16:05:18 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Modal Dialog]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1213</guid>
		<description><![CDATA[This is quick one, so hold on to your... whatever. Want to get rid of that X at the top right of the modal "Control"? Well here it is: (The bold part, moron) jQuery('#WaitingDiv').dialog({ autoOpen: false, bgiframe: false, height: 150, width: 200, modal: true, open: function(event, ui) { jQuery('.ui-dialog-titlebar-close').hide(); } }); And that's really it. [...]]]></description>
			<content:encoded><![CDATA[<p>This is quick one, so hold on to your... whatever.</p>
<p>Want to get rid of that X at the top right of the modal "Control"?  Well here it is:  (The bold part, moron)</p>
<pre>     jQuery(<span style="color: #800000;">'#WaitingDiv'</span>).dialog({
          autoOpen: false,
          bgiframe: false,
          height: 150,
          width: 200,
          modal: true,
         <strong> open: function(event, ui) { jQuery(<span style="color: #800000;">'.ui-dialog-titlebar-close'</span>).hide();</strong> }
        });
</pre>
<p>And that's really it.  Just thought I'd pass that on to you, the consumer.</p>
<p>On a side note, the big movie thing is the <a href=" http://www.huffingtonpost.com/2010/01/11/spider-man-4-off-spider-m_n_419264.html">canceling of Spiderman 4 or at least going in a new direction</a>.  As much as I don't care, it might lead to my dream come true:</p>
<p>A spiderman movie based on the SNES classic: Spider-Man &amp; Venom: Maximum Carnage.  Uwe Boll, are you out there?</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-check-all-checkboxes-toggle-method-thingy/" title="jQuery &#8211; Check All Checkboxes Toggle Method Thingy">jQuery &#8211; Check All Checkboxes Toggle Method Thingy</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery &#8211; Check All Checkboxes Toggle Method Thingy</title>
		<link>http://byatool.com/ui/jquery-check-all-checkboxes-toggle-method-thingy/</link>
		<comments>http://byatool.com/ui/jquery-check-all-checkboxes-toggle-method-thingy/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 23:20:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1203</guid>
		<description><![CDATA[So here's a quick on for all you boys and girls... and anything else I might have missed. Say you want to check all check boxes in a grid or whatever, but you want it to toggle. So if if it's time to check all, all get checked and then the next time the button [...]]]></description>
			<content:encoded><![CDATA[<p>So here's a quick on for all you boys and girls... and anything else I might have missed.<br />
Say you want to check all check boxes in a grid or whatever, but you want it to toggle.  So if if it's time to check all, all get checked and then the next time the button is clicked it will uncheck.  Now you could do this in three methods, maybe two but that's why you're a mouth breather.</p>
<p>Special people like me do it in one.</p>
<pre>    <span style="color: #0000ff;">function</span> toggleCheckboxes(buttonId, checkBoxNamePrefix, checked)
    {
      <span style="color: #008000;">//Get all checkboxes with the prefix name and check/uncheck</span>
      jQuery(<span style="color: #800000;">'[id*=' + checkBoxNamePrefix + ']'</span>).attr(<span style="color: #800000;">'checked'</span>, checked);

      <span style="color: #008000;">//remove any click handlers from the button</span>
      <span style="color: #008000;">//  Why?  Because jQuery(buttonId).click() will just add another handler</span>
      jQuery(buttonId).unbind(<span style="color: #800000;">'click'</span>); 

      <span style="color: #008000;">//Add the new click handler</span>
      jQuery(buttonId).click(
        <span style="color: #0000ff;">function</span>() {
          toggleCheckboxes(buttonId, checkBoxNamePrefix, !checked);
        }
      );
    }</pre>
<p>And POW!  What?  You want to know how to use it?  Fine, but only because I'm nice.</p>
<pre>   jQuery(<span style="color: #800000;">'#buttonName'</span>).click(
     <span style="color: #0000ff;">function</span>() {
       toggleCheckboxes(<span style="color: #800000;">'#buttonName'</span>,<span style="color: #800000;"> 'someCheckBoxName'</span>, <span style="color: #0000ff;">true</span>);
     }
   );</pre>
<p>And in case you were still wondering why I used .unbind and didn't understand the comment, it's because using just .click will add another handler to the button. So next click you will get both check and uncheck all.  Kind of hurts the effectiveness.</p>
<p>And there you have it.  Once again I have filled your life with meaning.  Couldn't be easier.  Or maybe it could but that's not my problem now is it?  Go back to doing something useful.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-check-all-checkboxes-toggle-method-thingy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery Grab Bag &#8211; Whole bunch of jQuery Functionality</title>
		<link>http://byatool.com/ui/jquery-grab-bag-whole-bunch-of-jquery-functionality/</link>
		<comments>http://byatool.com/ui/jquery-grab-bag-whole-bunch-of-jquery-functionality/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 21:06:32 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1125</guid>
		<description><![CDATA[You could look at this post as a self serving block that I will use to save stuff I don't want to remember later. Or you could see it as a knowledge transfer that you the reader can learn from because I am a generous and giving tool. Whatever, I really don't care. I won't [...]]]></description>
			<content:encoded><![CDATA[<p>You could look at this post as a self serving block that I will use to save stuff I don't want to remember later.</p>
<p>Or you could see it as a knowledge transfer that you the reader can learn from because I am a generous and giving tool.</p>
<p>Whatever, I really don't care.  I won't sleep any differently.  I just don't feel like making 1000 posts of jquery stupidity.</p>
<p>I'll add more as I come across them.</p>
<h2>Checkbox</h2>
<p><strong>How To Check a CheckBox</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#CheckBoxId'</span>).attr(<span style="color: #800000;">'checked'</span>, <span style="color: #0000ff;">false</span>);</pre>
<p><strong>How To Know if a Checkbox is Checked</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#CheckBoxId'</span>).is(<span style="color: #800000;">':checked'</span>)</pre>
<p><strong>How To Find a Bunch of Like Named Checkboxes that are Checked</strong></p>
<pre>  jQuery(<span style="color: #800000;">'[id*=someCheckBox_]:checked'</span>);</pre>
<h2>Drop Down Lists</h2>
<p><strong>How to Add an Item/Option to a Drop Down List</strong></p>
<pre> jQuery(<span style="color: #800000;">'#DropDownListId'</span>).append(jQuery(<span style="color: #800000;">'&lt;option&gt;'</span>).val(1).html(<span style="color: #800000;">"SomeText"</span>));</pre>
<p>Odd Note:  Apparently jquery is smart enough to set the attributes on the option AND close the option with &lt;/option&gt;</p>
<p><strong>How to Remove an Item/Option from a Drop Down List</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#DropDownListId option[value=\'3\']'</span>).remove();</pre>
<p><strong>How to Sort a Drop Down List by Option Text</strong></p>
<pre>  <span style="color: #0000ff;">var</span> sortedList = jQuery(<span style="color: #800000;">'#someDropDownList option'</span>).sort(
    <span style="color: #0000ff;">function</span> (first, second) {
      <span style="color: #0000ff;">return</span> first.text == second.text ? 0 : first.text &lt; second.text ? -1 : 1;
    }
  );

  jQuery(<span style="color: #800000;">'#someDropDownList'</span>).html(sortedList);</pre>
<h2>Elements</h2>
<p><strong> How to Filter Out an Element in a List</strong></p>
<pre>  someElementList.filter(<span style="color: #800000;">'[id=someId]'</span>);</pre>
<p>Example:</p>
<pre><span style="color: #008000;">  //Uncheck all id like someId_ and check only where id = someId_1</span>
<span style="color: #0000ff;">  var</span> someElementList = someElement.children(<span style="color: #800000;">'[id*=someId_]'</span>);
  someElementList.attr(<span style="color: #800000;">'checked'</span>, <span style="color: #0000ff;">false</span>);
  someElementList.filter(<span style="color: #800000;">'[id=someId_1]'</span>).attr(<span style="color: #800000;">'checked'</span>, <span style="color: #0000ff;">true</span>);</pre>
<p><strong>How To Find an Element Based on a Like Name/Using Wildcards</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#SomeTable'</span>).children(<span style="color: #800000;">'tr[id*=SomeRowId]'</span>);</pre>
<p><strong>How To Know if a Something is Hidden</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#CheckBoxId'</span>).is(<span style="color: #800000;">':hidden'</span>)</pre>
<p>Possible Use:</p>
<pre> <span style="color: #0000ff;">function</span> showOrHide(element) {
    <span style="color: #0000ff;">if</span>(jQuery(element).is(<span style="color: #800000;">':hidden'</span>)) {
      jQuery(element).show();
    }
    <span style="color: #0000ff;">else</span> {
      jQuery(element).hide();
    }
  }</pre>
<p><strong>How to Disable an Input/Select</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#DropDownListId'</span>).attr(<span style="color: #800000;">'disabled'</span>, <span style="color: #800000;">'disabled'</span>);</pre>
<p><strong>How to Enable an Input/Select</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#DropDownListId'</span>).removeAttr(<span style="color: #800000;">'disabled'</span>);</pre>
<p><strong>How to Know if an Element Exists</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#ElementId'</span>).length &gt; 0</pre>
<p>Possible use:</p>
<pre>  <span style="color: #0000ff;">function</span> elementExists(elementId) {
    <span style="color: #0000ff;">return</span> jQuery(elementId).length &gt; 0;
  }</pre>
<h2>String</h2>
<p><strong>How To Remove the First Character from a String</strong></p>
<pre>  someString.substring(1, someString.length)</pre>
<h2>Tables</h2>
<p><strong>How to Add a Row to a Table</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#TableId &gt; tbody:last'</span>).append(<span style="color: #800000;">'&lt;tr&gt;...&lt;/tr&gt;'</span>)</pre>
<p><strong>How to Remove a Table Row</strong></p>
<pre>  jQuery(<span style="color: #800000;">'#TableId &gt; tbody:last'</span>).children(someSortOfRowId).remove();</pre>
<h2>Methods</h2>
<p><strong>How to Post With a Link</strong></p>
<pre>  <span style="color: #0000ff;">function</span> postToUrl(path, params)
  {
    <span style="color: #0000ff;">var</span> method = "post"; 

    <span style="color: #0000ff;">var</span> form = document.createElement(<span style="color: #800000;">"form"</span>);
    form.setAttribute(<span style="color: #800000;">"method"</span>, method);
    form.setAttribute(<span style="color: #800000;">"action"</span>, path);
    for(<span style="color: #0000ff;">var</span> key <span style="color: #0000ff;">in</span> params)
    {
        var hiddenField = document.createElement(<span style="color: #800000;">"input"</span>);
        hiddenField.setAttribute(<span style="color: #800000;">"type"</span>, <span style="color: #800000;">"hidden"</span>);
        hiddenField.setAttribute(<span style="color: #800000;">"name"</span>, key);
        hiddenField.setAttribute(<span style="color: #800000;">"value"</span>, params[key]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form);    

    form.submit();
  }</pre>
<p>Use:</p>
<pre>&lt;a href="javascript:postToUrl('/SomeController/SomeAction/', { 'id', '1' })"&gt;link&lt;/a&gt;</pre>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-grab-bag-whole-bunch-of-jquery-functionality/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery: Find All Checked Checkboxes</title>
		<link>http://byatool.com/ui/jquery-find-all-checked-checkboxes/</link>
		<comments>http://byatool.com/ui/jquery-find-all-checked-checkboxes/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 15:14:27 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=909</guid>
		<description><![CDATA[So this is sort of a repeat of another post, but I figured it has some use on its own. Say you have this: &#60;div&#62; &#60;input type="checkbox" name="JqueryIdList" value="1" /&#62; &#60;input type="checkbox" name="JqueryIdList" value="2" /&#62; &#60;input type="checkbox" name="JqueryIdList" value="3" /&#62; &#60;input type="checkbox" name="JqueryIdList" value="4" /&#62; &#60;/div&#62; &#60;div onclick="getIds('JqueryIdList');"&#62; CLICK &#60;/div&#62; And you need the getIds [...]]]></description>
			<content:encoded><![CDATA[<p>So this is sort of a repeat of another post, but I figured it has some use on its own.</p>
<p>Say you have this:</p>
<pre>  &lt;<span style="color: #800000;">div</span>&gt;
    &lt;<span style="color: #800000;">input</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="checkbox"</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="JqueryIdList"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="1"</span>  /&gt;
    &lt;<span style="color: #800000;">input</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="checkbox"</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="JqueryIdList"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="2"</span> /&gt;
    &lt;<span style="color: #800000;">input</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="checkbox"</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="JqueryIdList"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="3"</span> /&gt;
    &lt;<span style="color: #800000;">input</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="checkbox"</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="JqueryIdList"</span> <span style="color: #ff0000;">value</span><span style="color: #0000ff;">="4"</span> /&gt;
  &lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="getIds('JqueryIdList');"</span>&gt;
    CLICK
  &lt;/<span style="color: #800000;">div</span>&gt;</pre>
<p>And you need the getIds method to find all the checked checkboxes from that markup. Well it's actually fairly simple, or at least it wil be once I enlighten you. I should get paid for this...</p>
<pre>  <span style="color: #0000ff;">function</span> getIds(checkList)
  {
    <span style="color: #0000ff;">var</span> idList = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Array</span>();
    <span style="color: #0000ff;">var</span> loopCounter = 0;
    <span style="color: #008000;">//find all the checked checkboxes</span>
    jQuery(<span style="color: #800000;">"input[name="</span> + checkList + <span style="color: #800000;">"]:checked"</span>).each
    (
      <span style="color: #0000ff;">function</span>()
      {
        <span style="color: #008000;">//fill the array with the values</span>
        idList[loopCounter] = jQuery(<span style="color: #0000ff;">this</span>).val();
        loopCounter += 1;
      }
    );
    ...
  }</pre>
<p>The important part is this:</p>
<pre>
 jQuery(<span style="color: #800000;">"input[name="</span> + checkList + <span style="color: #800000;">"]:checked"</span>).</pre>
<p>As you can see, it is very simple.  Give it the name of the checkboxes, add on the ":checked" and boom you have a list of checked checkboxes.  Could it be more simple?  I think not.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-find-all-checked-checkboxes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery: Holy Smokes I Like This Link</title>
		<link>http://byatool.com/ui/jquery-holy-smokes-i-like-this-link/</link>
		<comments>http://byatool.com/ui/jquery-holy-smokes-i-like-this-link/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 13:11:07 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Good Link]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=877</guid>
		<description><![CDATA[Good old Stackoverflow. Wasn't sure how to post this without looking like I'm trying to get search engine hits. However, wanted to pass this one on anyhow. How to center a div using jQuery. Have used it. Works brilliantly. Although the: this.css("position","absolute"); May be overkill since any "pop up" div I create already has that [...]]]></description>
			<content:encoded><![CDATA[<p>Good old Stackoverflow. Wasn't sure how to post this without looking like I'm trying to get search engine hits. However, wanted to pass this one on anyhow.</p>
<p><a href="http://stackoverflow.com/questions/210717/what-is-the-best-way-to-center-a-div-on-the-screen-using-jquery">How to center a div using jQuery</a>.</p>
<p>Have used it. Works brilliantly. Although the:</p>
<pre>  <span style="color: #0000ff;">this</span>.css(<span style="color: #ff0000;">"position"</span>,<span style="color: #ff0000;">"absolute"</span>);</pre>
<p>May be overkill since any "pop up" div I create already has that in the style where personally I think it should be.</p>
<p>How does it work? Well break down the "top" part:</p>
<pre>  <span style="color: #0000ff;">this</span>.css(<span style="color: #ff0000;">"top"</span>, ( jQuery(window).height() - <span style="color: #0000ff;">this</span>.height() ) / 2+jQuery(window).scrollTop() + <span style="color: #ff0000;">"px"</span>);</pre>
<p>To</p>
<pre>   jQuery(window).height() - <span style="color: #0000ff;">this</span>.height() ) / 2</pre>
<p>But why not just divide the height by 2? Well say the height of the window is 800 and your item is 600 high. If you just placed the item at 800 / 2, the bottom of the item would end up 1000. (800/2 = 400... 600 tall item + 400 starting point = 1000). So subtracting the item's height from the window height and THEN dividing by two ensures that that item, provided it is no taller than the window, will end up within the 800 tall window.</p>
<pre>  + jQuery(window).scrollTop()</pre>
<p>Now what if the user has scrolled the widow down? Well adjust for that. using the scrollTop(), the item can be started at the correct point adjusted with the scroll. Nice huh?</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li>No Related Posts</li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-holy-smokes-i-like-this-link/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript:  Scope and Learning Something New Everyday.</title>
		<link>http://byatool.com/ui/javascript-scope-and-learning-something-new-everyday/</link>
		<comments>http://byatool.com/ui/javascript-scope-and-learning-something-new-everyday/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 14:17:20 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=863</guid>
		<description><![CDATA[Course for me it doesn't take much to learn something new, but let's not get into that. So pop quiz, and one that doesn't involve a hostage and shooting. jQuery(document).ready ( function() { for(var loopCounter = 0; loopCounter &#60; 3; loopCounter++) { var tempThing = loopCounter; alert('in' + tempThing); jQuery(".someButton").click ( function() { alert('out' + [...]]]></description>
			<content:encoded><![CDATA[<p>Course for me it doesn't take much to learn something new, but let's not get into that.</p>
<p>So pop quiz, and one that doesn't involve a hostage and shooting.</p>
<pre>jQuery(document).ready
(
  <span style="color: #0000ff;">function</span>()
  {
    <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; 3; loopCounter++)
    {
      <span style="color: #0000ff;">var</span> tempThing = loopCounter;
      alert(<span style="color: #ff0000;">'in'</span> + tempThing); 

      jQuery(<span style="color: #800000;">".someButton"</span>).click
      (
        <span style="color: #0000ff;">function</span>()
        {
          alert(<span style="color: #ff0000;">'out'</span> + tempThing);
        }
      );
    }
  }
);
....
  &lt;<span style="color: #800000;">button</span> <span style="color: #0000ff;">class</span><span style="color: #ff0000;">="someButton"</span>&gt;click!&lt;/<span style="color: #800000;">button</span>&gt;</pre>
<p>What happens when you click that button? Well for one it will alert three times but what will it print out?</p>
<p>The first alert (in the loop) does what you would expect. It gives 0, 1, 2. But what about the button click? Well that's a little different. It gives 2, 2, 2... Wait what? The f--- is that? That makes no real sense.</p>
<p>Given that this was C#, it would output 0, 1, 2 because we would know that every loop the tempThing field is a new field. Sure it has the same name in code, but every iteration would create a new space in memory for a new tempThing. And then with closures, a reference would be made to that tempThing so that when the method actually runs, the values are what they should be. Javascript does something a little different. Go figure, right? Can't do anything like anyone else... anyone else being C#.</p>
<p>Now what does happen is it does give you the correct value of tempThing. What you didn't know (Assuming you didn't know because if you are reading this you either are really bored or don't know... or both) is that tempThing is global to that method. Turns out variables are global to the current scope no matter if in a loop or not. tempThing, no matter where in that function is created/instantiated/ect is the same tempThing through out the method. So what's happening is that the first time in the loop it's being created and every iteration after it's just being updated with the current loopCounter value.</p>
<p>How do you solve this? Well you have to make a method to create a method to set the value. AWESOME!!111</p>
<pre>jQuery(document).ready
(
  <span style="color: #0000ff;">function</span>()
  {
    <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; 3; loopCounter++)
    {
      alert(<span style="color: #ff0000;">'in'</span> + tempThing); 

      jQuery(<span style="color: #800000;">".someButton"</span>).click
      (
        createReturnMethod(loopCounter)
      );
    }
  }

);

<span style="color: #008000;">//This is used to create a new method scope so that
</span><span style="color: #008000;">//tempThing is unique to each call.</span>
<span style="color: #0000ff;">function</span> createReturnMethod(loopCounter)
{
  <span style="color: #0000ff;">var</span> tempThing = loopCounter;

  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">function</span>(event)
  {
    alert(<span style="color: #ff0000;">'out'</span> + tempThing);
  };
}</pre>
<p>As you can see, I created a method to create a method to be run on click. Because tempThing is now in the second method's scope, it will be different for every time this method is called. Magic!</p>
<p>Here's a thought, if only you can prevent forest fires then you really should get on that.  People have lost a lot of money because of your slacking.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li>No Related Posts</li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/javascript-scope-and-learning-something-new-everyday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Whack-A-Mole&#8230; Timer, Hide, Slide, and Fun</title>
		<link>http://byatool.com/ui/jquery-whack-a-mole-timer-hide-slide-and-fun/</link>
		<comments>http://byatool.com/ui/jquery-whack-a-mole-timer-hide-slide-and-fun/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 14:12:16 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Stupid]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=857</guid>
		<description><![CDATA[So I'll just straight up file this under "I really shouldn't have" and not bother asking for forgiveness. For some reason I had it in my mind I wanted to see if I could take the stuff I learned from this guy and this guy and see if I could make a simple Whack A [...]]]></description>
			<content:encoded><![CDATA[<p>So I'll just straight up file this under "I really shouldn't have" and not bother asking for forgiveness.  For some reason I had it in my mind I wanted to see if I could take the stuff I learned from <a href="http://byatool.com/index.php/ui/jquery-slide-menu-with-pop-up-divs-yay/">this guy</a> and <a href="http://byatool.com/index.php/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/">this guy</a> and see if I could make a simple Whack A Mole game with just jquery and the three include files. (jquery, jquery UI, and jquery Timer)  Not thinking of the massive consequences such a thing might bring, I pushed on in the name of science.  </p>
<p>So with no further build up or stupidity (I used that all up in building this game) I give you <a href="http://www.byatool.com/Hosted/LiveDemos/WhackAMole/WhackAMole.htm">jQuery WHACK A MOLE</a> which oddly enough has somehow clawed it's way into my <a href="http://www.iamwebproject.com/BAT/WhackAMole.zip">hosting here.</a>  So if you're foolish enough to actually want to know how it works, there are plenty of comments in the script files.  Just don't blame me for any loss of intelligence.</p>
<p>I might actually take this to the next horrifying level and make it an mvc application.  Not sure I... no... THE WORLD can take it.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-whack-a-mole-timer-hide-slide-and-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery: Slide Menu With Pop Up Divs.  Yay!</title>
		<link>http://byatool.com/ui/jquery-slide-menu-with-pop-up-divs-yay/</link>
		<comments>http://byatool.com/ui/jquery-slide-menu-with-pop-up-divs-yay/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 20:48:30 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic Controls]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=851</guid>
		<description><![CDATA[So you may have seen this post about how I came to create such a wonderful menu with no real purpose as of yet. Well that menu has been improved and can be downloaded here. Remember that question you never asked? Well once again I've answered it. To take the normal sliding menu farther I [...]]]></description>
			<content:encoded><![CDATA[<p>So you may have seen <a href="http://byatool.com/index.php/ui/jquery-slide-menu-another-cause-i-can-experiment/">this post</a> about how I came to create such a wonderful menu with no real purpose as of yet. Well that <a href="http://byatool.com/Hosted/LiveDemos/SlideMenuHoverDivs/SlideMenuHoverDivs.htm">menu has been improved</a> and can be <a href="http://iamwebproject.com/BAT/SlideMenuHoverDivs.zip">downloaded here</a>.</p>
<p>Remember that question you never asked? Well once again I've answered it.</p>
<p>To take the normal sliding menu farther I thought I should have something happen when hovering over the menu items. Novel! Well as you can see if you clicked on the link above (Sorry, can't repost the link. Links are expensive.) the menu items have large counter parts that show up when hovered over and don't go away until the counter part or the main item is left. What does this all mean? I DON'T KNOW but I'll find out eventually. It's in the cards man, it's in the cards...</p>
<p>Ok so additions... For the style sheet, I added a class:</p>
<pre><span style="color: #800000;">.menuItemBig
</span>{
  <span style="color: #ff0000;">height</span>:<span style="color: #0000ff;">100px</span>;
  <span style="color: #ff0000;">margin</span><span style="color: #ff0000;">-right</span>:<span style="color: #0000ff;">5px</span>;
  <span style="color: #ff0000;">position</span>:<span style="color: #0000ff;">absolute</span>;
  <span style="color: #ff0000;">width</span>:<span style="color: #0000ff;">100px</span>;
  <span style="color: #ff0000;">z-index</span>:<span style="color: #0000ff;">100</span>;
}</pre>
<p>And have included a child div to the menu item:</p>
<p><strong>Before</strong></p>
<pre>  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem</span> <span style="color: #0000ff;">floatLeft</span> <span style="color: #0000ff;">blue</span>"&gt;1&lt;/<span style="color: #800000;">div</span>&gt;</pre>
<p><strong>Now</strong></p>
<pre>  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;
    1
    &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItemBig red"</span>&gt;THIS IS THE BIG ITEM 1&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;/<span style="color: #800000;">div</span>&gt;</pre>
<p>I added two methods:</p>
<pre><span style="color: #008000;">//
</span><span style="color: #008000;">// mouseenter: Find any menuItemBig within the given element and show</span>
<span style="color: #008000;">//             Reset the position of the menuItemBig element to appear</span>
<span style="color: #008000;">//             to be in the middle of the parent element</span>
<span style="color: #008000;">// mouseleave: Hide the child menuItemBig</span>
<span style="color: #008000;">//</span>
<span style="color: #0000ff;">function</span> setOnHoverForMenuItems(items)
{
  jQuery(items)
  .each
  (
    <span style="color: #0000ff;">function</span> ()
    {
      jQuery(<span style="color: #0000ff;">this</span>).mouseenter
      (
        <span style="color: #0000ff;">function</span>()
        {
          <span style="color: #008000;">//Why the positioning?  I wanted the child div to show up in the</span>
          <span style="color: #008000;">// middle of the parent div which is done by putting the child's left side</span>
          <span style="color: #008000;">// to half of the width of the parent over from the parent's left side</span>
          <span style="color: #0000ff;">var</span> parentPosition = jQuery(<span style="color: #0000ff;">this</span>).position();
          <span style="color: #0000ff;">var</span> bigItems = jQuery(<span style="color: #0000ff;">this</span>).children(<span style="color: #800000;">".menuItemBig"</span>);
          bigItems.css({ left: parentPosition.left - (jQuery(<span style="color: #0000ff;">this</span>).width() /2), top: parentPosition.top + 10  });
          bigItems.show();
        }
      );

      jQuery(<span style="color: #0000ff;">this</span>).mouseleave
      (
        <span style="color: #0000ff;">function</span>()
        {
          jQuery(<span style="color: #0000ff;">this</span>).children(<span style="color: #800000;">".menuItemBig"</span>).hide();
        }
      );
    }
  );
}</pre>
<p>Why not hover? *EDIT* It should be hover.  Turns out an issue I was having with hover was not actually an issue with it...  *END EDIT*</p>
<p>The other method I added was a simple one:</p>
<pre><span style="color: #008000;">//</span>
<span style="color: #008000;">// Used to find any element of menuItemBig</span>
<span style="color: #008000;">//</span>
<span style="color: #0000ff;">function</span> hideAllBigItems(bigItemParentItems)
{
  jQuery(bigItemParentItems).children(<span style="color: #800000;">".menuItemBig"</span>).hide();
}</pre>
<p>And where did I use these? I appended them to the setChildrenDivs method:</p>
<pre><span style="color: #0000ff;">function</span> setChildrenDivs(mainHolder)
{
  <span style="color: #0000ff;">var</span> menuItems = getMenuItems(mainHolder);

  <span style="color: #0000ff;">for</span> (var loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    <span style="color: #0000ff;">if</span>(loopCounter &gt; maximumToShow - 1)
    {
      jQuery(menuItems[loopCounter]).hide();
    }
  }

  setPager(jQuery(mainHolder).children(<span style="color: #800000;">".leftPager"</span>), getLastVisible, getNextInLineBack);
  setPager(jQuery(mainHolder).children(<span style="color: #800000;">".rightPager"</span>),getFirstVisible, getNextInLineFront);
  <span style="color: #ff0000;">setOnHoverForMenuItems(menuItems);</span>
  <span style="color: #ff0000;">hideAllBigItems(menuItems);</span>
}</pre>
<p>So in all, this didn't take much at all, thanks to mouseleave. Next up... not sure. No idea if I will take this any further.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-slide-menu-with-pop-up-divs-yay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Slide Menu&#8230;  Another Cause I Can Experiment</title>
		<link>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/</link>
		<comments>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 17:00:41 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Menu]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=827</guid>
		<description><![CDATA[So for no real reason at all I had it in my mind that I wanted to make a horizontal menu with jQuery that would work like that weird scrolling menu thing that Macs have. No idea what it's called. So basically I don't need it, have no reason for it, but damnit I'm going [...]]]></description>
			<content:encoded><![CDATA[<p>So for no real reason at all I had it in my mind that I wanted to make a horizontal menu with jQuery that would work like that weird scrolling menu thing that Macs have.  No idea what it's called.  So basically I don't need it, have no reason for it, but damnit I'm going to make it happen and I did it with only 3 things from <a href="http://www.jquery.com">jquery.com</a>; 1.3.2, ui 1.7.2, and jquery.timer . Now this is still rough in the sense it has no real styling but it works tried and true functionality wise.</p>
<p>The main idea is that there are two scroll arrows, one on each side, and X amount of divs. Now at the start, a certain amount of divs are hidden (global variable). When hovering over the left pager, for example, it causes one on the right to hide while one on the left appears giving the feeling of the items sliding.</p>
<p><a href="http://www.byatool.com/Hosted/LiveDemos/SlideMenu/SlideMenu.htm">EXAMPLE HERE!!11</a></p>
<p>The markup is simple, a holder with x number of elements that are "menuitems" and two pager divs.</p>
<pre>&lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="mainHolder"</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="leftPager green floatLeft"</span>&gt;&lt;&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;1&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft red"</span>&gt;2&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft yellow"</span>&gt;3&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;4&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft red"</span>&gt;5&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft yellow"</span>&gt;6&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;7&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="rightPager green floatLeft"</span>&gt;&gt;&lt;/<span style="color: #800000;">div</span>&gt;
&lt;/<span style="color: #800000;">div</span>&gt;</pre>
<p>I think from the classes you can tell what you need to know about them.</p>
<p>First thing we need from jQuery is methods to find various elements in the container when paging.</p>
<pre><span style="color: #008000;">//When using the left pager, it's important to find the first visible element
</span><span style="color: #008000;">//then find the item before it so that it can be shown.</span>
<span style="color: #0000ff;">function</span> getNextInLineBack(menuItems)
{
  <span style="color: #0000ff;">var</span> oneBefore = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>) &amp;&amp; loopCounter &gt; 0)
    {
      oneBefore = jQuery(menuItems[loopCounter - 1 ]);
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> oneBefore;
}

<span style="color: #008000;">//Find the first visible element from the beginning.
</span><span style="color: #008000;">//This will be needed when paging right since it will have to be hidden</span>
<span style="color: #0000ff;">function</span> getFirstVisible(menuItems)
{
  <span style="color: #0000ff;">var</span> firstVisible = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>) &amp;&amp; loopCounter &lt; menuItems.length - maximumToShow)
    {
        firstVisible = menuItems[loopCounter];
        <span style="color: #0000ff;">break</span>;
    }
  }
  <span style="color: #0000ff;">return</span> firstVisible;
} 

<span style="color: #008000;">//Get the last possible visible item
</span><span style="color: #008000;">/</span><span style="color: #008000;">/If the item is in an index less than the maximum number to show, then null is returned since there has to be no more or less than the maximumToShow.
</span><span style="color: #0000ff;">function</span> getLastVisible(menuItems)
{
  <span style="color: #0000ff;">var</span> lastVisible = <span style="color: #0000ff;">null</span>;
  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = menuItems.length - 1; loopCounter &gt; maximumToShow - 1; loopCounter--)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>))
    {
      lastVisible = menuItems[loopCounter];
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> lastVisible;
}

<span style="color: #008000;">//Find the first visible from the end
</span><span style="color: #008000;">//Pretty simple, this will be important when paging left since this</span>
<span style="color: #008000;">//will be the next item to be hidden</span>
<span style="color: #0000ff;">function</span> getNextInLineFront(menuItems)
{
  <span style="color: #0000ff;">var</span> lastOne = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = menuItems.length-1; loopCounter &gt; -1; loopCounter--)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>))
    {
      lastOne = menuItems[loopCounter + 1];
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> lastOne;
}</pre>
<p>Next is a method that is just used to stop having to repeat the same thing over and over when needing a list of all the menu items.</p>
<pre><span style="color: #0000ff;">function</span> getMenuItems(mainHolder)
{
  <span style="color: #0000ff;">return</span> jQuery(mainHolder).children(<span style="color: #ff0000;">".menuItem"</span>);
}</pre>
<p>Next is the method to handle what item to show and what item to hide when the pager has the mouse over it. Instead of having methods for the right and left pager, I just ended up having the methods for finding the item to hide and show sent through as parameters.</p>
<pre><span style="color: #0000ff;">function</span> showHideOnHover(pager, timer, getHideMethod, getShowMethod)
{
  <span style="color: #008000;">//This is just candy for changing the color of the pager when the mouse</span>
  <span style="color: #008000;">//is over it</span>
  jQuery(pager).removeClass(<span style="color: #ff0000;">"green"</span>);
  jQuery(pager).addClass(<span style="color: #ff0000;">"orange"</span>);

  <span style="color: #008000;">//Remember those methods I passed through, well here they</span>
  <span style="color: #008000;">//are in use.  I'm using them to get the item to hide and the item</span>
  <span style="color: #008000;">//to show along with the list of items.</span>
  <span style="color: #0000ff;">var</span> menuItems = getMenuItems(jQuery(pager).parent());
  <span style="color: #0000ff;">var</span> hide = getHideMethod(menuItems);
  <span style="color: #0000ff;">var</span> show = getShowMethod(menuItems);

  <span style="color: #008000;">//If neither is null, then go ahead and show/hide</span>
  <span style="color: #008000;">//If either one is null, something isn't right and the timer</span>
  <span style="color: #008000;">//needs to be stopped.... timer??  Well I'll get to that</span>
  <span style="color: #008000;">//next.</span>
  <span style="color: #0000ff;">if</span>(show != <span style="color: #0000ff;">null</span> &amp;&amp; hide != <span style="color: #0000ff;">null</span>)
  {
    jQuery(hide).hide( <span style="color: #ff0000;">"slide"</span>, { direction: <span style="color: #ff0000;">"right"</span> } , 0);
    jQuery(show).show( <span style="color: #ff0000;">"slide"</span>, { direction: <span style="color: #ff0000;">"left"</span> } , 100);
  }
  else
  {
    timer.stop();
  }
}</pre>
<p>Now for the method above the last one, this one involves the timer passed in the last method. This method actually sets the mouseover/mouseout events (aka hover). When mouseover, the timer is created and the showHideOnHover method is called every 500 units, that's we'll call tools, (Not sure how much that is, seems like a half second) after the first time it's called. On mouseout, the timer is stopped, nulled out, and the pager changes it's color.</p>
<pre><span style="color: #0000ff;">function</span> setPager(pager, hideMethod, showMethod)
{
  <span style="color: #008000;">//Making the timer variable "global" to the events so that I know
</span>  <span style="color: #008000;">//I have the same timer for both mouseover and mouseout.</span>
  <span style="color: #0000ff;">var</span> newTimer;
  pager.hover
  (
    <span style="color: #008000;">//Mouseover method
</span>    function()
    {
      var first = true;
      <span style="color: #008000;">//This sets the timer, consequently starting the method for the first time.
</span>      <span style="color: #008000;">//Why timer doesn't have a start method I don't know.  Ask jquery.com.
</span>      <span style="color: #008000;">//The first thing is just so that the first time around it runs right away,
</span>      <span style="color: #008000;">//then each call afterwards comes every 500 tools.
</span>      newTimer = jQuery.timer
                      (
                         0, <span style="color: #008000;">//First time through, runs after 0 tools.</span>
                         function(timer)
                         {
                           showHideOnHover(pager, timer, hideMethod, showMethod);
                           <span style="color: #008000;">//If this is the first time through, reset</span>
                           <span style="color: #008000;">//timer to run every 500 tools.</span>
                           <span style="color: #0000ff;">if</span>(first)
                           {
                              timer.reset(500);
                              first = <span style="color: #0000ff;">false</span>;
                           }
                         }
                       );
    },

    <span style="color: #008000;">//Mouseout method
</span>    <span style="color: #0000ff;">function</span>()
    {
      <span style="color: #008000;">//mouse is done, stop the timer</span>
      newTimer.stop();
      newTimer = <span style="color: #0000ff;">null</span>;
      jQuery(pager).addClass(<span style="color: #ff0000;">"green"</span>);
      jQuery(pager).removeClass(<span style="color: #ff0000;">"orange"</span>)
    }
  );
}</pre>
<p>Now for the method above the one... above. This is used to set the children of the passed in holder.</p>
<pre><span style="color: #0000ff;">function</span> setChildrenDivs(mainHolder)
{
  <span style="color: #008000;">//Get the items for the holder
</span>  <span style="color: #0000ff;">var</span> menuItems = getMenuItems(mainHolder);

  <span style="color: #008000;">//Hide all the items after the first X items (maximumToShow)</span>
  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    if(loopCounter &gt; maximumToShow - 1)
    {
      jQuery(menuItems[loopCounter]).hide();
    }
  }

  <span style="color: #008000;">//set the pagers.</span>
  setPager(jQuery(mainHolder).children(<span style="color: #ff0000;">".leftPager"</span>), getLastVisible, getNextInLineBack);
  setPager(jQuery(mainHolder).children(<span style="color: #ff0000;">".rightPager"</span>),getFirstVisible, getNextInLineFront);
}</pre>
<p>FINALLY THE END! This is the document.ready method used to set this whole joke in motion. maximumToShow is just how many items to show at a time and is global.</p>
<pre><span style="color: #0000ff;">var</span> maximumToShow = 5;

jQuery(document).ready
(
  <span style="color: #0000ff;">function</span>()
  {
    <span style="color: #008000;">//Find every holder on the page and set everything in motion.
</span>    <span style="color: #0000ff;">var</span> mainHolders = jQuery(<span style="color: #ff0000;">".mainHolder"</span>);
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; mainHolders.length; loopCounter++)
    {
      setChildrenDivs(mainHolders[loopCounter]);
    }
  }
);</pre>
<p>Why the timer? If you haven't figured that out yet, well it's because I had issues with how to get the menu to keep doing it's thing as long as the user had his/her mouse over a pager. I didn't want this to be a click menu because, let's be honest, that would be much easier. So as is, the timer is started the moment the mouse is over a pager and hides/shows an item. Then every 500 tools the mouse is over the pager, it continues the hide/show until it runs out of items to show/hide. (End of the list)</p>
<p>Uhg that's annoying to type out even with cut and paste so <a href="http://www.iamwebproject.com/BAT/SlideMenu.zip">I will host it here</a>.</p>
<p>I suppose the next part of this would have the items blow up or something when hovering over them but that should be much easier than this was.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/" title="jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide">jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide</a></li><li><a href="http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/" title="ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?">ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide</title>
		<link>http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/</link>
		<comments>http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 19:19:31 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=749</guid>
		<description><![CDATA[So this could be filed under "I did it because I can" (which is a really good mantra in science...) and am not sure I'll use it but it is useful in the concept. LIVE EXAMPLE HERE The idea is to set the hover on one div to show/hide another div WITHOUT having to use [...]]]></description>
			<content:encoded><![CDATA[<p>So this could be filed under "I did it because I can" (which is a really good mantra in science...) and am not sure I'll use it but it is useful in the concept.</p>
<p><a href="http://www.byatool.com/Hosted/LiveDemos/SlideOnHover/SlideOnHover.htm">LIVE EXAMPLE HERE</a></p>
<p>The idea is to set the hover on one div to show/hide another div WITHOUT having to use some kind of id/name trick (as in setting the id to "divHide1") and to have it be completely self setting in the .ready method. Why would this be at all useful? Say you want to roll through a list of things and generate the divs, but want to defer the jQuery until the page has loaded. And you don't want to have to resort to:</p>
<pre>    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div</span> <span style="color: #ff0000;">id</span>="someDiv&lt;%= someId %&gt;"<span style="color: #0000ff;">&gt;</span><span style="color: #0000ff;">&lt;/</span>div<span style="color: #0000ff;">&gt;</span></pre>
<p>like structure where you parse some identifier from the id property. Mostly because you have no idea how many someDiv1s there could be on the page. It could be a highly reused name (someDiv) and that could lead to a mess.</p>
<p>Also the reason 'cuz. If you have any more questions of why after that answer, well you're just being annoying.</p>
<p>Anywho, here's the actual html for structure.</p>
<pre>   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="mainDiv"</span><span style="color: #0000ff;">&gt;</span>
      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="showDiv"</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="oneTop"&gt;</span>
        Kaneda?
      <span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
      <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="slideDiv"</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="one"&gt;</span>
        What do you see?!
      <span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="clear"</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #0000ff;">/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span></pre>
<p>Now I get that this isn't off a foreach, but it doesn't take much to figure out that it's easily made into a loop if you just loop it over and over. Why? Because first there are no ids or names so that you can't have two of the same name and also because that chunk is self contained.</p>
<p>So what is going on there? Simple, you have a parent container that holds a div to hold and a div to hover over and the other that will be shown/hidden.</p>
<p>Here's the styles involved just incase you care:</p>
<pre>    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">style</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/css"&gt;</span>
        <span style="color: #800000;">.clear
</span>        {
        	<span style="color: #ff0000;">clear</span>:<span style="color: #0000ff;">both</span>;
        }

        <span style="color: #800000;">.leftDiv</span>
        {
        	<span style="color: #ff0000;">float</span>:<span style="color: #0000ff;">left</span>;
        }

        <span style="color: #800000;">.mainDiv</span>
        {
        	<span style="color: #ff0000;">margin</span>:<span style="color: #0000ff;">5px</span>;
        	<span style="color: #ff0000;">height</span>:<span style="color: #0000ff;">200px</span>;
        }

        <span style="color: #800000;">.rightDiv</span>
        {
        	<span style="color: #ff0000;">float</span>:<span style="color: #0000ff;">left</span>;
        }

        <span style="color: #800000;">.showDiv</span>
        {
        	<span style="color: #ff0000;">float</span>:<span style="color: #0000ff;">left</span>;
        	<span style="color: #ff0000;">margin-right</span>:<span style="color: #0000ff;">5px</span>;
        	<span style="color: #ff0000;">height</span>:<span style="color: #0000ff;">200px</span>;
        	<span style="color: #ff0000;">background-color</span>:<span style="color: #0000ff;">Silver</span>;
        }

        <span style="color: #800000;">.slideDiv</span>
        {
        	<span style="color: #ff0000;">background-color</span>:<span style="color: #0000ff;">Teal</span>;
        	<span style="color: #ff0000;">height</span>:<span style="color: #0000ff;">200px</span>;
        	<span style="color: #ff0000;">position</span>:<span style="color: #0000ff;">absolute</span>;
        	<span style="color: #ff0000;">float</span>:<span style="color: #0000ff;">left</span>;
        	<span style="color: #ff0000;">z-index</span>:<span style="color: #0000ff;">100</span>;
        }
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">style</span><span style="color: #0000ff;">&gt;</span></pre>
<p>That doesn't entirely matter, but it does to show the div "sliding" over another div. Kind of a little somethin' somethin' I threw in at no extra cost. Now for the jQuery:</p>
<p>First the method for setting the mouse over and out events for the show div, we turn to .hover:</p>
<pre>    <span style="color: #0000ff;">function</span> setHover(currentSlideDiv, currentStableDiv)
    {
      currentStableDiv.hover
      (
        <span style="color: #008000;">//first parameter is the method for showing the div on mouse over
</span>        <span style="color: #0000ff;">function</span>()
        {
          if (currentSlideDiv.is(<span style="color: #800000;">":hidden"</span>))
          {
            currentSlideDiv.show(<span style="color: #800000;">"slide"</span>, { direction: <span style="color: #800000;">"left"</span> }, 100);
          }
        },
        <span style="color: #008000;">//second parameter is the method for hiding the div on mouse out
</span>        <span style="color: #0000ff;">function</span>()
        {
          if (currentSlideDiv.is(<span style="color: #800000;">":visible"</span>))
          {
            currentSlideDiv.hide(<span style="color: #800000;">"slide"</span>, { direction: <span style="color: #800000;">"left"</span> }, 100);
          }
        }
      );
    };</pre>
<p>Now for the method that actually uses these:</p>
<pre>    <span style="color: #008000;">//This is used to take in one of the main divs and set all the
</span>    <span style="color: #008000;">//show and slide divs within.</span>
    <span style="color: #0000ff;">function</span> setChildrenDivs(mainDiv)
    {
      <span style="color: #008000;">//get all the show and slide dvis within the main div
</span>      <span style="color: #0000ff;">var</span> mainChildrenStableDiv = jQuery(mainDiv).children(<span style="color: #800000;">".showDiv"</span>);
      <span style="color: #0000ff;">var</span> mainChildrenSlide = jQuery(mainDiv).children(<span style="color: #800000;">".slideDiv"</span>);

      <span style="color: #008000;">//loop through the list of show divs</span>
      <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; mainChildrenStableDiv.length; loopCounter++)
      {
        <span style="color: #008000;">//Get the show div and it's corresponding slide div using the
</span>        <span style="color: #008000;">//two lists and the current counter.</span>
        <span style="color: #0000ff;">var</span> currentStableDiv = jQuery(mainChildrenStableDiv[loopCounter]);
        <span style="color: #0000ff;">var</span> currentSlideDiv = jQuery(mainChildrenSlide[loopCounter]);

         <span style="color: #008000;">//This is to make sure the slide is where it should be.
</span>         <span style="color: #008000;">//to the right of the show div.</span>
         <span style="color: #0000ff;">var</span> containerPosition = jQuery(currentStableDiv).position();
         jQuery(currentSlideDiv).css({ left: containerPosition.left + currentStableDiv.width() });
        <span style="color: #008000;">//Set the mouse over and the mouse out on the show div</span>
        setHover(currentSlideDiv, currentStableDiv);
      }
    }</pre>
<p>Now the final touch, the .ready method:</p>
<pre>    jQuery(document).ready
    (
      <span style="color: #0000ff;">function</span>()
      {
        <span style="color: #008000;">//hide all the slide divs
</span>        jQuery(<span style="color: #800000;">".slideDiv"</span>).hide();

        <span style="color: #008000;">//find all the parent divs
</span>        <span style="color: #0000ff;">var</span> mainDivs = jQuery(<span style="color: #800000;">".mainDiv"</span>);

        <span style="color: #008000;">//set the children</span>
        for (var loopCounter = 0; loopCounter &lt; mainDivs.length; loopCounter++)
        {
          setChildrenDivs(mainDivs[loopCounter]);
        }
      }
    );</pre>
<p>And boom, now you have multiple show/hide (Slide in this instance). Now if I could just find a use for it...</p>
<p>Oh yeah and you'll need <a href="http://jquery.com/">jQuery 1.3.2</a> and <a href="http://jqueryui.com/">jQuery ui 1.7.2</a> to use this.  At least those are the versions I know this works with.</p>
<p><strong>Update:  Due to popular demand... one person... <a href="http://www.byatool.com/Hosted/SlideOnHover.zip">Source can be found here.</a></strong></p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li><li><a href="http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/" title="ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?">ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?</title>
		<link>http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/</link>
		<comments>http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 15:44:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=721</guid>
		<description><![CDATA[One of the things I've come to realize is how easy it easy to do a lot of things with these three buzzwords. In fact, I'm pretty convinced it's so easy that it's actually complex and I am a genius. Not buying it? Neither am I. So for an experiement the other day I decided [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I've come to realize is how easy it easy to do a lot of things with these three buzzwords. In fact, I'm pretty convinced it's so easy that it's actually complex and I am a genius. Not buying it? Neither am I.</p>
<p>So for an experiement the other day I decided to try my hand at some sort of dynamic grid using jQuery's ajax fun and JSon. Just so happens that this works really well with MVC's REST like makeup. Don't know what REST is? On a very tool level, it's using a url and a command to tell the server what to do. So something like:</p>
<p>www.byatool.com/users/dostuff</p>
<p>Could mean either get all users (if using get) or create a user (If using post). And yes that is probably a ridiculously simplistic view so I'd suggest consulting the <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Wikitruth</a>. In an MVC sense this would be:</p>
<p>Controller: Users<br />
Action: dostuff</p>
<p>Now most likely your Get All Users action isn't going to the same as your Add A User action, but it was just a stupid example ok?</p>
<p>However, with jQuery what this means is you have a simple url that it can call and get information from, making it incredibly easy to set up a dynamic grid.</p>
<p>So first off, lets say I have a Forum controller with an action of IndexJquery... yeah I know cheesy name, but it gets the job done. Basically the method IndexJquery would have to take in a page number and optionally (And for this example) how many items to show along with a sort. With that it should return a JSon "object" that will be in this example holds first page, last page, next page, previous page, sortBy, and some kind of list of stuff. (For the two people actually reading this, comment if you want the c# code. It's really just basic MVC stuff.)</p>
<p>The markup for this is pretty simple. I have a div to hold the grid, four directional divs that work as buttons (First, Previous, Next, Last), and two div "butons" for how many items to show.</p>
<pre>    &lt;div&gt;
        &lt;div id="divHolder"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div id="divFirstPage" class="divLink floatLeft"&gt;
            &lt;&lt;
        &lt;/div&gt;
        &lt;div id="divPreviousPage" class="divLink floatLeft"&gt;
            &lt;
        &lt;/div&gt;
        &lt;div id="divNextPage" class="divLink floatLeft"&gt;
            &gt;
        &lt;/div&gt;
        &lt;div id="divLastPage" class="divLink floatLeft"&gt;
            &gt;&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="clear"&gt;&lt;/div&gt;
    &lt;div&gt;
        &lt;div id="divAmountToShowOne" class="divLink floatLeft"&gt;
            1
        &lt;/div&gt;
        &lt;div id="divAmountToShowFive" class="divLink floatLeft"&gt;
            5
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="clear"&gt;&lt;/div&gt;</pre>
<p>As you can see exactly as advertised.</p>
<p>Ready for the call? It's waaaaay hard:</p>
<pre>        <span style="color: #0000ff;">function</span> getGrid(pageNumberIn, amountToShowIn, sortByIn)
        {
            jQuery.getJSON
            (
               <span style="color: #008000;">//This is the url for the information I need
</span>               <span style="color: #800000;">"http://www.someurl.com/Forum/IndexJquery/"</span>,
               <span style="color: #008000;">//this is the construction of the "object" to send... really this just
               //means that I have a method somewhere looking for pageNumber,
               //amountToShow, and sortBy</span>
               {
                   pageNumber: pageNumberIn,
                   amountToShow: amountToShowIn,
                   sortBy: sortByIn
               },
               <span style="color: #008000;">//This is the method to call once this ajax transaction has completed...
</span>               <span style="color: #008000;">//transaction may not be the best word.  Basically it has to be a method
</span>               <span style="color: #008000;">//that takes in the result from the getJSon call
</span>               returned
            );
        }</pre>
<p>Next would be the script to actually create the grid. Looks verbose, but most likely thats because I didn't refactor much.</p>
<pre>    <span style="color: #0000ff;">function</span> returned(jsonObject)
    {
        <span style="color: #008000;">//Have to remove all the previous click event handlers since because
</span>        <span style="color: #008000;">//this is all client side, there's no "refresh" and therefore the object
</span>        <span style="color: #008000;">//is still in memory.  So even though I might call the method to get the
</span>        <span style="color: #008000;">//information, the "objects" are still in memory.</span>
        jQuery(<span style="color: #800000;">"#divFirstPage"</span>).unbind(<span style="color: #800000;">"click"</span>);
        jQuery(<span style="color: #800000;">"#divLastPage"</span>).unbind(<span style="color: #800000;">"click"</span>);
        jQuery(<span style="color: #800000;">"#divNextPage"</span>).unbind(<span style="color: #800000;">"click"</span>);
        jQuery(<span style="color: #800000;">"#divPreviousPage"</span>).unbind(<span style="color: #800000;">"click"</span>);
        jQuery(<span style="color: #800000;">"#divAmountToShowOne"</span>).unbind(<span style="color: #800000;">"click"</span>);
        jQuery(<span style="color: #800000;">"#divAmountToShowFive"</span>).unbind(<span style="color: #800000;">"click"</span>);

        <span style="color: #008000;">//Ok so now that the event is not being listened to, set up the listeners</span>
        <span style="color: #008000;">//The idea is to call that getGrid method and pass in the values straight</span>
        <span style="color: #008000;">//off the previously returned json object.  Using jQuery's easy .click method
</span>        <span style="color: #008000;">//makes this so simple.
</span>        jQuery(<span style="color: #800000;">"#divFirstPage"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(jsonObject.FirstPage, jsonObject.AmountToShow, jsonObject.SortBy); })
        jQuery(<span style="color: #800000;">"#divPreviousPage"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(jsonObject.PreviousPage, jsonObject.AmountToShow, jsonObject.SortBy); })
        jQuery(<span style="color: #800000;">"#divNextPage"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(jsonObject.NextPage, jsonObject.AmountToShow, jsonObject.SortBy); })
        jQuery(<span style="color: #800000;">"#divLastPage"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(jsonObject.LastPage, jsonObject.AmountToShow, jsonObject.SortBy); })
        jQuery(<span style="color: #800000;">"#divAmountToShowOne"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(0, 1, jsonObject.SortBy); })
        jQuery(<span style="color: #800000;">"#divAmountToShowFive"</span>).click(<span style="color: #0000ff;">function</span>() { getGrid(0, 5, jsonObject.SortBy); })

        <span style="color: #008000;">//Again since this is client side, the divHolder "object" still is holding
</span>        <span style="color: #008000;">//the previous results.  These have to be cleared.
</span>        jQuery(<span style="color: #800000;">"#divHolder"</span>).children().remove();

        <span style="color: #008000;">//Create the table and loop through the list.
</span>        <span style="color: #0000ff;">var</span> mytable = document.createElement(<span style="color: #800000;">"table"</span>);
        <span style="color: #0000ff;">var</span> mytablebody = document.createElement(<span style="color: #800000;">"tbody"</span>);

        <span style="color: #0000ff;">for</span> (loopCounter = 0; loopCounter &lt; jsonObject.ListForViewing.length; loopCounter++)
        {
           <span style="color: #0000ff;">var</span> currentItem = something.ListForViewing[loopCounter];
           <span style="color: #0000ff;">var</span> mycurrent_row = document.createElement(<span style="color: #800000;">"tr"</span>);

           <span style="color: #0000ff;">var</span> mycurrent_cell = document.createElement(<span style="color: #800000;">"td"</span>);
           <span style="color: #0000ff;">var</span> currentText = document.createTextNode(currentItem.ForumName);
           mycurrent_cell.appendChild(currentText);
           mycurrent_row.appendChild(mycurrent_cell);

           mytablebody.appendChild(mycurrent_row);
        }

        mytable.appendChild(mytablebody);
        <span style="color: #008000;">//Don't forget to add the table to the div!!11
</span>        jQuery(<span style="color: #800000;">"#divHolder"</span>).append(mytable);
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;
    }</pre>
<p>And boom. So easy even a ca... tool can do it. Now if you want this grid to come preloaded, it's pretty easy:</p>
<pre>
    jQuery(document).ready
    (
        <span style="color: #0000ff;">function</span> setPage()
        {
            getGrid(0, 10, null);
        }
    )</pre>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li><li><a href="http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/" title="jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide">jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Stop the IPhone / Safari from Minimzing a Page</title>
		<link>http://byatool.com/ui/stop-the-iphone-safari-from-minimzing-a-page/</link>
		<comments>http://byatool.com/ui/stop-the-iphone-safari-from-minimzing-a-page/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 16:40:25 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[IPhone]]></category>
		<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=579</guid>
		<description><![CDATA[Real quick one, but I thought this was crazy useful. If you're developing a web based app that might be used by IPhone users, there is a simple way to stop the IPhone from shrinking the page onload. Thanks to Mark W on The Stack Hath Runith Over there is a simple solution. &#60;head runat="server"&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Real quick one, but I thought this was crazy useful.</p>
<p>If you're developing a web based app that might be used by IPhone users, there is a simple way to stop the IPhone from shrinking the page <span class="showItLink" xmlns:comment="Which causes the ultra annoying need to resize.">onload</span>. Thanks to <a href="http://markwunsch.com">Mark W</a> on <a href="http://www.stackoverflow.com">The Stack Hath Runith Over</a> there is a simple solution.</p>
<pre>  &lt;head runat="server"&gt;
     &lt;title&gt;I LOVE TITLES!&lt;/title&gt;
     &lt;meta name="viewport" content="width=420" /&gt;</pre>
<p>That one beautiful metatag makes life worth living again... until the next Street Fighter movie comes out. Then life is back in the forfeit container. Here's a link to more options:<br />
<a href="http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/chapter_4_section_5.html#//apple_ref/doc/uid/TP40006509-SW26">BYEBYE</a></p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li>No Related Posts</li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/stop-the-iphone-safari-from-minimzing-a-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery: Add a Pop Up Div to a Link Dynamically</title>
		<link>http://byatool.com/ui/556/</link>
		<comments>http://byatool.com/ui/556/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 14:50:31 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=556</guid>
		<description><![CDATA[So as an exercise to learn more about jQuery, I decided to redo this little gem using jQuery. Have to say though only technically XHTML compliant, it worked out much better and with less code. So the idea is to have something really easy for non programmers (You know, lesser people) to be able to [...]]]></description>
			<content:encoded><![CDATA[<p>So as an exercise to learn more about jQuery, I decided to redo this <a href="http://byatool.com/index.php/ui/add-a-pop-up-div-to-a-link-dynamically">little gem</a> using jQuery. Have to say though only technically XHTML compliant, it worked out much better and with less code. So the idea is to have something really easy for non programmers (You know, lesser people) to be able to have a pop up comment added to some chunk of text on a web site. What I came up with before was ok but kind of annoying since it looked like this:</p>
<pre>&lt;<span style="color: #800000;">a</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="return ShowCommentForPost('1', this, 'THIS IS SO STUPID!');"</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="www.byatool.com"</span>&gt;word.&lt;<span style="color: #800000;">/a</span>&gt;</pre>
<p>Kind of annoying since I would have to explain that '1' is the name and it has to be unique for every one of <a class="showItLink" href="http://www.byatool.com" xmlns:comment="Hahaha... yeah they'll make them unique.  Really.  People aren't lazy.">these</a>, this isn't really understood by those people, and well it just seems more complicated then it needs to be. So what if I told you it could look like this?</p>
<pre>&lt;<span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="http://www.byatool.com"</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="showItLink"</span> <span style="color: #ff0000;">xmlns:comment</span><span style="color: #0000ff;">="hihihi"</span>&gt;HI&lt;<span style="color: #800000;">/a</span>&gt;</pre>
<p>Gorsh, that seems even better and it works in both IE and Firefox... which might be a first for this site and anything javascript. First let's get the css out of the way, shall we?</p>
<pre>    .<span style="color: #800000;">showItLink</span>
    {
    }

    .<span style="color: #800000;">postComment</span>
    {
        <span style="color: #ff0000;">background-color</span>:<span style="color: #0000ff;">Gray</span>;
        <span style="color: #ff0000;">border-color</span>:<span style="color: #0000ff;">Black</span>;
        <span style="color: #ff0000;">border-style</span>:<span style="color: #0000ff;">dotted</span>;
        <span style="color: #ff0000;">border-width</span>:<span style="color: #0000ff;">thin</span>;
        <span style="color: #ff0000;">color</span>:<span style="color: #0000ff;">White</span>;
        <span style="color: #ff0000;">margin-right</span>:<span style="color: #0000ff;">3px</span>;
        <span style="color: #ff0000;">padding</span>:<span style="color: #0000ff;">3px</span>;
        <span style="color: #ff0000;">position</span>:<span style="color: #0000ff;">absolute</span>;
        <span style="color: #ff0000;">text-decoration</span>:<span style="color: #0000ff;">none</span>;
        <span style="color: #ff0000;">z-index</span>:<span style="color: #0000ff;">50</span>;
    }</pre>
<p>Most of the is just for looks, but like the older example I've called upon the power of <a href="http://byatool.com/index.php/ui/jquery-position-absolute-and-how-to-make-it-all-work">position:absolute</a> and z-index well that's just pulling it in front of everything else.</p>
<p>Next part will be the actual code, and if you take the method <a class="showItLink" href="http://www.byatool.com" xmlns:comment="ANOTHER PLUG AHHHHH!!!11">from</a> <a href="http://byatool.com/index.php/ui/jquery-position-absolute-and-how-to-make-it-all-work">THE POSITION ABSOLUTE POST</a></p>
<pre>    <span style="color: #0000ff;">function</span> SetTopAndLeft(parentContainer, elementToSet)
    {
        <span style="color: #0000ff;">var</span> containerPosition;

        containerPosition = $(parentContainer).position();
        $(elementToSet).css({ top: containerPosition.top + 10, left: containerPosition.left + 10 });
    }</pre>
<p>and add in a simple span creation method:</p>
<pre>    <span style="color: #0000ff;">function</span> CreateDiv(innerText, cssClass)
    {
        <span style="color: #0000ff;">var</span> spanToAdd;

        spanToAdd = document.createElement(<span style="color: #ff0000;">'span'</span>);
        spanToAdd.className = cssClass;
        spanToAdd.innerHTML = innerText;

        <span style="color: #0000ff;">return</span> spanToAdd;
    }</pre>
<p>you're ready for the actual fun part... making sure something pops up when the link is clicked.</p>
<pre>jQuery(document).ready  <span style="color: #008000;">//Everything inside this will load as soon as the DOM is loaded and before the page contents are loaded.</span><a href="http://www.learningjquery.com/2006/09/introducing-document-ready"><span style="color: #008000;">*</span></a>
(
    <span style="color: #0000ff;">function</span>()  //this is the start of an anonymous method
    {
        jQuery(<span style="color: #800000;">".showItLink"</span>).click <span style="color: #008000;">//find all things with the .showItLink class and assign the click event to the next anonymous method
        </span>(
            <span style="color: #0000ff;">function</span>(event) <span style="color: #008000;">//this is the start of an anonymous method for the click event</span>
            {
                <span style="color: #0000ff;">var</span> containerPosition;
                <span style="color: #0000ff;">var</span> createdSpan;
                <span style="color: #0000ff;">var</span> comment;

                comment = jQuery(this).attr(<span style="color: #800000;">"xmlns:comment"</span>);  <span style="color: #008000;">//Get the value from the comment attribute on the link.</span>
                createdSpan = jQuery(this).children(<span style="color: #800000;">".postComment"</span>); <span style="color: #008000;">//Find a possible span already attached to the link if it exists.  The span is of class 'postComment'</span>

                <span style="color: #0000ff;">if</span> (createdSpan.length == 0)  <span style="color: #008000;">//span doesn't exist</span>
                {
                    createdSpan= CreateDiv(comment, <span style="color: #800000;">"postComment"</span>);  <span style="color: #008000;">//create the span</span>
                    jQuery(this).append(createdSpan);  <span style="color: #008000;">//Add the span to the link</span>
                    jQuery(this).children(<span style="color: #800000;">".postComment"</span>).hide();  <span style="color: #008000;">//Make sure the new span is hidden
</span>                }
                SetTopAndLeft(this, createdSpan);  <span style="color: #008000;">//Set the position of the span</span>
                jQuery(createdDiv).toggle();  <span style="color: #008000;">//This will hide if it's showing, show if it's hidden... kind of nice huh?</span>

                event.preventDefault();  <span style="color: #008000;">//Equivalent to false.  Need this for Firefox.</span>
            }
        );
    }
);</pre>
<p>And boom you have something that works. Hooray.</p>
<p>Couple things of note:</p>
<p>.Hide - At first I though this would screw up my class for the span by removing the current class and adding display:none. Turns out it doesn't harm the original class. Kind of nice.</p>
<p>.Toggle() - This is really nice. It will hide if it is showing and show if hidden. Stupid easy to use and is pretty effective. Just like .Hide, the class of the element is not harmed.</p>
<p>$ versus jQuery - Some people might notice that I am not using the short hand $ for my jQuery calls. Turns out that it might be safer this way. There are other javascript libraries that use the $ short hand like prototype. I ran into this with <a class="showItLink" href="http://byatool.com" xmlns:comment="Go figure...">WordPress </a>since it uses both jQuery and Prototype and blocks jQuery from using $ since it could conflict with other libraries. Weeeee!</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/" title="jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains">jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains</a></li><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/556/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JQuery, Position : Absolute, and How to Make It All Work</title>
		<link>http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/</link>
		<comments>http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 20:39:59 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=549</guid>
		<description><![CDATA[This is a really quick one but when I was taking my cheesy pop up and reworking it using JQuery (After Andre the Annoying wouldn't shut up about it), I ran into a fun problem: position:absolute wasn't working like it should. You know "absolute is positioned at the specified coordinates relative to its containing block.". [...]]]></description>
			<content:encoded><![CDATA[<p>This is a really quick one but when I was taking <a href="http://byatool.com/index.php/ui/add-a-pop-up-div-to-a-link-dynamically">my cheesy pop up</a> and reworking it using <a href="http://docs.jquery.com/Main_Page">JQuery</a> (After Andre the Annoying wouldn't shut up about it), I ran into a fun problem: position:absolute wasn't working like it should. You know "absolute is positioned at the specified coordinates relative to its containing block.". Meaning it should at worst show up within it's container, where ever that is. Now IE is fine with that and the thing was showing up well:</p>
<pre><img title="absolutepositionie" src="http://byatool.com/wp-content/uploads/2009/03/absolutepositionie.png" alt="absolutepositionie" width="247" height="49" /></pre>
<p>Firefox? Not so much:</p>
<pre><img title="absolutepositionfirefox" src="http://byatool.com/wp-content/uploads/2009/03/absolutepositionfirefox.png" alt="absolutepositionfirefox" width="280" height="70" /></pre>
<p>Well... turns out JQuery pretty much does it for me. With a simple method, you can set one element's position relative to a parent's position:</p>
<pre>    <span style="color: #0000ff;">function</span> SetTopAndLeft(parentContainer, elementToSet)
    {
        <span style="color: #0000ff;">var</span> containerPosition;

        containerPosition = $(parentContainer).position();
        $(elementToSet).css({ top: containerPosition.top + 10, left: containerPosition.left + 10 });
    }</pre>
<p>Really simple, you get the position of the parent container and you set the child element's top and left to it. Or in this case, I have it just off since hiding the parent container could be problematic. (Say if the parent container is a link AND NOW YOU CAN'T FIND IT TO CLICK ON IT AND THINGS HAPPEN BAD THINGS AND THE WORLD EXPLODES BECAUSE OF YOU!)</p>
<p>And boom, you can for once be a winner just like me.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/" title="jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains">jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains</a></li><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Add a Pop Up Div to a Link Dynamically</title>
		<link>http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/</link>
		<comments>http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 19:30:36 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=523</guid>
		<description><![CDATA[Sometimes in life you have to ask "should I do this", this is not one of those times. The idea is simple, click on a link and a div appears over the link with some kind of message in it. Kind of like being able to add a pop up note to a word. If [...]]]></description>
			<content:encoded><![CDATA[<p>    Sometimes in life you have to ask "should I do this", this is not one of those times. The idea is simple, click on a link and a div appears over the link with some kind of message in it. Kind of like being able to add a pop up note to a <a onclick="return ShowCommentForPost('1', this, 'THIS IS SO STUPID!');" href="www.byatool.com">word.</a> If you are absolutely amazed by that, don't be afraid. Most likely you'll die soon from forgetting to breathe. However, if you are just slightly curious as to why and how, keep reading.</p>
<p>    So why did I do this? Well it started with the idea of having something simple for a blog that has multiple authors: What if other authors could add notes to someone's post in the post. Well the idea of using some kind of text change (Like italics) sounded lame. I wanted something easy that could be replicated quickly and wouldn't be visible unless needed. Thus the onClick idea. Now the next problem I had was the class needed for the style sheet. As you can see, when the div is shown, it doesn't displace any of the items on the page. This is because I am using position:absolute and a high z-index. This allows for the div to lay on top of other things and not touch them. Problem with absolute is that it basicaly plants the div in relation to it's parent container. Now that whole parent container thing seems to be up for interpretation when you are talking about browsers. Each seems to deal with it the way it sees fit.</p>
<p>    Originally I had it as a div that would contain this new div. This was a pain in the -ss. In IE it showed up over the div, FireFox not so much. So the next thought was to create a div to hold the div that held the div. Something that isn't exactly "user friendly" to be sure. Then it hit me, maybe I could put this in a link. After all, people who are viewing the blog would understand it's something they can click on (Provided I don't screw with the link styles too much) and it's easy for non coders to copy and paste.</p>
<p>So on to the promised land. First I'll just get the CSS out of the way since it's absolutely needed but needs little explanation:</p>
<pre>.<span style="color: #ff0000;">hidePostComment</span>
{
    <span style="color: #0000ff;">visibility</span>:hidden;
    <span style="color: #0000ff;">position</span>: absolute;
    <span style="color: #0000ff;">z-index</span>:-100;
}

.<span style="color: #ff0000;">showPostComment</span>
{
    <span style="color: #0000ff;">background-color</span>:Gray;
    <span style="color: #0000ff;">border-color</span>:Black;
    <span style="color: #0000ff;">border-style</span>:dotted;
    <span style="color: #0000ff;">border-width</span>:thin;
    <span style="color: #0000ff;">color</span>:White;
    <span style="color: #0000ff;">margin-right</span>:3px;
    <span style="color: #0000ff;">padding</span>:3px;
    <span style="color: #0000ff;">position</span>:absolute;
    <span style="color: #0000ff;">text-decoration</span>:none;
    <span style="color: #0000ff;">visibility</span>:visible;
    <span style="color: #0000ff;">width</span>:100px;
    <span style="color: #0000ff;">z-index</span>:10;
}</pre>
<p>.hidePostComment</p>
<p>    As you can see, I've screwed with the z-index, visibility, and position. Position I've already explained, and I think you can understand why visibility is hidden with this class. However, z-index might not be something you know about. Basically,z-index tells the browser where an item is in a vertical sense. When you look at a browser, there are actually a lot of layers regardless of the 2nd appearance. The z-index is used to bring something forward or backward. If I want the div to be behind say the text I am typing right now, it has to be at a z-index lower than the text. I used -100 in the example just to make sure it's behind anything. It's really an arbitrary number though. A positive number would make the div appear in front of the text (And in that case the text would not show up since it would be "behind" the div) which is what I did with the visibility class.</p>
<p>.showPostComment</p>
<p>    Mostly just a bunch of visual changes like border and background color. However, you will also notice the the position is still absolute and that the z-index is now 10. (positive) The div will now effectively be "in front" of the link when it shows up giving it the pop up look. One Note: I had to add in text-decoration:none since the div is attached to the link and IE wants to drag the underline with the pop up causing the text to up with an underline. Kind of odd but no big deal.</p>
<p>Now for the code:</p>
<pre><span style="color: #0000ff;">function</span> BuildSelectableSpanForPost(spanName, parentElement, innerText)
{
    <span style="color: #0000ff;">var</span> divToAdd;
    <span style="color: #0000ff;">var</span> parentContainer;
    <span style="color: #008000;">//check to see if the parentElement is actually an element or string.  If string, use it
    //to find the element.
</span>    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">typeof</span> parentElement == <span style="color: #ff0000;">'string'</span>)
    {
        parentContainer = document.getElementById(parentElement);
    }
    <span style="color: #0000ff;">else</span>
    {
        parentContainer = parentElement;
    }
    <span style="color: #008000;">//Create the div
    //set the name (The name must unique since there could be a million "pop ups" per page
    //set the id
    //set the text for the div which is what we want to show up in the pop up</span>
    divToAdd = document.createElement(<span style="color: #ff0000;">'span'</span>);
    divToAdd.setAttribute(<span style="color: #ff0000;">'name'</span>, spanName);
    divToAdd.id = spanName;
    divToAdd.innerHTML = innerText;

    <span style="color: #008000;">//Add the div to whatever element that was found.  For this post it will be a link
    //but it doesn't really matter.</span>
    parentContainer.appendChild(divToAdd);

    <span style="color: #0000ff;">return</span> divToAdd;
}</pre>
<p>So there is the building of the pop up div. Here's the method to be called by the onclick event:</p>
<pre><span style="color: #0000ff;">function</span> ShowCommentForPost(postName, parentElement, innerText)
{
    <span style="color: #0000ff;">var</span> divName;
    <span style="color: #0000ff;">var</span> createdDiv;
    <span style="color: #0000ff;">var</span> parentContainer;

    <span style="color: #008000;">//Same as before
</span>    <span style="color: #0000ff;">if</span> (typeof parentElement == 'string')
    {
        parentContainer = document.getElementById(parentElement);
    }
    <span style="color: #0000ff;">else</span>
    {
        parentContainer = parentElement;
    }
    <span style="color: #008000;">//See if the pop up div  already exists.  If it does, then don't create again
    //I didn't have this before and it would create a new div everytime
    //That's what some might call a surprise feature</span>
    divName = 'comment' + postName;
    createdDiv = document.getElementById(divName);
    <span style="color: #008000;">//Ooops, the div didn't exist, create it and add the hide class</span>
    <span style="color: #0000ff;">if</span> (createdDiv == <span style="color: #0000ff;">null</span>)
    {
        createdDiv = BuildSelectableSpanForPost(divName, parentContainer, innerText);
        <span style="color: #008000;">//this is a method found on </span><a href="http://byatool.com/index.php/ui/add-remove-or-replace-a-css-class-using-javascript"><span style="color: #008000;">this post</span></a>
        ClassHandler.AddClass(createdDiv, 'hidePostComment');
    }

    <span style="color: #008000;">//this is a method based off </span><a href="http://byatool.com/index.php/ui/add-remove-or-replace-a-css-class-using-javascript"><span style="color: #008000;">this post</span></a>
<span style="color: #008000;">    //As you can guess it will show or hide depending on which class it already has.</span>
    ShowHideElementBasedOnCss(createdDiv);

    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;
}</pre>
<p>To start, there is the code to create the actual div.</p>
<p>Now for the actual use:</p>
<pre><span style="color: #800000;">&lt;a</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="return ShowCommentForPost('1', this, 'THIS IS SO STUPID!');"</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="www.byatool.com"</span><span style="color: #800000;">&gt;</span>word.<span style="color: #800000;">&lt;/a&gt;</span></pre>
<p>    Pretty easy to actually use right? The actual location doesn't really matter since it the method will always return false and therefore the link will never redirect.  Also, you'll see that I put 1 as the name sent in.  The name sent in doesn't matter what it is, but for every link it has to be different.  If you are using this in a blog situation where there could be multiple blog posts in one page, I would suggest the name sent in would be the title and an increasing number.</p>
<p>    If you got to this point and feel robbed of five minutes in your life, well just be happy this post robbed me of 15 minutes of mine.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/add-remove-or-replace-a-css-class-using-javascript/" title="Add, Remove, or Replace a CSS Class Using Javascript">Add, Remove, or Replace a CSS Class Using Javascript</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li><li><a href="http://byatool.com/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/" title="Use Javascript and an HttpHandler to Load an Image from a Database">Use Javascript and an HttpHandler to Load an Image from a Database</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add, Remove, or Replace a CSS Class Using Javascript</title>
		<link>http://byatool.com/ui/add-remove-or-replace-a-css-class-using-javascript/</link>
		<comments>http://byatool.com/ui/add-remove-or-replace-a-css-class-using-javascript/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 21:02:04 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=525</guid>
		<description><![CDATA[Now honestly, I think the all famous Prototype has something for this, but if you aren't using the all famous Prototype... well you're screwed. Until now. This is the idea, you want show or hide something on a click of it. &#60;div onclick="HideOrShowMe();"&#62; YAYAAYAYAY! &#60;/div&#62; Annoying thing is trying to keep up with whether it's [...]]]></description>
			<content:encoded><![CDATA[<p>Now honestly, I think the all famous Prototype has something for this, but if you aren't using the all famous Prototype... well you're screwed. Until now.</p>
<p>This is the idea, you want show or hide something on a click of it.</p>
<pre>    &lt;div onclick="HideOrShowMe();"&gt;
      YAYAAYAYAY!
    &lt;/div&gt;</pre>
<p>Annoying thing is trying to keep up with whether it's hidden or not. Now there are ways to do this for sure, but if you had them you wouldn't be here... or you just love idiotic banter. Either way, you're here.</p>
<p>To start, get a class going:</p>
<pre>    if (<span style="color: #0000ff;">typeof</span> ClassHandler != <span style="color: #ff0000;">'object'</span>)
    {
      ClassHandler = <span style="color: #0000ff;">new</span> Object();
    }</pre>
<p>Normal declaration. Yeehaw. Now we need a method to check for the class:</p>
<pre>ClassHandler.CheckForClass = <span style="color: #0000ff;">function</span>(element, nameOfClass)
{
    <span style="color: #0000ff;">var</span> returnValue = <span style="color: #0000ff;">false</span>;

    <span style="color: #008000;">//Check to see if the element variable is a string or (hopefully) a control.
    //If it is a string, get the control that belongs to that id.
</span>    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">typeof</span> element == <span style="color: #ff0000;">'string'</span>)
    {
        element = document.getElementById(element);
    }

    <span style="color: #008000;">//next you use a regular expression to check the className property for
    //  the class you want.  If it finds a match, you're good to go.</span>
    <span style="color: #0000ff;">if</span> (element.className != <span style="color: #ff0000;">''</span>)
    {
        returnValue = <span style="color: #0000ff;">new</span> RegExp(<span style="color: #ff0000;">'\\b'</span> + nameOfClass + <span style="color: #ff0000;">'\\b'</span>).test(element.className);
    }

    <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>Ok so now you have a method to check if it's there... but what about if you want to replace the old one with a new one? Well that could be broken into two methods first (Both that are useful). First one is the removal of the old class:</p>
<pre>ClassHandler.RemoveClass = <span style="color: #0000ff;">function</span>(element, nameOfClass)
{
    <span style="color: #0000ff;">var</span> returnValue = <span style="color: #0000ff;">false</span>;

    <span style="color: #008000;">//You know the drill
</span>    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">typeof</span> element == <span style="color: #ff0000;">'string'</span>)
    {
        element = document.getElementById(element);
    }

    <span style="color: #008000;">//Hey cool, I just used the CheckForClass method
</span>    <span style="color: #0000ff;">if</span> (ClassHandler.CheckForClass(element, nameOfClass))
    {
        <span style="color: #008000;">//Real simple, replace the old
        //But you have to check if the nameOfClass exists with a preceding space
        //If it does, then you replace that with a space, other wise just replace
        //the nameOfClass with a space.
</span>        element.className = element.className.replace((element.className.indexOf(<span style="color: #ff0000;">' '</span> + nameOfClass) &gt;= 0 ? <span style="color: #ff0000;">' '</span> + nameOfClass : nameOfClass),<span style="color: #ff0000;">''</span>);
        returnValue = true;
    } 

    <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>And now you'll have to add in the new one.</p>
<pre>ClassHandler.AddClass = function(element, nameOfClass)
{
    <span style="color: #0000ff;">var</span> returnValue = <span style="color: #0000ff;">false</span>;
    <span style="color: #008000;">//lalalala</span>
    <span style="color: #0000ff;">if</span> (typeof element == <span style="color: #ff0000;">'string'</span>)
    {
        element = document.getElementById(element);
    }
    <span style="color: #008000;">//If className already has a value, precede the nameOfClass with a space
    // otherwise just add it in.
</span>    <span style="color: #0000ff;">if</span> (!ClassHandler.CheckForClass(element, nameOfClass))
    {
        element.className += (element.className ?  <span style="color: #ff0000;">'  '</span>  :  <span style="color: #ff0000;">''</span>) + nameOfClass;
        returnValue = true;
    } 

    <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>Now for the replace:</p>
<pre>ClassHandler.ReplaceClass = function(element, classToRemove, classToAdd)
{
    <span style="color: #0000ff;">var</span> returnValue = <span style="color: #0000ff;">false</span>;

    <span style="color: #008000;">//african elephants only have four teeth for chewing their food.
</span>    <span style="color: #0000ff;">if</span> (typeof element == <span style="color: #ff0000;">'string'</span>)
    {
        element = document.getElementById(element);
    }

    <span style="color: #008000;">//Remove the old one
    //Add the new one
</span>    if(ClassHandler.CheckForClass(element, classToRemove))
    {
        ClassHandler.RemoveClass(element, classToRemove);
        ClassHandler.AddClass(element, classToAdd);
        returnValue = <span style="color: #0000ff;">true</span>;
    }

    <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>Now you might be thinking, "Tool, why don't you just use the replace method in the replace method." Well I suppose you could, but this way you have two other methods at your disposal for future use. Up to you really, part of this was because I actually needed those methods and the title says "Add, Remove, or Replace". I can't go against the title. I don't have that kind of strength.</p>
<p>Usage? Well there are couple ways you can do this.</p>
<pre>    if(ClassHandler.CheckForClass(<span style="color: #ff0000;">'someDiv'</span>, <span style="color: #ff0000;">'hide'</span>))
    {
       ClassHandler.ReplaceClass(<span style="color: #ff0000;">'someDiv'</span>, <span style="color: #ff0000;">'hide'</span>, <span style="color: #ff0000;">'show'</span>);
    }
    else
    {
       ClassHandler.ReplaceClass(<span style="color: #ff0000;">'someDiv'</span>, <span style="color: #ff0000;">'show'</span>, <span style="color: #ff0000;">'hide'</span>);
    }</pre>
<p>or this:</p>
<pre>    if(!ClassHandler.ReplaceClass(<span style="color: #ff0000;">'someDiv'</span>, <span style="color: #ff0000;">'hide'</span>, <span style="color: #ff0000;">'show'</span>))
    {
       ClassHandler.ReplaceClass(<span style="color: #ff0000;">'someDiv'</span>, <span style="color: #ff0000;">'show'</span>, <span style="color: #ff0000;">'hide'</span>));
    }</pre>
<p>And other ways to be sure. Now I'm not saying this is the best way to do it, just a decent way... although looking back on this I wonder if I could clean up the regex stuff.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/" title="Add a Pop Up Div to a Link Dynamically">Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li><li><a href="http://byatool.com/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/" title="Use Javascript and an HttpHandler to Load an Image from a Database">Use Javascript and an HttpHandler to Load an Image from a Database</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/add-remove-or-replace-a-css-class-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Hit: Hiding Horizontal or Verticle scroll using CSS</title>
		<link>http://byatool.com/ui/quick-hit-hiding-horizontal-or-verticle-scroll-using-css/</link>
		<comments>http://byatool.com/ui/quick-hit-hiding-horizontal-or-verticle-scroll-using-css/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 19:19:14 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=438</guid>
		<description><![CDATA[If nothing else the motto for this site is, "Why was this written?" but sometimes it can also be, "I bypass the middle man and pass the savings on to you." In this case, it has to do with a really simple way to show only on scroll bar on a section be it a [...]]]></description>
			<content:encoded><![CDATA[<p>If nothing else the motto for this site is, "Why was this written?" but sometimes it can also be, "I bypass the middle man and pass the savings on to you."  In this case, it has to do with a really simple way to show only on scroll bar on a section be it a div, span, pre tag, ect.  In fact, I actually used it to for this site.</p>
<p>Here's the problem, you have a section that you want to only show the horizontal scroll bar.  Now the scroll bar is usually set with the overflow keyword in css:</p>
<pre>  <span style="color: #993300;">overflow:auto;</span></pre>
<p>But what does this do? Well if the section you have is larger than its parent (Say a pre section in a set width div), both scroll bars come up. Sometimes that's not a bad thing but sometimes it looks really annoying if you only have two lines in it like:<br />
<img class="alignnone size-full wp-image-442" title="bat_scroll1" src="http://byatool.com/wp-content/uploads/2009/02/bat_scroll1.jpg" alt="bat_scroll1" width="613" height="104" /><br />
Since it will automatically force both scroll bars. And on top of that, the scroll bars themselves now take up space and essentially cover up some of that second line. Well there's two ways you can go about this in order to at least allow for everything to be seen without a verticle scroll.</p>
<p>1)</p>
<p>You can use the overflow-x and overflow-y keywords:</p>
<pre>  {
    <span style="color: #993300;">overflow-y:hidden;</span>
    <span style="color: #993300;">overflow-x:auto;</span>
  }</pre>
<p>What this does is completely hides the verticle scroll bar. Now this will work very well if you have sections that are of a specific height. Problem is if the height isn't set specifically, it will just not show the parts that would normally need the verticle scroll for. (Happens sometimes)</p>
<p><img class="alignnone size-full wp-image-443" title="bat_scroll2" src="http://byatool.com/wp-content/uploads/2009/02/bat_scroll2.jpg" alt="bat_scroll2" width="598" height="86" /></p>
<p>This brings us to the second solution:</p>
<p>B)</p>
<p>Padding.</p>
<pre>  <span style="color: #993300;">padding:8px 0px 40px 0px;</span>
  <span style="color: #993300;">overflow-x:auto;</span>
  <span style="color: #993300;">overflow-y:hidden;</span></pre>
<p>Really simple addition that will basically push the bottom of the container down so that the writing that was being hidden before (Because the scroll bar didn't exist) is now pushed upwards causing no loss of stuff.</p>
<p><img class="alignnone size-full wp-image-441" title="bat_scroll3" src="http://byatool.com/wp-content/uploads/2009/02/bat_scroll3.jpg" alt="bat_scroll3" width="604" height="93" /></p>
<p>As you can see, now everything can be... eh seen.  Sure you get that annoying bit of space, but the 40px I'm using could be a bit of overkill. You really just have to shoot for a number that allows the bottom to be shown. But hey, life is tough.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/lessons/force-embeded-video-behind-a-jquery-modal-dialog/" title="Force Embeded Video Behind A JQuery Modal Dialog">Force Embeded Video Behind A JQuery Modal Dialog</a></li><li><a href="http://byatool.com/lessons/element-embed-is-not-supported-embedding-youtube-video-without-the-embed-tag/" title="Element &#8216;Embed&#8217; Is Not Supported: Embedding YouTube Video Without The Embed Tag">Element &#8216;Embed&#8217; Is Not Supported: Embedding YouTube Video Without The Embed Tag</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/" title="Add a Pop Up Div to a Link Dynamically">Add a Pop Up Div to a Link Dynamically</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/quick-hit-hiding-horizontal-or-verticle-scroll-using-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My jQuery Primer</title>
		<link>http://byatool.com/ui/my-jquery-primer/</link>
		<comments>http://byatool.com/ui/my-jquery-primer/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 21:45:15 +0000</pubDate>
		<dc:creator>Andre</dc:creator>
				<category><![CDATA[.Net Issues]]></category>
		<category><![CDATA[Plain Text]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=372</guid>
		<description><![CDATA[So you have been reading about jQuery and want to dive in and try some? I recently attended the MSDN Dev Conference in Detroit where jQuery integration and client-side programming were very hot topics. With Microsoft's acceptance of the open source library, I figured I would give it a try. This is what I have [...]]]></description>
			<content:encoded><![CDATA[<p>So you have been reading about jQuery and want to dive in and try some?  I recently attended the MSDN Dev Conference in Detroit where jQuery integration and client-side programming were very hot topics.  With Microsoft's acceptance of the open source library, I figured I would give it a try.  This is what I have learned so far.</p>
<p>Before I can show you what you can do with jQuery, I think I should probably show you how to get a reference to jQuery into your code.</p>
<p>In ASP.NET you can add your reference directly to your Script Manager</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>scriptmanager id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;pageScriptManager&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
	<span style="color: #006600; font-weight: bold;">&lt;</span>scripts<span style="color: #006600; font-weight: bold;">&gt;</span>
		<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>scriptreference path<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;/client/jquery-1.3.1.min.js&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
	<span style="color: #006600; font-weight: bold;">&lt;/</span>scripts<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">:</span>scriptmanager<span style="color: #006600; font-weight: bold;">&gt;</span></pre></div></div>

<p>What does jQuery have to offer?  First and foremost, jQuery has given me power over the Document Object Model (DOM)!  jQuery Selectors are the greatest thing since sliced bread, no lie.  Being able to EASILY find an object or group of objects in the DOM by ID, CSS Class, or by HTML Tag is something web developers have long needed.</p>
<p>To select an object from the DOM by ID would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ID_Goes_Here'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>To select an object from the DOM by CSS Class would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.CSS_Class_Name_Goes_Here'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>To select an object from the DOM by HTML Tag would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;Tag_Goes_Here&gt;'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>With jQuery Selectors being so simple, it allows the developer to easily select an object or a group of objects.  Now that we can select objects, what can we do with them?  This is where the power of Selectors really builds into what else you can do.  In a web application, round trips to the server to manage UI is wasteful.  I avoid JavaScript like the plague because it's a pain in the ass.  jQuery makes client-side UI management feel like climbing the rope in gym class.</p>
<p>For example, if I have a label that I want to be rendered but not visible I could create the label.</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>label id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Label4&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> cssclass<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myLabel&quot;</span> text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;This is &quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span></pre></div></div>

<p>And later on in jQuery I can hide it like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#Label4'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It's nice to be able to easily select an object and modify it, but what if you have a whole group of items you want to modify?  With jQuery, you can EASILY loop through a collection of objects.<br />
In this example I have a group of labels.</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>label id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Label1&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> cssclass<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myLabel&quot;</span> text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;This is &quot;</span>  <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>label id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Label2&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> cssclass<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myLabel&quot;</span> text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;This is &quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>label id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Label3&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> cssclass<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myLabel&quot;</span> text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;This is &quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>label id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Label4&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> cssclass<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myLabel&quot;</span> text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;This is &quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span></pre></div></div>

<p>Now I want to update the text of each label to include its ID.  I am going to loop through each object in the DOM with a CSS Class of .myLabel.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.myLabel'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What the jQuery code above does is execute a function that will append the object's ID to its text value.  Notice the Selector inside the function $(this).  The syntax is used to find the loop's current object in the DOM.</p>
<p>I do a lot of web work where I create controls on the fly.  For my last example, I just wanted to show a way to quickly insert some HTML into a container object.</p>
<p>I will start with a Panel AKA a DIV.</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>panel id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Panel1&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span></pre></div></div>

<p>Now I am going to use jQuery to select my DIV and add some HTML to it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Panel1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;ul&gt;&lt;li&gt;Item One&lt;/li&gt;&lt;li&gt;Item 2&lt;/li&gt;&lt;/ul&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now I have shown you some snippets here and there, let me show you what my page actually looks like.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> &gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>jQuery Examples<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/style/jQuery.css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
			function pageLoad() {
				$('.myLabel').each(function() { $(this).append(this.id); });
				$('.myLabel').css('color', '#FFFFFF').css('font-size', '30px');
				$('#Label4').css('display', 'none');
				$(&quot;#Panel1&quot;).html(&quot;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item One<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>&quot;);
			}
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pageForm&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;asp:scriptmanager <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pageScriptManager&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;scripts&gt;</span>
					<span style="color: #009900;">&lt;asp:scriptreference path<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/client/jquery-1.3.1.min.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>scripts&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>asp:scriptmanager&gt;</span>
&nbsp;
			<span style="color: #009900;">&lt;asp:<span style="color: #000066;">label</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Label1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> cssclass<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myLabel&quot;</span> <span style="color: #000066;">text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;This is &quot;</span>  <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
			<span style="color: #009900;">&lt;asp:<span style="color: #000066;">label</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Label2&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> cssclass<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myLabel&quot;</span> <span style="color: #000066;">text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;This is &quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
			<span style="color: #009900;">&lt;asp:<span style="color: #000066;">label</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Label3&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> cssclass<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myLabel&quot;</span> <span style="color: #000066;">text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;This is &quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
			<span style="color: #009900;">&lt;asp:<span style="color: #000066;">label</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Label4&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> cssclass<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myLabel&quot;</span> <span style="color: #000066;">text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;This is &quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
			<span style="color: #009900;">&lt;asp:panel <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Panel1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><strong>Links</strong><br />
jQuery: <a href="http://www.jQuery.com/" target="_blank">http://www.jQuery.com/</a><br />
Document Object Model: <a href="http://en.wikipedia.org/wiki/Document_Object_Model" target="_blank">http://en.wikipedia.org/wiki/Document_Object_Model</a></p>
<p>Twitter: http://www.twitter.com/RockCityGhost</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li><li><a href="http://byatool.com/ui/jquery-modal-dialog-hide-that-stupid-x-button-windows-close-button/" title="jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button">jQuery Modal Dialog : Hide That Stupid X Button / Windows Close Button</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/my-jquery-primer/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Javascript Anonymous Methods and How they help Ajax WebMethod</title>
		<link>http://byatool.com/ui/javascript-anonymous-methods-and-how-they-help-ajax-webmethod/</link>
		<comments>http://byatool.com/ui/javascript-anonymous-methods-and-how-they-help-ajax-webmethod/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 20:34:46 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[WebMethod]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=364</guid>
		<description><![CDATA[Ok so here's what you're trying to do: You have a button and when it's pressed you want to have it fill a drop down list without a postback. So your first thoughts are, "I like Mentos" followed by, "WebMethod" since you of course read this super awesome post. Now it might look like this: [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so here's what you're trying to do:</p>
<p>You have a button and when it's pressed you want to have it fill a drop down list without a postback. So your first thoughts are, "I like Mentos" followed by, "WebMethod" since you of course read <a href="http://byatool.com/?p=351" target="_new">this super awesome post</a>. Now it might look like this:</p>
<pre>        <span style="color: #0000ff;">function</span> GetUserList()
        {
            PageMethods.GetUserList(OnGetUserListComplete);
        }

        <span style="color: #0000ff;">function</span> OnGetUserListComplete(result)
        {
            <span style="color: #0000ff;">var</span> dropdownList = $get('dropdownListID');
            <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i = 0; i &lt; result.length; i++)
            {
                <span style="color: #0000ff;">var</span> item;
                item = document.createElement(<span style="color: #800000;">"option"</span>);
                item.text = result[i].UserName;
                item.value = result[i].UserID;
                dropdownList.options.add(item);
            }
        }</pre>
<p>And GetUserList would be called on click. Fine and dandy, but this sort of bad since you're hardcoding the id of the control. Meh. You look back at the method and see that there just isn't a way to pass in a name, and if you didn't you would be sooooo wrong it hurts. Shame on you. Good thing I'm here. The way you're going to do this is to use an anonymous method. You see, as it is the PageMethod call needs a method to call post completion and that's all it takes in. (Well not completely true, it can take in parameters for the server side method, but that's not the point.) So you have to find a way for that PageMethod to call the method after completion AND make sure it has the list name with it.</p>
<pre>        <span style="color: #0000ff;">function</span> GetUserList(dropdownList)
        {
            PageMethods.GetUserList(function(result) {
                 OnGetUserListComplete(result, dropdownList); });
        }

        <span style="color: #0000ff;">function</span> OnGetUserListComplete(result, dropdownList)
        {
            <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i = 0; i &lt; result.length; i++)
            {
                <span style="color: #0000ff;">var</span> item;
                item = document.createElement(<span style="color: #800000;">"option"</span>);
                item.text = result[i].UserName;
                item.value = result[i].UserID;
                dropdownList.options.add(item);
            }
        }</pre>
<p>Ooo pretty tricky huh?</p>
<pre>        PageMethods.GetUserList(function(result) {
                 OnGetUserListComplete(result, dropdownList); });</pre>
<p>See what I did there, I created an anonymous method to be called on completion and in turn it calls the original (Though modified) OnGetUserListComplete method now. And you thought javascript sucked.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/call-server-side-methods-with-javascript-in-aspnet-yeah-its-in-there/" title="Call Server Side methods With Javascript in ASP.Net&#8230; Yeah Ajax">Call Server Side methods With Javascript in ASP.Net&#8230; Yeah Ajax</a></li><li><a href="http://byatool.com/pontification/ajax-webmethods-javascript-and-anonymous-types-booyah/" title="Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah ">Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah </a></li><li><a href="http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/" title="Return False from a ScriptControl method and why I hate Firefox&#8230;">Return False from a ScriptControl method and why I hate Firefox&#8230;</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/javascript-anonymous-methods-and-how-they-help-ajax-webmethod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Call Server Side methods With Javascript in ASP.Net&#8230; Yeah Ajax</title>
		<link>http://byatool.com/ui/call-server-side-methods-with-javascript-in-aspnet-yeah-its-in-there/</link>
		<comments>http://byatool.com/ui/call-server-side-methods-with-javascript-in-aspnet-yeah-its-in-there/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 21:25:04 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[WebMethod]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=351</guid>
		<description><![CDATA[So I've been looking for something to match the simplicity of the old Ajax.dll WebMethods (The ability to asychronously call a method on a class from javascript) since the project was abandoned by the owner. A lot of what I saw before involved WebServices which wasn't horrible, but I wanted Ajax.WebMethods. Come to find out, [...]]]></description>
			<content:encoded><![CDATA[<p>So I've been looking for something to match the simplicity of the old Ajax.dll WebMethods (The ability to asychronously call a method on a class from javascript) since the project was abandoned by the owner. A lot of what I saw before involved WebServices which wasn't horrible, but I wanted Ajax.WebMethods. Come to find out, this simplicity exists in 3.5.</p>
<p>So let's say I want to click on a div and have it's innerHTML be filled with some string. Sounds like a completely viable situation for any money making business... Anyways, I could do some kind of post back and fill it, or I could use an update panel (Uhg) or I could use a Web Service or I could just bang my head on the desk and get the same level of satisfaction. But then came 3.5 and something wonderful. Take this page:</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">Test</span> : System.Web.UI.<span style="color: #008080;">Page</span>
    {
        [System.Web.Services.<span style="color: #008080;">WebMethod</span>]
        [System.Web.Script.Services.<span style="color: #008080;">ScriptMethod</span>]
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">String</span> GiveMeString()
        {
            <span style="color: #0000ff;">return</span> <span style="color: #800000;">"hi"</span>;
        }
    }</pre>
<p>See somethig weird? Yeah that's the magic of the class file. All I need is:</p>
<ul>
<li>A Static Method</li>
<li>The WebMethod Attribute</li>
<li>The ScriptMethod attribute</li>
<li>Optionally something to return, but for this example it's not optional just like Burt Renold's mustache</li>
</ul>
<p>And now the page file is ready, but what about the markup? Well, that's next, so glad you asked. First the javascript:</p>
<pre>        <span style="color: #0000ff;">function</span> ShowString()
        {
            <span style="color: #0000ff;">var</span> test = $get(<span style="color: #800000;">'ShowThings'</span>);
            PageMethods.GiveMeString(OnGiveMeStringComplete);
        }

        <span style="color: #0000ff;">function</span> OnGiveMeStringComplete(result)
        {
            <span style="color: #0000ff;">var</span> test = $get(<span style="color: #800000;">'ShowThings'</span>);
            test.innerHTML = result;
        }</pre>
<p>First method is the method to call. The PageMethods object will allow the use of the class method that was created. The parameter being sent in is actually the method needing to be called when the server side call is complete. With that in mind, guess what the second method is... No, not ice cream. It's the method called when the server call is compete. As you can see, I am simply changing the innerHTML of a div.</p>
<pre>    &lt;<span style="color: #800000;">form</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="form1"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span>&gt;
        &lt;<span style="color: #800000;">asp</span>:<span style="color: #800000;">ScriptManager</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="smMain"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">EnablePageMethods</span><span style="color: #0000ff;">="true"</span> /&gt;
        &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="ShowThings"</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="</span><span style="color: #ff0000;">background-color</span><span style="color: #0000ff;">:Yellow;</span><span style="color: #ff0000;">height</span><span style="color: #0000ff;">:20px;</span><span style="color: #ff0000;">width</span><span style="color: #0000ff;">:20px;"</span>
          <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="ShowString</span>()<span style="color: #0000ff;">"</span>&gt;&lt;/<span style="color: #800000;">div</span>&gt;
    &lt;/<span style="color: #800000;">form</span>&gt;</pre>
<p>And there you have the markup. ScriptManager is a must in this case, but that's pretty standard when dealing with Ajax in asp.net. The only other thing of note is the method call on the div's onClick. Beyond that, it's ready to go. And if you were to make this example yourself and click on the div, you will get a 'hi' string in it on the first click. Amazing.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/javascript-anonymous-methods-and-how-they-help-ajax-webmethod/" title="Javascript Anonymous Methods and How they help Ajax WebMethod">Javascript Anonymous Methods and How they help Ajax WebMethod</a></li><li><a href="http://byatool.com/pontification/ajax-webmethods-javascript-and-anonymous-types-booyah/" title="Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah ">Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah </a></li><li><a href="http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/" title="Return False from a ScriptControl method and why I hate Firefox&#8230;">Return False from a ScriptControl method and why I hate Firefox&#8230;</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/call-server-side-methods-with-javascript-in-aspnet-yeah-its-in-there/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Javascript and an HttpHandler to Load an Image from a Database</title>
		<link>http://byatool.com/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/</link>
		<comments>http://byatool.com/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 20:35:40 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[HttpHandler]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=341</guid>
		<description><![CDATA[This might be part of another ongoing series, but the for this post, right here, RIGHT NOW, I am going to show are really simple but fun (??) way to change an image's... eh image from a database stored image using Javascript and an .ashx file. And when I say simple, I mean it took [...]]]></description>
			<content:encoded><![CDATA[<p>This might be part of another ongoing series, but the for this post, right here, RIGHT NOW, I am going to show are really simple but fun (??) way to change an image's... eh image from a database stored image using Javascript and an .ashx file. And when I say simple, I mean it took me longer to get test code going than it did to make this work.</p>
<p>First you need a handler (If you don't what this is for, well for this example it allows you to create a non existant url for an image loaded from the database.) which is aptly named Generic Handler when you do the usual Add New Item. Amazing. You should get something like this in the class file:</p>
<pre>    [<span style="color: #008080;">WebService</span>(Namespace = <span style="color: #800000;">"http://tempuri.org/"</span>)]
    [<span style="color: #008080;">WebServiceBinding</span>(ConformsTo = <span style="color: #008080;">WsiProfiles</span>.BasicProfile1_1)]
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">SomeImage</span>: <span style="color: #008080;">IHttpHandler</span>
    {
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> <span style="color: #008080;">ProcessRequest</span>(<span style="color: #008080;">HttpContext</span> context)
        {
            context.Response.ContentType = <span style="color: #800000;">"text/plain"</span>;
            context.Response.Write(<span style="color: #800000;">"Hello World"</span>);
        }

        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> IsReusable
        {
            <span style="color: #0000ff;">get</span>
            {
                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;
            }
        }
    }</pre>
<p>Yeah you like that don't you? Yeah you do.</p>
<p>So now we have something to display the image right? Well that's pretty easy too. For me, I have an UserImage class I created with Linq to Sql to mimic my UserImage table. I'm cool like that. I then created a method to return the image bytes based on the ID sent in. That part is up to you how to handle. The main thing you need is to get the image bytes somehow. With that in mind, here is what the class file looks like now:</p>
<pre>    [<span style="color: #008080;">WebService</span>(Namespace = <span style="color: #800000;">"http://tempuri.org/"</span>)]
    [<span style="color: #008080;">WebServiceBinding</span>(ConformsTo = <span style="color: #008080;">WsiProfiles</span>.BasicProfile1_1)]
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ShowImage</span> : <span style="color: #008080;">IHttpHandler</span>
    {
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ProcessRequest(<span style="color: #008080;">HttpContext</span> context)
        {
            <span style="color: #008080;">Int32</span> imageID;
            <span style="color: #008080;">Byte</span>[] imageBytes;

            imageID = Convert.ToInt32(context.Request.QueryString[<span style="color: #800000;">"ImageID"</span>]);
            imageBytes = <span style="color: #008080;">UserImage</span>.GetImageBytesById(imageID);
            context.Response.ContentType = "image/jpeg";
            context.Response.Cache.SetCacheability(<span style="color: #008080;">HttpCacheability</span>.Public);
            context.Response.BufferOutput = false;
            context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
        }

        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> IsReusable
        {
            <span style="color: #0000ff;">get</span>
            {
                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;
            }
        }
    }</pre>
<p>As you can see, the query string has an id being sent in and I'm retrieving it. From there I get the image bytes, tell the context what it is, how to handle the cache, and sending the bytes out. What you can't see right now is that somewhere I have something that looks like this:</p>
<pre>   &lt;<span style="color: #800000;">image</span> <span style="color: #ff0000;">src</span><span style="color: #0000ff;">="SomeImage.ashx?ImageID=1"</span> /&gt;</pre>
<p>When that url is read, it will send it off to the handler to get and display the image. (Not taking caching into account mnd you)</p>
<p>Now I know what you're thinking right now, "I'm bored" which I understand but you're also thinking, "Where's the f@&amp;#ing javascript?". Well my vulgar friend, it's on the way.</p>
<pre>&lt;<span style="color: #800000;">head</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> &gt;
        &lt;<span style="color: #800000;">script</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span> <span style="color: #ff0000;">language</span><span style="color: #0000ff;">="javascript"</span>&gt;
        <span style="color: #0000ff;">function</span> TestThis(name, imageID)
        {
            <span style="color: #0000ff;">var</span> test = document.getElementById(name);
            test.src = <span style="color: #800000;">'ShowImage.ashx?imageID='</span> + imageID;
        }
    &lt;<span style="color: #800000;">/script</span>&gt;
&lt;<span style="color: #800000;">/head</span>&gt;
&lt;<span style="color: #800000;">body</span>&gt;
    &lt;<span style="color: #800000;">form</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="form1"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span>&gt;
    &lt;<span style="color: #800000;">div</span>&gt;
        &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="</span><span style="color: #ff0000;">background-color</span><span style="color: #0000ff;">:Gray;</span><span style="color: #ff0000;">height</span><span style="color: #0000ff;">:20px;</span><span style="color: #ff0000;">width</span><span style="color: #0000ff;">:20px;"</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="TestThis('hi', 1);"</span>&gt; &lt;/<span style="color: #800000;">div</span>&gt;
        &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="</span><span style="color: #ff0000;">background-color</span><span style="color: #0000ff;">:Red;</span><span style="color: #ff0000;">height</span><span style="color: #0000ff;">:20px;</span><span style="color: #ff0000;">width</span><span style="color: #0000ff;">:20px;"</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="TestThis('hi', 2);"</span> &gt; &lt;/<span style="color: #800000;">div</span>&gt;
        &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="</span><span style="color: #ff0000;">background-color</span><span style="color: #0000ff;">:Blue;</span><span style="color: #ff0000;">height</span><span style="color: #0000ff;">:20px;</span><span style="color: #ff0000;">width</span><span style="color: #0000ff;">:20px;"</span> <span style="color: #ff0000;">onclick</span><span style="color: #0000ff;">="TestThis('hi', 3);"</span>&gt; &lt;/<span style="color: #800000;">div</span>&gt;
        &lt;<span style="color: #800000;">br</span> /&gt;
        &lt;<span style="color: #800000;">img</span> <span style="color: #ff0000;">src</span><span style="color: #0000ff;">=""</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="hi"</span> <span style="color: #ff0000;">name</span><span style="color: #0000ff;">="hi"</span> /&gt;
    &lt;/<span style="color: #800000;">div</span>&gt;
    &lt;/<span style="color: #800000;">form</span>&gt;
&lt;/<span style="color: #800000;">body</span>&gt;</pre>
<p>So now every time one of the three divs is "clicked", the image is changed depending on the image id sent in using the "url" of the handler you created.  Much like sending information to another page, you send the id to the handler so it can display the correct image.   </p>
<p>I realize this isn't the best of examples but it's a push in the right direction. Maybe next time you want something, you won't swear at me.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li><li><a href="http://byatool.com/ui/add-a-pop-up-div-to-a-link-dynamically/" title="Add a Pop Up Div to a Link Dynamically">Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/add-remove-or-replace-a-css-class-using-javascript/" title="Add, Remove, or Replace a CSS Class Using Javascript">Add, Remove, or Replace a CSS Class Using Javascript</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax Control Library: Autocomplete Control to Scriptcontrol</title>
		<link>http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/</link>
		<comments>http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 16:08:06 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax Toolkit]]></category>
		<category><![CDATA[Autocomplete]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ScriptControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=329</guid>
		<description><![CDATA[[Part One] [Part Two] [Part Three] [Part Four] [Part Five] Ok so in part four I showed you how to create a webcontrol from the AutoComplete control, so now it's time to take the first four lessons and combine them. Now it's time to have an autocomplete script control. The first part is really simple, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/?p=219">[Part One]</a> <a href="http://byatool.com/?p=231">[Part Two]</a> <a href="http://byatool.com/?p=250">[Part Three]</a> <a href="http://byatool.com/?p=244">[Part Four]</a> <a href="http://byatool.com/?p=329">[Part Five]</a></p>
<p>Ok so in <a href="http://byatool.com/?p=244">part four</a> I showed you how to create a webcontrol from the AutoComplete control, so now it's time to take the first four lessons and combine them.  Now it's time to have an autocomplete script control.   The first part is really simple, just like before you have to:</p>
<ul>
<li>Inherit from ScriptControl</li>
<li>Override the GetScriptDescriptors and GetScriptReferences methods</li>
<li>Create the .js file</li>
<li>Make the .js file an embedded resource</li>
<li>Update the assemblyinfo file in the Properties folder</li>
<li>Add the minimal needed javascript</li>
</ul>
<p>So it will look something like this:</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">AutocompleteScriptControl</span> : <span style="color: #008080;">ScriptControl</span>, <span style="color: #008080;">INamingContainer</span>
{
  <span style="color: #0000ff;">private</span> <span style="color: #008080;">AutoCompleteExtender</span> autoComplete;
  <span style="color: #0000ff;">private</span> <span style="color: #008080;">TextBox</span> textboxTarget;

  <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> CreateChildControls()
  {
    base.CreateChildControls();

    autoComplete = <span style="color: #0000ff;">new</span> <span style="color: #008080;">AutoCompleteExtender</span>();
    autoComplete.ID = <span style="color: #800000;">"autoCompleteMain";</span>

    textboxTarget = <span style="color: #0000ff;">new</span> <span style="color: #008080;">TextBox</span>();
    textboxTarget.ID = <span style="color: #800000;">"textboxTarget"</span>;

    Controls.Add(textboxTarget);
    Controls.Add(autoComplete);
  }

  <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnPreRender(<span style="color: #008080;">EventArgs</span> e)
  {
    base.OnPreRender(e);
    autoComplete.TargetControlID = textboxTarget.ID;
  }

  <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #008080;">IEnumerable&lt;ScriptDescriptor&gt;</span> GetScriptDescriptors()
  {
    <span style="color: #008080;">ScriptControlDescriptor</span> desc =
      <span style="color: #0000ff;">new</span> <span style="color: #008080;">ScriptControlDescriptor</span><span style="color: #800000;">("Test.Examples.Client.AutocompleteScriptControl"</span>, ClientID);
    desc.AddProperty<span style="color: #800000;"><span style="color: #003366;">(</span>"textboxTargetID"</span>, textboxTarget.ClientID);
    desc.AddProperty(<span style="color: #800000;">"autocompleteID"</span>, autoComplete.ClientID);

    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #008080;">ScriptDescriptor</span>[] { desc };
  }

  <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #008080;">IEnumerable</span>&lt;<span style="color: #008080;">ScriptDescriptor</span>&gt; GetScriptReferences()
  {
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> [] { <span style="color: #0000ff;">new</span> <span style="color: #008080;">ScriptReference</span>
      (<span style="color: #800000;">"Test.Examples.AutoCompleteExample.AutocompleteScriptControl.js"</span>, <span style="color: #800000;">"Test.Examples"</span>) };
  }

}</pre>
<p>And the script file:</p>
<pre><span style="color: #0000ff;">if</span> (Type)
{
    Type.registerNamespace(<span style="color: #800000;">"Test.Examples.Client"</span>);
}

Test.Examples.Client.AutocompleteScriptControl = function(element)
{
    Test.Examples.Client.AutocompleteScriptControl.initializeBase(this, [element]);

    <span style="color: #0000ff;">this</span>._autocompleteID = <span style="color: #ff0000;">""</span>;
    <span style="color: #0000ff;">this</span>._textboxTargetID = <span style="color: #ff0000;">""</span>;

    <span style="color: #0000ff;">this</span>._autoComplete = <span style="color: #0000ff;">null</span>;
    <span style="color: #0000ff;">this</span>._textboxTarget = <span style="color: #0000ff;">null</span>;
}

Test.Examples.Client.AutocompleteScriptControl.prototype =
{
    get_autocompleteID: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>._autocompleteID;
    },

    set_autocompleteID: <span style="color: #0000ff;">function</span>(value)
    {
        <span style="color: #0000ff;">this</span>._autocompleteID = value;
    },

    get_textboxTargetID: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>._textboxTargetID;
    },

    set_textboxTargetID: <span style="color: #0000ff;">function</span>(value)
    {
        <span style="color: #0000ff;">this</span>._textboxTargetID = value;
    },

    initialize: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #0000ff;">this</span>._autoComplete = $get(<span style="color: #0000ff;">this</span>._autocompleteID);
        <span style="color: #0000ff;">this</span>._textboxTarget = $get(<span style="color: #0000ff;">this</span>._textboxTargetID);

        Test.Examples.Client.AutocompleteScriptControl.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #ff0000;">'initialize'</span>);
    },

    dispose: function()
    {
        Test.Examples.Client.AutocompleteScriptControl.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #ff0000;">'dispose'</span>);
    }
}

/***********************************/

Test.Examples.Client.AutocompleteScriptControl.registerClass
    (<span style="color: #ff0000;">'Test.Examples.Client.AutocompleteScriptControl'</span>, Sys.UI.Control);</pre>
<p>And now this is the bare minimum needed to get this done.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/" title="Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)">Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)</a></li><li><a href="http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/" title="Return False from a ScriptControl method and why I hate Firefox&#8230;">Return False from a ScriptControl method and why I hate Firefox&#8230;</a></li><li><a href="http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/" title="Make an Autocomplete Control a Webcontrol">Make an Autocomplete Control a Webcontrol</a></li><li><a href="http://byatool.com/ui/ajax-control-toolkit-autocomplete-how-to-use-simple-example/" title="Ajax Control Toolkit Autocomplete &#8211; How to use&#8230; simple example.">Ajax Control Toolkit Autocomplete &#8211; How to use&#8230; simple example.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Markup Property Collection for a WebControl</title>
		<link>http://byatool.com/ui/dynamic-markup-property-collection-for-a-webcontrol/</link>
		<comments>http://byatool.com/ui/dynamic-markup-property-collection-for-a-webcontrol/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 21:37:53 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ScriptControl]]></category>
		<category><![CDATA[WebControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=317</guid>
		<description><![CDATA[I wasn't exactly sure how to write this title as it's not easy to explain in a short sentence, however I can say that I am completely embarrassed by the result and may have to consider hiring a title consultant. However, for now it will have to do. Ok so what is this about? Well [...]]]></description>
			<content:encoded><![CDATA[<p>I wasn't exactly sure how to write this title as it's not easy to explain in a short sentence, however I can say that I am completely embarrassed by the result and may have to consider hiring a title consultant.  However, for now it will have to do.</p>
<p>Ok so what is this about? Well let's say you have the control from the <a href="http://byatool.com/?p=283" target="new">last example</a> and you don't want a list of controls in the markup, but maybe you want a list of strings set in the markup. (Say for a list of settings) Or even better, let's say you have controls already on the page but you want  our ParentControl (Yes I said "our" but no I'm not proposing... yet) to do something with those controls. Say we want to have the ParentControl to assign some javascript to certain buttons on the page. Ok yeah, kind of dumb but that's the example I made. I supposed you could think of it like this: You want a validation control to validate multiple controls but you want to give it the names of the controls in the markup.  Anyways, let's stick with the example I already have.</p>
<p>First off you'll need to create a new class, something that you want to hold the information in. (Although truth be told I could do something way easier and just use a list of strings, but this should prove more interesting) Here's the one I have:</p>
<pre>  <span style="color: #0000ff;">namespace</span> Test.Frontend.ControlInControl
  {
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ChildControlDefinition</span>
    {
        <span style="color: #0000ff;">public</span> <span style="color: #008080;">String</span> ControlName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    }
  }</pre>
<p>Pretty sweet? Eh? Eh? No? Ok. So now we have a class, and that's like wow. Now with the same attributes from the old example, build the WebControl:</p>
<pre>    [<span style="color: #008080;">ParseChildren</span>(<span style="color: #0000ff;">true</span>)]
    [<span style="color: #008080;">PersistChildren</span>(<span style="color: #0000ff;">false</span>)]
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ParentControl</span> : <span style="color: #008080;">UserControl</span>
    {
        <span style="color: #0000ff;">public</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">ChildControlDefinition</span>&gt; NamedControls { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    }</pre>
<p>Holy smokes, that just got way complicated compared to the last example. I added a whole new property NamedControls that is a list of the ChildControlDefinition class we created. Now you might notice that the AddedControls property from the old example isn't there anymore. I just removed it to simplify this one.</p>
<p>And then there's the markup:</p>
<pre>    &lt;<span style="color: #800000;">asp</span>:<span style="color: #800000;">Button</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="mainButton"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> /&gt;
    &lt;<span style="color: #800000;">uc1</span>:<span style="color: #800000;">ParentControl</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="ParentControl1"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> &gt;
        &lt;<span style="color: #800000;">NamedControls</span>&gt;
            &lt;<span style="color: #800000;">examples</span>:<span style="color: #800000;">ChildControlDefinition</span> <span style="color: #ff0000;">ControlName</span><span style="color: #0000ff;">="mainButton"</span> /&gt;
        &lt;/<span style="color: #800000;">NamedControls</span>&gt;
    &lt;/<span style="color: #800000;">uc1</span>:<span style="color: #800000;">ParentControl</span>&gt;</pre>
<p>Now you might notice two things here:</p>
<p>1) There is a mark up section named "NamedControls" just like the collection. Can you guess why?</p>
<p>2) examples:ChildControlDefinition : This is the class type name and the TagPrefix for where the class is. For this to work, you have to register the assembly the class is in EVEN IF the class is in the web application assembly. So it would look like this:</p>
<pre><span style="color: #000000;">&lt;%</span><span style="color: #0000ff;">@</span> <span style="color: #800000;">Register</span> <span style="color: #ff0000;">Assembly</span><span style="color: #0000ff;">="Test.Frontend"</span> <span style="color: #ff0000;">Namespace</span><span style="color: #0000ff;">="Test.Frontend.ControlInControl"</span> <span style="color: #ff0000;">TagPrefix</span><span style="color: #0000ff;">="examples"</span> %&gt;</pre>
<p>Now what does this do for anyone? Possibly it prevents you from dying a little inside but it also allows you to find those controls within the usercontrol... Ah buh? Don't worry, I'll show you!</p>
<pre>    [<span style="color: #008080;">ParseChildren</span>(<span style="color: #0000ff;">true</span>)]
    [<span style="color: #008080;">PersistChildren</span>(<span style="color: #0000ff;">false</span>)]
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ParentControl</span> : <span style="color: #008080;">UserControl</span>
    {
        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnInit(<span style="color: #008080;">EventArgs</span> e)
        {
            base.OnInit(e);
            NamedControls.ForEach(ConnectButtonToClick);
        }

        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> ConnectButtonToClick(<span style="color: #008080;">ChildControlDefinition</span> control)
        {
            <span style="color: #008080;">Button</span> foundControl;

            foundControl = Page.FindControl(control.ControlName) as <span style="color: #008080;">Button</span>;

            <span style="color: #0000ff;">if</span> (foundControl != <span style="color: #0000ff;">null</span>)
            {
                foundControl.OnClientClick = <span style="color: #800000;">"alert('clicked'); return false;"</span>;
            }
        }

        <span style="color: #0000ff;">public</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">ChildControlDefinition</span>&gt; NamedControls { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    }</pre>
<p>See what I did there? SOMETHING COMPLETELY USELESS and yes you too can now have that power. However, there might be something worth taking home from that example. From the list of ChildControlDefinition I was able to get the control name that I wanted to attach the worthless alert script to and attach it. How was that done? Well I just iterated through the ChildControlDefinition list, used the Page.FindControl method to find the control, and then set the OnClientClick method.</p>
<p>Now this example is pretty stupid but I hope you can see the overall value of this. One would be a paging controller that has a list of paging control names that it iterated through and sets various events so they can all work in harmony like one great big continuation of the Hands Across America utopia that just didn't seem to make it.</p>
<p>Now to spin it Nintendo User Manual Style:</p>
<p>Think you are bad enough for Dynamic Controls?  Take grab of the power within and dare to conquer!  Take a chance, the world can be your for the taking!  Dynamic is waiting...</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/add-controls-to-control-webcontrol-or-usercontrol-whatever-the-kids-say/" title="Add Controls to Control in ASP.Net (With Less Pulp)">Add Controls to Control in ASP.Net (With Less Pulp)</a></li><li><a href="http://byatool.com/utilities/asp-net-mvc-create-a-link-method-ie-just-give-me-the-stupid-url/" title="ASP.Net MVC: Create a link method&#8230; ie JUST GIVE ME THE STUPID URL">ASP.Net MVC: Create a link method&#8230; ie JUST GIVE ME THE STUPID URL</a></li><li><a href="http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/" title="Paging and the Entity Framework, Skip, and Take Part 4">Paging and the Entity Framework, Skip, and Take Part 4</a></li><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/dynamic-markup-property-collection-for-a-webcontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Controls to Control in ASP.Net (With Less Pulp)</title>
		<link>http://byatool.com/ui/add-controls-to-control-webcontrol-or-usercontrol-whatever-the-kids-say/</link>
		<comments>http://byatool.com/ui/add-controls-to-control-webcontrol-or-usercontrol-whatever-the-kids-say/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 22:24:32 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[UserControl]]></category>
		<category><![CDATA[WebControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=283</guid>
		<description><![CDATA[So this should be a fairly easy showing and you'll be on your way quickly. Hell most likely you won't finish this sentence before going somewhere else. BUT for those who brave this post, you will be rewarded... I hope. So here's the problem, you have a UserControl/WebControl/ExtenderControl/ScriptControl/... (Seriously?) that you want to be able [...]]]></description>
			<content:encoded><![CDATA[<p>So this should be a fairly easy showing and you'll be on your way quickly.   Hell most likely you won't finish this sentence before going somewhere else.  BUT for those who brave this post, you will be rewarded... I hope.</p>
<p>So here's the problem, you have a UserControl/WebControl/ExtenderControl/ScriptControl/... (Seriously?) that you want to be able to add controls to in the markup like thus:</p>
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">SomeControl</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ControlList</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">asp</span><span style="color: #0000ff;">:</span><span style="color: #800000;">Label</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">asp</span><span style="color: #0000ff;">:</span><span style="color: #800000;">Label</span> /<span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ControlList</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">SomeControl</span><span style="color: #0000ff;">&gt;</span></pre>
<p>As you see here, the idea is that SomeControl actually can dynamically house controls based on the markup. Seems like this should be hard, but in reality it's pretty simple. First start with creating a user control, which I hope you know how to do. (I'm calling it ParentControl) Second open up the class file and let's add some stuff.</p>
<pre>    [<span style="color: #008080;">ParseChildren</span>(<span style="color: #0000ff;">true</span>)]
    [<span style="color: #008080;">PersistChildren</span>(<span style="color: #0000ff;">false</span>)]
<span style="color: #0000ff;">    public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ParentControl</span> : <span style="color: #008080;">UserControl
</span>    {
        <span style="color: #0000ff;">public</span> <span style="color: #333333;">ParentControl</span>()
        {
            addedControls = <span style="color: #0000ff;">new</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt;();
        }

        <span style="color: #0000ff;">private</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt; addedControls;

        <span style="color: #0000ff;">public</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt; AddedControls
        {
            <span style="color: #0000ff;">get</span>
            {
                <span style="color: #0000ff;">return</span> addedControls;
            }
        }
    }</pre>
<p>And honestly, that's the code needed but I'll give a literary once over to make sure things are clear.</p>
<p>So first off you have to add a list of controls to the eh... control and create a property that is used to access it and this can actually be done verbosely:</p>
<pre>        <span style="color: #008000;">//Field
</span>        <span style="color: #0000ff;">private</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt; addedControls;

        <span style="color: #008000;">//Instantiation on constructor</span>
        <span style="color: #0000ff;">public</span> ParentControl()
        {
            addedControls = <span style="color: #0000ff;">new</span> <span style="color: #008080;">List&lt;WebControl&gt;</span>();
        }

        <span style="color: #008000;">//Property to access</span>
        <span style="color: #0000ff;">public</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt; AddedControls
        {
            <span style="color: #0000ff;">get</span>
            {
                <span style="color: #0000ff;">return</span> addedControls;
            }
        }</pre>
<p>OR the easy way (Which is not what the original example showed:</p>
<pre>        <span style="color: #0000ff;">public</span> <span style="color: #008080;">List</span>&lt;<span style="color: #008080;">WebControl</span>&gt;AddedControls { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }</pre>
<p>As you can see, the 2.0 auto property syntax will actually work for this. So if you like that, you can save yourself some typing.</p>
<p>Ok so now we have a property, and field if you choose, to handle this. Sounds way too easy right? Well the magic is in the attributes:</p>
<pre>    [<span style="color: #008080;">ParseChildren</span>(<span style="color: #0000ff;">true</span>)]
    [<span style="color: #008080;">PersistChildren</span>(<span style="color: #0000ff;">false</span>)]
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">ParentControl</span> : <span style="color: #008080;">UserControl</span></pre>
<p>And you're all set. Now the markup:</p>
<pre>    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">userControl</span>:<span style="color: #800000;">ParentControl</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="ParentControl1"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">AddedControls</span>&gt;
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">asp</span>:<span style="color: #800000;">Label</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="labelHi"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">AddedControls</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">userControl</span>:<span style="color: #800000;">ParentControl</span><span style="color: #0000ff;">&gt;</span></pre>
<p>Still waiting for this to get complicated? Well you're going to waiting for a long time because that's it. When page load comes around will now have a list with the label in it. Pretty nice huh?</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/dynamic-markup-property-collection-for-a-webcontrol/" title="Dynamic Markup Property Collection for a WebControl">Dynamic Markup Property Collection for a WebControl</a></li><li><a href="http://byatool.com/utilities/asp-net-mvc-create-a-link-method-ie-just-give-me-the-stupid-url/" title="ASP.Net MVC: Create a link method&#8230; ie JUST GIVE ME THE STUPID URL">ASP.Net MVC: Create a link method&#8230; ie JUST GIVE ME THE STUPID URL</a></li><li><a href="http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/" title="Paging and the Entity Framework, Skip, and Take Part 4">Paging and the Entity Framework, Skip, and Take Part 4</a></li><li><a href="http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/" title="Make an Autocomplete Control a Webcontrol">Make an Autocomplete Control a Webcontrol</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/add-controls-to-control-webcontrol-or-usercontrol-whatever-the-kids-say/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return False from a ScriptControl method and why I hate Firefox&#8230;</title>
		<link>http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/</link>
		<comments>http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 00:45:33 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ScriptControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=271</guid>
		<description><![CDATA[I have to admit that it might be IE that is wrong, but I don't know. That and I do kind of hate Firefox so I'll stand by the title anyhow. This is a little off the current beaten path, but does deal with the current ScriptControl theme so I thought I'd put it in [...]]]></description>
			<content:encoded><![CDATA[<p>I have to admit that it might be IE that is wrong, but I don't know. That and I do kind of hate Firefox so I'll stand by the title anyhow.</p>
<p>This is a little off the current beaten path, but does deal with the current ScriptControl theme so I thought I'd put it in here.</p>
<p>Here's the problem: You have a script control, a button, and want to have the person confirm before it's allowed to postback. Easy right? If this were the non script control way you would do this:</p>
<p><strong>Javascript:</strong></p>
<pre>    <span style="color: #0000ff;">function</span> ReturnFalse()
    {
      <span style="color: #0000ff;">var</span> confirmed = confirm(<span style="color: #800000;">'Is there anyone more awesomer that Sean?'</span>);
      <span style="color: #0000ff;">return</span> confirmed;
    }</pre>
<p><strong>Markup:</strong></p>
<pre>  &lt;<span style="color: #800000;">asp:Button</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="whoCares"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span> <span style="color: #ff0000;">OnClientClick</span><span style="color: #3366ff;">="return ReturnFalse();"</span> &gt;</pre>
<p><strong>Class Stuff:</strong></p>
<pre>  whoCares.Click += whoCares_Click;
  ...
  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> whoCares_Click(<span style="color: #0000ff;">object</span> sender, <span style="color: #33cccc;">EventArgs</span> e)
  {
    <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> NotImplementedException();
  }</pre>
<p>Basically what I've done is made it so that if it doesn't return false, an exception is thrown. Easy test. Works in both browsers right? Brilliant.</p>
<p>Now for the script control:</p>
<pre>  handleLnkDeleteButtonClick: <span style="color: #0000ff;">function</span>(e)
  {
      <span style="color: #0000ff;">var</span> confirmed = confirm(<span style="color: #800000;">'Is there anyone more awesomer that Sean?'</span>);
      <span style="color: #0000ff;">return</span> confirmed;
  },

 initialize: function()
 {
    <span style="color: #0000ff;">this</span>._lnkDeleteButton = $get(<span style="color: #0000ff;">this</span>._lnkDeleteButtonID);
    <span style="color: #0000ff;">this</span>._lnkDeleteButton.idpicker = <span style="color: #0000ff;">this</span>;

    <span style="color: #008000;">//HOOK BEGINS HERE
</span>    <span style="color: #0000ff;">this</span>._lnkDeleteButtonClick = <span style="color: #0000ff;">Function</span>.createDelegate(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">this</span>.handleLnkDeleteButtonClick);
    $addHandler(<span style="color: #0000ff;">this</span>._lnkDeleteButton, <span style="color: #800000;">"click"</span>, <span style="color: #0000ff;">this</span>._lnkDeleteButtonClick);
    <span style="color: #008000;">//END HOOK HERE</span>

    NDI.WebControls.Client.PersonalMessageTypePicker.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #800000;">'initialize'</span>);
},</pre>
<p>Now that looks ok right? The method pops up the confirmation button alert dialogue thingy and returns the answer but guess what, you're wrong. So wrong. In IE this works but in Firefox, sorry no deal. Firefox will ignore anything returned if the "return" keyword is not in the markup. Well from here there's no real markup so how the hell would I do that? Well yes Virginia there is a Santa Clause and he lives in e.preventDefault();</p>
<p>It's actually really simple, one small change in the handling method. You replace return X with e.preventDefault();</p>
<pre>  handleLnkDeleteButtonClick: <span style="color: #0000ff;">function</span>(e)
  {
    <span style="color: #0000ff;">var</span> confirmed = confirm(<span style="color: #800000;">'This will delete the currery Message category</span>
      <span style="color: #800000;">and move all messages to the Oprhan cataegory.  Allow?'</span>);
    <span style="color: #0000ff;">if</span> (!confirmed)
    {
        e.preventDefault();
    }
  },</pre>
<p>And now it works. Yes you can thank me with large donations of money or cheese.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/javascript-anonymous-methods-and-how-they-help-ajax-webmethod/" title="Javascript Anonymous Methods and How they help Ajax WebMethod">Javascript Anonymous Methods and How they help Ajax WebMethod</a></li><li><a href="http://byatool.com/ui/call-server-side-methods-with-javascript-in-aspnet-yeah-its-in-there/" title="Call Server Side methods With Javascript in ASP.Net&#8230; Yeah Ajax">Call Server Side methods With Javascript in ASP.Net&#8230; Yeah Ajax</a></li><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li><li><a href="http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/" title="Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)">Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make an Autocomplete Control a Webcontrol</title>
		<link>http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/</link>
		<comments>http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 00:44:54 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax Toolkit]]></category>
		<category><![CDATA[Autocomplete]]></category>
		<category><![CDATA[WebControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=244</guid>
		<description><![CDATA[[Part One] [Part Two] [Part Three] [Part Four] [Part Five] So in the last post I showed the easy way to use the Toolkit Autocomplete control, but it might have left you with some questions like: Do I have to keep adding a textbox everytime I want an autocomplete control? Is there an easy way [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/?p=219">[Part One]</a> <a href="http://byatool.com/?p=231">[Part Two]</a> <a href="http://byatool.com/?p=250">[Part Three]</a> <a href="http://byatool.com/?p=244">[Part Four]</a> <a href="http://byatool.com/?p=329">[Part Five]</a></p>
<p>So in the last post I showed the easy way to use the Toolkit Autocomplete control, but it might have left you with some questions like:  Do I have to keep adding a textbox everytime I want an autocomplete control?  Is there an easy way to make a composite control?  Is this guy an idiot?</p>
<p>The easy answer is: yes.</p>
<p>Now I'm not a huge fan of making web project controls (.ascx) unless there is heavy style formatting.  For the most part if its going to be usable somewhere else and it is fairly simple to represent codewise, I'll put it in a class library as a WebControl or ScriptControl.  So how is this done with the autocomplete?</p>
<p>For my examples, I'll be using the same root namespace as the Script Control examples, namely Test.Examples.AutoCompleteExample. Now that I have a wonderful folder created I'm going to go ahead and create a WebControl Class named AutoCompleteControl.</p>
<p>Now when I create this new control there are some things I will need:<br />
-Has to inherit from at least WebControl (ScriptControl example I will do later)<br />
-Has to have a textbox (The autocomplete needs a target control)<br />
-I would strongly suggest a certain amount of properties to be exposed, basically reflecting the properties on the autocomplete<br />
-I added the implementing of ITextControl mainly to relfect a .Text property but that isn't really needed. I just found this useful if you end up treating this control like a textbox for say validation purposes.</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #33cccc;">AutoCompleteControl</span> : <span style="color: #33cccc;">WebControl</span>, <span style="color: #33cccc;">INamingContainer</span>
    {
        <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">AutoCompleteExtender</span> autoComplete;
        <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">TextBox</span> textboxTarget;

        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> CreateChildControls()
        {
            base.CreateChildControls();

            autoComplete = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">AutoCompleteExtender</span>();
            autoComplete.ID = <span style="color: #800000;">"autoCompleteMain"</span>;

            textboxTarget = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">TextBox</span>();
            textboxTarget.ID = <span style="color: #800000;">"textboxTarget"</span>;

            Controls.Add(textboxTarget);
            Controls.Add(autoComplete);
        }

        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            autoComplete.TargetControlID = textboxTarget.ID;

            autoComplete.CompletionInterval = 5000;
            autoComplete.EnableCaching = EnableCaching;
            autoComplete.CompletionSetCount = CompletionSetCount;
            autoComplete.MinimumPrefixLength = MinimumPrefixLength;
            autoComplete.OnClientItemSelected = OnClientItemSelected;
            autoComplete.ServiceMethod = ServiceMethod;
            autoComplete.ServicePath = ServicePath;
        }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Int32</span> CompletionSetCount { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Boolean</span> EnableCaching { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Int32</span> MinimumPrefixLength { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">String</span> OnClientItemSelected { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">String</span> ServiceMethod { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">String</span> ServicePath { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }

        <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">String</span> Text
        {
            <span style="color: #0000ff;">get</span>
            {
                EnsureChildControls();
                <span style="color: #0000ff;">return</span> textboxTarget.Text;
            }
            <span style="color: #0000ff;">set</span>
            {
                EnsureChildControls();
                textboxTarget.Text = <span style="color: #0000ff;">value</span>;
            }
        }
    }</pre>
<p>Well hell that was easy, wasn't it? All I had to do is create the class, add the textbox and autocomplete as controls, and set a few properties. Voila, we now have a working autocomplete control. Now if you had read the last post on this (Page 3) then you might have noticed this new property:</p>
<p>CompletionInterval</p>
<p>What is that? Well apparently it deals with the amount of time a particular item is highlighted when hovering over it. Found this out the other day. SURPRISE!!</p>
<p>Couple of points from this:</p>
<p>Why?<br />
- Well if nothing else, you now have a compostite control to use. The markup will supply the service location and the method to use, everything else is taken care of with this control.</p>
<p>- On top of that, this can now be snug in a class library (assembly) for reuse thus removing it's need to be recopied everytime you need it in another web project.</p>
<p>- You can now inherit from this control to add fucntionality if you need.</p>
<p>- You can now convert this to a script control in order to access javascript events and other fun things.</p>
<p>So as you can see, this is a pretty good way to go.</p>
<p>Something else of note is the CreateChildControls/EnsureChildControls situation. CreateChildControls is a overridable method that is used to make sure that the controls themselves are created and handled. The nice thing about this is that you won't run into the timing issues you might if you try to initialize controls in other methods like OnInit. Also, it allows you the use of the EnsureChildControls method. When this method is called, the CreateChildControls method is either called or not depending if it's already been run. EnsureChildControls basically asks if CreateChildControls has been run. If it has then it doesn't call it again, if it hasn't then the method is called to create the controls. So as you can see, this makes it easy to guarantee that the controls have been created in order to access them. This becomes important with properties that are tied to controls as there is no guarantee that when a property is accessed the controls aren't null. Kind of nice, huh?</p>
<p>Also you might have noticed that I set all the control properties that were "attached" to the autocomplete in prerender. Usually for safety, I don't both setting any outward facing properties like that until prerender so that I know I have the latest and greatest values.</p>
<p>Just incase you needed the markup example:</p>
<pre>&lt;%@ <span style="color: #800000;">Register</span> <span style="color: #ff0000;">Assembly</span><span style="color: #3366ff;">="Test.Examples"
</span>  <span style="color: #ff0000;">Namespace</span><span style="color: #3366ff;">="Test.Examples.AutoCompleteExample"</span> <span style="color: #ff0000;">TagPrefix</span><span style="color: #3366ff;">="test"</span> %&gt;

&lt;<span style="color: #800000;">test:AutoCompleteControl</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="autoCompleteTest"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span>
  <span style="color: #ff0000;">ServicePath</span><span style="color: #3366ff;">="~/Service/AutocompleteWebControl.svc"</span> <span style="color: #ff0000;">ServiceMethod</span><span style="color: #3366ff;">="GetUserNameList"</span>
  <span style="color: #ff0000;">CompletionSetCount</span><span style="color: #3366ff;">="10"</span> <span style="color: #ff0000;">MinimumPrefixLength</span><span style="color: #3366ff;">="1"</span> /&gt;</pre>
<p>HOLY SMOKES NO NEED FOR TEXTBOXES!!11</p>
<pre>  <span style="color: #0000ff;">using</span> System.Web.UI;
  <span style="color: #0000ff;">using</span> System.Web.UI.WebControls;
  <span style="color: #0000ff;">using</span> AjaxControlToolkit;</pre>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li><li><a href="http://byatool.com/ui/ajax-control-toolkit-autocomplete-how-to-use-simple-example/" title="Ajax Control Toolkit Autocomplete &#8211; How to use&#8230; simple example.">Ajax Control Toolkit Autocomplete &#8211; How to use&#8230; simple example.</a></li><li><a href="http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/" title="Paging and the Entity Framework, Skip, and Take Part 4">Paging and the Entity Framework, Skip, and Take Part 4</a></li><li><a href="http://byatool.com/ui/dynamic-markup-property-collection-for-a-webcontrol/" title="Dynamic Markup Property Collection for a WebControl">Dynamic Markup Property Collection for a WebControl</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Ajax Control Toolkit Autocomplete &#8211; How to use&#8230; simple example.</title>
		<link>http://byatool.com/ui/ajax-control-toolkit-autocomplete-how-to-use-simple-example/</link>
		<comments>http://byatool.com/ui/ajax-control-toolkit-autocomplete-how-to-use-simple-example/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 15:06:12 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax Toolkit]]></category>
		<category><![CDATA[Autocomplete]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=250</guid>
		<description><![CDATA[[Part One] [Part Two] [Part Three] [Part Four] [Part Five] So in my journey to create an autocomplete control, I had it working except something really screwy with the stylesheet and how to make it look... oh I don't know... not hideous. So on a whim I decided to give the Ajax Control Toolkit Autocomplete [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/?p=219">[Part One]</a> <a href="http://byatool.com/?p=231">[Part Two]</a> <a href="http://byatool.com/?p=250">[Part Three]</a> <a href="http://byatool.com/?p=244">[Part Four]</a> <a href="http://byatool.com/?p=329">[Part Five]</a></p>
<p>So in my journey to create an autocomplete control, I had it working except something really screwy with the stylesheet and how to make it look... oh I don't know... not hideous. So on a whim I decided to give the <a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx">Ajax Control Toolkit Autocomplete</a> a try. I figured that if so many other people use it, why shouldn't I? Or when properly translated: I hate dealing with style sheet issues and someone already had a control that works in both Firefox and IE.</p>
<p><strong>Introduction... Skip if you know what this is already and just want the stupid code.</strong></p>
<p>So where to begin? Well the autocomplete control itself is free and comes along with the Toolkit assembly.</p>
<p>Sounds good so far, so what does it do? It's a "control", for lack of a better word... more on that later, that can be used to attach to a textbox and allow a user to type in parts of a word and get back dropdown list like item... list.</p>
<p>Pretty good huh? What's the catch? Well basically you need to use a web service to work with it, meaning either old school (.asmx?) or new school Communication Foundation services. For this example I will actually be doing it the "hard" way and use WCF. I've done it with both, but I figure I might as make it the more difficult of the two for fun. If I remember, the old web services are really easy to do this with. Another catch is that the web service has to be on the same server as the project.</p>
<p>So what isn't it? End of world hunger, world peace, or the meaning of life. Sorry, I can only give you one of those and the autocomplete doesn't cover that subject.</p>
<p><strong>End Introduction and Begin the stupid code</strong></p>
<p>Ok so you want to use the autocomplete control, huh? Well, you've come to the right place.</p>
<p>For this example, I'll be doing the most simple version of adding the autocomplete control. This basically means setting up the service and creating some markup. Really easy. Next post I will get into how to create a web control class in a non web assembly.</p>
<p>Right off the bat you'll need the toolkit assembly and create a project reference to it. Next you have to set up the WCF Service which is actually a lot easier than it sounds? Why? Because Microsoft was nice enough to create a default one for us. I've created a folder named Service (Brilliant!) and then I right clicked and chose Add New Item -&gt; Ajax-Enabled WCF Service (I called it AutocompleteWebControl)... and boom already almost there.</p>
<p>You should now have a AutocompleteWebControl.svc.cs file in the folder and if you look at the code you get this:</p>
<pre><span style="color: #0000ff;">namespace</span> Test.Frontend.Service
{
    [<span style="color: #33cccc;">ServiceContract</span>(Namespace = <span style="color: #800000;">""</span>)]
    [<span style="color: #33cccc;">AspNetCompatibilityRequirements</span>(RequirementsMode =
      <span style="color: #33cccc;">AspNetCompatibilityRequirementsMode</span>.Allowed)]
    <span style="color: #0000ff;">public class</span> AutocompleteWebControl
    {
        <span style="color: #008000;">// Add [WebGet] attribute to use HTTP GET
</span>        [<span style="color: #33cccc;">OperationContract</span>]
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork()
        {
            <span style="color: #008080;"><span style="color: #008000;">// Add your operation implementation here</span>
</span>            <span style="color: #0000ff;">return</span>;
        }
    }
}</pre>
<p>Only thing of real importance at this point is the method with the [OperationContract] attribute. If you are going to expose a method to the autocomplete it has to have this tag. Now as you can see, we'll need a method. At this point though I have to note two things that for the method to be correct:</p>
<ul>
<li>It has to take in a String and an Integer. The first is the string that the user has typed in to search on, the second (If you choose to use it) is for limiting the number of items back. (This is set by the CompletionSetCount property on the control, more on this later.)</li>
<li>The second thing is that without any changes to how the control works, you have to send back a list of strings. This could be a deal breaker if you need to send back more information. I think it's possible to do so, but I haven't gotten to that point yet.</li>
</ul>
<p>Ok so let's create a quick method. My example is using the typical Linq to Sql stuff I've been using but I have faith you can figure out how to get some kind of needed information whether it's LInq to Sql, NHibernate, LLBLGEN, or Stored Procedures...</p>
<pre>....
  [<span style="color: #33cccc;">OperationContract</span>]
  <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">String</span>[] GetUserNameList(<span style="color: #33cccc;">String</span> prefixText, <span style="color: #33cccc;">Int32</span> count)
  {
    <span style="color: #33cccc;">String</span>[] userNames;

    userNames = LinqData.DataClasses.<span style="color: #33cccc;">User</span>
        .GetUserListByLikeName(prefixText)
        .Select(currentUser =&gt; currentUser.UserName)
        .ToArray();

    <span style="color: #0000ff;">return</span> userNames;
  }
....</pre>
<p>Now I didn't use the passed in integer, but I didn't need it for this situation. As you can see this method, however follows the two rules: Takes in a string and integer and returns a string array.</p>
<p>Now you have the servce set up, next up is the mark up and let me tell you, it's freakishly hard.</p>
<pre>&lt;%@ <span style="color: #800000;">Register</span> <span style="color: #ff0000;">assembly</span><span style="color: #3366ff;">="AjaxControlToolkit"</span> <span style="color: #ff0000;">namespace</span><span style="color: #3366ff;">="AjaxControlToolkit"
</span>tagPrefix<span style="color: #3366ff;">="controlkit"</span> %&gt;

&lt;<span style="color: #800000;">asp:ScriptManager</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="smMain"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span> /&gt;
&lt;<span style="color: #800000;">asp:TextBox</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="textboxTarget"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span> /&gt;
&lt;<span style="color: #800000;">ajax:AutoCompleteExtender</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="autoCompleteMain"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span>
  <span style="color: #ff0000;">ServicePath</span><span style="color: #3366ff;">="~/Service/AutocompleteWebControl.svc"</span> <span style="color: #ff0000;">ServiceMethod</span><span style="color: #3366ff;">="GetUserNameList"</span>
  <span style="color: #ff0000;">CompletionSetCount</span><span style="color: #3366ff;">="10"</span> <span style="color: #ff0000;">MinimumPrefixLength</span><span style="color: #3366ff;">="1"</span> <span style="color: #ff0000;">TargetControlID</span><span style="color: #3366ff;">="textboxTarget"</span> /&gt;</pre>
<p>So there it is. Now you have a working autocomplete. But just in case you need it, I'll run through this stuff.</p>
<p><span style="color: #ff0000;">ServicePath</span> - This is where the web service is "located" relative to the project.</p>
<p><span style="color: #ff0000;">ServiceMethod</span> - This is the name of the method it will call to fill itself.</p>
<p><span style="color: #ff0000;">CompletionSetCount</span> - This is the other parameter passed into the web method and used if you want to limit the count of items returned.</p>
<p><span style="color: #ff0000;">MinimumPrefixLength</span> - The minimum amount of characters needed to trigger the method call.</p>
<p><span style="color: #ff0000;">TargetControlID</span> - Come on, honestly? You can't figure that one out?</p>
<p>At this point you're thinking this is great and all but what if you want to make a composite control? Well that's the next post and it's fairly easy.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li><li><a href="http://byatool.com/ui/ajax-control-toolkit-autocomplete-control-webcontrols-createchildcontrols-and-you/" title="Make an Autocomplete Control a Webcontrol">Make an Autocomplete Control a Webcontrol</a></li><li><a href="http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/" title="Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)">Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)</a></li><li><a href="http://byatool.com/ui/creating-script-controls-and-love/" title="Creating Script Controls And Love (ASP.Net ScriptControl)">Creating Script Controls And Love (ASP.Net ScriptControl)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/ajax-control-toolkit-autocomplete-how-to-use-simple-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)</title>
		<link>http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/</link>
		<comments>http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 22:18:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax Toolkit]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ScriptControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=231</guid>
		<description><![CDATA[[Part One] [Part Two] [Part Three] [Part Four] [Part Five] Ok in the last post I went over the creation of a web control. Simple enough as you can see. Now it's time to screw around with Controls and how to capture their events client side. Something only a brilliant person like me could figure [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/?p=219">[Part One]</a> <a href="http://byatool.com/?p=231">[Part Two]</a> <a href="http://byatool.com/?p=250">[Part Three]</a> <a href="http://byatool.com/?p=244">[Part Four]</a> <a href="http://byatool.com/?p=329">[Part Five]</a></p>
<p>Ok in the last post I went over the creation of a web control.  Simple enough as you can see.  Now it's time to screw around with Controls and how to capture their events client side.  Something only a brilliant person like me could figure out...  I so wish that were true.  I could make millions.</p>
<p>Let's add a control to the eh... control. Say we want to throw an alert everytime a person focuses on a textbox. Yes, that's really stupid but it's easy so live with it. Let's use the class from the other post:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #33cccc;">ScriptControlExample</span> : <span style="color: #33cccc;">ScriptControl</span> , <span style="color: #33cccc;">INamingContainer</span>
  {
     <span style="color: #0000ff;">private</span> TextBox textboxStupid;

     <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> CreateChildControls()
     {
       base.CreateChildControls();

       textboxStupid = new TextBox();
       textboxStupid.ID = "textboxStupid";
       Controls.Add(textboxStupid);
     }

      <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #33cccc;">IEnumerable</span>&lt;<span style="color: #33cccc;">ScriptDescriptor</span>&gt; GetScriptDescriptors()
      {
        <span style="color: #33cccc;">ScriptControlDescriptor</span> desc = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ScriptControlDescriptor</span>
          (<span style="color: #800000;">"Test.Examples.Client.ScriptControlExample"</span>, ClientID);
        desc.AddProperty(<span style="color: #800000;">"textboxStupid"</span>, textboxStupid.ClientID);

        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ScriptDescriptor</span>[] { desc };
      }

      <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #33cccc;">IEnumerable</span>&lt;<span style="color: #33cccc;">ScriptReference</span>&gt; GetScriptReferences()
      {
         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ScriptReference</span>[] { <span style="color: #0000ff;">new</span> ScriptReference
           (<span style="color: #800000;">"Test.Examples.ScriptControlTemplate.ScriptControlExample.js"</span>, <span style="color: #800000;">"Test.Examples"</span>) };
      }
    }</pre>
<p>Notice anything new? I added a textbox, created it in the CreateChildControls method, then added soemthing new to the GetScriptDescriptors method. Remeber the GetScriptDescriptors method is used to send values to the client from the server. Here I am sending the clientID of the textbox... can you guess why? That's right, because I'm going to use it to find the control client side. Now for the JavaScript end.</p>
<pre>  <span style="color: #0000ff;">if</span> (Type)
  {
    Type.registerNamespace(<span style="color: #800000;">"Test.Examples.Client"</span>);
  }

  NDI.WebControls.Client.GenericAutoComplete = <span style="color: #0000ff;">function</span>(element)
  {
      NDI.WebControls.Client.GenericAutoComplete.initializeBase(<span style="color: #0000ff;">this</span>, [element]);

      <span style="color: #008000;">//Textbox ID value</span>
      <span style="color: #0000ff;">this</span>._textboxStupidID = <span style="color: #800000;">""</span>;
      <span style="color: #008000;">//Textbox Control</span>
      <span style="color: #0000ff;">this</span>._textboxStupid = <span style="color: #0000ff;">null</span>;
      <span style="color: #008000;">//Event Handler</span>
      <span style="color: #0000ff;">this</span>._textboxStupidOnFocus = <span style="color: #0000ff;">null</span>;
  }

  Test.Examples.Client.ScriptControlExample.prototype =
  {
    <span style="color: #008000;">//Properties for the _textboxStupidID field</span>
    get_textboxStupidID : <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>._textboxStupidID;
    },

    set_textboxStupidID : <span style="color: #0000ff;">function</span>(value)
    {
        <span style="color: #0000ff;">this</span>._textboxStupidID = value;
    },

    <span style="color: #008000;">//Method to be called on focus</span>
    handleOnTextBoxFocus : <span style="color: #0000ff;">function</span>(e)
    {
        alert(<span style="color: #800000;">'hi'</span>);
    },    

     initialize: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #008000;">//Find and set the textbox field using the passed in ID</span>
        <span style="color: #0000ff;">this</span>._textboxStupid = $get(<span style="color: #0000ff;">this</span>._textboxStupidID);

        <span style="color: #008000;">//Create a delegate to handle the onFocus event</span>
        <span style="color: #008000;">//Basically creating a means to call the handleOnTextBoxFocus method</span>
        <span style="color: #0000ff;">this</span>._textboxStupidOnFocus = Function.createDelegate(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">this</span>.handleOnTextBoxFocus);

        <span style="color: #008000;">//Set the onFocus event to be handled</span>
        $addHandler(<span style="color: #0000ff;">this</span>._textboxStupid, <span style="color: #800000;">"focus"</span>, <span style="color: #0000ff;">this</span>._textboxStupidOnFocus);

        Test.Examples.Client.ScriptControlExample.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #800000;">'initialize'</span>);
    },

    dispose: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #008000;">//Remove the event handling when this is all said and done.  A clean up.
</span>        $removeHandler(<span style="color: #0000ff;">this</span>._textboxStupid, <span style="color: #800000;">"focus"</span>, <span style="color: #0000ff;">this</span>._textboxStupidOnFocus);
        Test.Examples.Client.ScriptControlExample.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #800000;">'dispose'</span>);
    }
  }
  <span style="color: #008000;">//Same as before
</span>  Test.Examples.Client.ScriptControlExample.registerClass(
    <span style="color: #800000;">'Test.Examples.Client.ScriptControlExample'</span>, Sys.UI.Control);</pre>
<p>Oooooooooook So some added things, yes? Well I'll go through them:</p>
<pre>  NDI.WebControls.Client.GenericAutoComplete = <span style="color: #0000ff;">function</span>(element)
  {
      NDI.WebControls.Client.GenericAutoComplete.initializeBase(<span style="color: #0000ff;">this</span>, [element]);

      <span style="color: #008000;">//Textbox ID value
</span>      <span style="color: #0000ff;">this</span>._textboxStupidID = <span style="color: #800000;">""</span>;
      <span style="color: #008000;">//Textbox Control
</span>      <span style="color: #0000ff;">this</span>._textboxStupid = <span style="color: #0000ff;">null</span>;
      <span style="color: #008000;">//Event Handler</span>
      <span style="color: #0000ff;">this</span>._textboxStupidOnFocus = null;
  }</pre>
<p>This is basically declaring the fields for the class and setting their defaults. For this example we'll need the field to hold the ID in, a field to represent the textbox oject, and a field to represent the delegate later on used to hook up the onFocus Event.</p>
<p>Note:  In the next bit you will notice that every method/property is followed by a comma.  Do not forget that.</p>
<p>Next, let's look at the actual class definition. First we'll start at the properties:</p>
<pre>  Test.Examples.Client.ScriptControlExample.prototype =
  {
    <span style="color: #008000;">//Properties for the _textboxStupidID field
</span>    get_textboxStupidID : <span style="color: #0000ff;">function</span>()
    {
        return <span style="color: #0000ff;">this</span>._textboxStupidID;
    },  <span style="color: #008000;">//SEE COMMA
</span>
    set_textboxStupidID : <span style="color: #0000ff;">function</span>(value)
    {
        <span style="color: #0000ff;">this</span>._textboxStupidID = value;
    },  <span style="color: #008000;">//ANOTHER COMMA</span>

    .....</pre>
<p>First thing you'll notice is the :fuction notation. This is used to declare both methods and properties. Nothing huge, just something to note.</p>
<p>Second thing you might notice is that the property names look a little hard coded and you would be right. Remember how the value send out from the class file was named "textboxStupidID"? Well guess what, for .net to be able to attach that value to a client property, you have to name the properties exactly the same with with either get_ or set_ preceding. Not perfect, but since I'm the only perfect thing in existence, I guess it will have to do.</p>
<p>Now onto the method we need:</p>
<pre>    ....

    <span style="color: #008000;">//Method to be called on focus</span>
    handleOnTextBoxFocus : <span style="color: #0000ff;">function</span>(e)
    {
        alert(<span style="color: #800000;">'hi'</span>);
    },  

    ....</pre>
<p>Wow... uh well it's a method and it takes in an argument. What the hell do you want from me?</p>
<p>Onto the intialization/Constructorish thing:</p>
<pre>     initialize: <span style="color: #0000ff;">function</span>()
    {
        <span style="color: #008000;">//Find and set the textbox field using the passed in ID</span>
        <span style="color: #0000ff;">this</span>._textboxStupid = $get(<span style="color: #0000ff;">this</span>._textboxStupidID);

        <span style="color: #008000;">//Create a delegate to handle the onFocus event
</span>        <span style="color: #008000;">//Basically creating a means to call the handleOnTextBoxFocus method</span>
        <span style="color: #0000ff;">this</span>._textboxStupidOnFocus = Function.createDelegate(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">this</span>.handleOnTextBoxFocus);

        <span style="color: #008000;">//Set the onFocus event to be handled</span>
        $addHandler(<span style="color: #0000ff;">this</span>._textboxStupid, "focus", <span style="color: #0000ff;">this</span>._textboxStupidOnFocus);

        Test.Examples.Client.ScriptControlExample.callBaseMethod(this, 'initialize');
    },</pre>
<p>This might be the least easy part of the whole thing. The first commentented area:</p>
<pre>    <span style="color: #0000ff;">this</span>._textboxStupid = $get(<span style="color: #0000ff;">this</span>._textboxStupidID);</pre>
<p>Simple just gets the textbox by the passed in ID. I swear this will get more complicated.</p>
<pre>    <span style="color: #0000ff;">this</span>._textboxStupidOnFocus = Function.createDelegate(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">this</span>.handleOnTextBoxFocus);</pre>
<p>This gives a variable that points to the method we want to use when the focus event fires. Still waiting for complicated...</p>
<pre>   $addHandler(<span style="color: #0000ff;">this</span>._textboxStupid, <span style="color: #800000;">"focus"</span>, <span style="color: #0000ff;">this</span>._textboxStupidOnFocus);</pre>
<p>This assigns the method we want, through the delegate, to the focus event on the textbox. Hrm. Ok so not really complicated. Just messy looking. One this to note though is that the events on all the controls won't have the "on" prefix. So if you can't figure out why you're getting an error because it can't find the event, try dropping the "on".</p>
<p>And there you have it. Now you are free to run it and be really annoyed by how dumb this example was.  Next will be using this for the power of evil by adding an autocomplete control with a processing image.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li><li><a href="http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/" title="Return False from a ScriptControl method and why I hate Firefox&#8230;">Return False from a ScriptControl method and why I hate Firefox&#8230;</a></li><li><a href="http://byatool.com/ui/creating-script-controls-and-love/" title="Creating Script Controls And Love (ASP.Net ScriptControl)">Creating Script Controls And Love (ASP.Net ScriptControl)</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Script Controls And Love (ASP.Net ScriptControl)</title>
		<link>http://byatool.com/ui/creating-script-controls-and-love/</link>
		<comments>http://byatool.com/ui/creating-script-controls-and-love/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 21:16:08 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Ajax Toolkit]]></category>
		<category><![CDATA[ScriptControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=219</guid>
		<description><![CDATA[[Part One] [Part Two] [Part Three] [Part Four] [Part Five] So this will be the first of a couple posts having to do with Script Controls, the Ajax Control Tool Kit AutoComplete control, and Javascript. Now to get started, what's a script control? I'm glad you asked. Basically have you ever created a user control [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/?p=219">[Part One]</a> <a href="http://byatool.com/?p=231">[Part Two]</a> <a href="http://byatool.com/?p=250">[Part Three]</a> <a href="http://byatool.com/?p=244">[Part Four]</a> <a href="http://byatool.com/?p=329">[Part Five]</a></p>
<p>    So this will be the first of a couple posts having to do with Script Controls, the Ajax Control Tool Kit AutoComplete control, and Javascript.  Now to get started, what's a script control?</p>
<p>    I'm glad you asked.  Basically have you ever created a user control that required Javascript to go along with it?  What if the control was in another assembly?  Oh woe is you.  Well you could require the .js file to be placed in the web project and included on the page.  Meh.  OR you could use a script control YAY!  How does that help?  Well it allows you to "attach" a javascript file to a control in a class library, no includes needed.  Not only that, but you can pass values from the control to the client and use them with Javascript.  (This includes ids of controls in the control.)</p>
<p>So what's needed?  Well...<br />
1. Create a class library, Call it Test and make the default Namespace Test.Examples</p>
<p>2. Create a folder to hold the class file and the JavaScript file. Let's call it ScriptControlTemplate for now.</p>
<p>2.  Create the control class file and the needed JavaScript file. Call them both ScriptControlExample. (For right now it will be easier if they have the same name but they don't have to be.)</p>
<p>3. Right click the JavaScript Files -&lt; Properties -&lt; Build Action: Embedded Resource</p>
<p>4. Go to the Properties folder in the class Library and open the AssemblyInfo.cs. Add this line:</p>
<pre>[<span style="color: #0000ff;">assembly</span>: <span style="color: #33cccc;">WebResourceAttribute</span>
    (<span style="color: #800000;">"Test.ScriptControlTemplate.ScriptControlExample.js"</span>, <span style="color: #800000;">"text/javascript"</span>)]</pre>
<p>    Now you might notice something, that almost looks like the actual folder location and it kind of is. In order for this line to work, it has to match the folder location like when Visual Studios creates a Namespace automatically. It uses the directory name as the final part of the namespace. Although to note, the Namespace of the class DOES NOT have to match this. It just has to reflect the directory structure of where the .js file is.</p>
<p>    Ok so now we have a blank .cs file and a blank .js file. Woo hoo. Now what? Well first you need to have the class inherit from System.Web.UI.ScriptControl and override two protected methods</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #33cccc;">ScriptControlExample</span> : <span style="color: #33cccc;">ScriptControl</span> , <span style="color: #33cccc;">INamingContainer</span>
    {
        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #33cccc;">IEnumerable</span>&lt;<span style="color: #33cccc;">ScriptDescriptor</span>&gt; GetScriptDescriptors()
        {
            <span style="color: #33cccc;">ScriptControlDescriptor</span> desc =
                 <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ScriptControlDescriptor</span>(<span style="color: #800000;">"Test.Examples.Client.ScriptControlExample"</span>, ClientID);
        }

        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #33cccc;">IEnumerable</span>&lt;<span style="color: #33cccc;">ScriptReference</span>&gt; GetScriptReferences()
        {
          <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ScriptReference[] {
              <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ScriptReference</span>(<span style="color: #800000;">"Test.Examples.ScriptControlTemplate.ScriptControlExample.js"</span>, <span style="color: #800000;">"Test.Examples"</span>) };

        }
    }</pre>
<p>    Ok so I cheated a little and put in two lines that you will need anyhow. I figured it would save some time, mainly mine. Cause let's face it, I don't really care about yours.</p>
<p>    The first method GetScriptDescriptors is where we will be putting any values we want to send to the Client. The second just tells the system where the JavaScript file is to load. For now you won't be doing anything with the second.</p>
<p>    Ok now on to the Javascript file. Basically you have some cannon methods to add just to get the thing to work, and I'll be honest: It's extremely complicated and you probably won't be smart enough to figure it out.</p>
<pre>//Set the namespace
<span style="color: #0000ff;">if</span> (Type)
{
    Type.registerNamespace(<span style="color: #800000;">"Test.Examples.Client"</span>);
}

<span style="color: #339966;">//Won't be doing much with this for the examples I have
</span>Test.Examples.Client.ScriptControlExample = <span style="color: #0000ff;">function</span>(element)
{
    Test.Examples.Client.ScriptControlExample.initializeBase(<span style="color: #0000ff;">this</span>, [element]);
}

<span style="color: #008000;">//This is where you basically define the class
</span>Test.Examples.Client.ScriptControlExample.prototype =
{
    //Where you want to place constructor like code
    initialize: <span style="color: #0000ff;">function</span>()
    {
        Test.Examples.Client.ScriptControlExample.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #800000;">'initialize'</span>);
    },

    <span style="color: #008000;">//Remove events and such here
</span>    dispose: <span style="color: #0000ff;">function</span>()
    {
        Test.Examples.Client.ScriptControlExample.callBaseMethod(<span style="color: #0000ff;">this</span>, <span style="color: #800000;">'dispose'</span>);
    }
}
Test.Examples.Client.ScriptControlExample.registerClass(<span style="color: #800000;">'Test.Examples.Client.ScriptControlExample'</span>,
   Sys.UI.Control);</pre>
<p>   Phew that was complicated right?</p>
<p>    Ok so now you have what you need to create a script control. Run it and well nothing will really happen except you shouldn't get errors.</p>
<p>   Now just create a new page in a web application (Or project if you're a loser) and just do this realy quick:</p>
<pre><span style="color: #0000ff;">&lt;%</span><span style="color: #3366ff;">@</span> <span style="color: #800000;">Register</span> <span style="color: #ff0000;">Assembly</span><span style="color: #3366ff;">="Test.Examples"
</span>    <span style="color: #ff0000;">Namespace</span><span style="color: #3366ff;">="Test.Examples.ScriptControlTemplate"</span> <span style="color: #ff0000;">TagPrefix</span><span style="color: #3366ff;">="test"</span> <span style="color: #ffff99;"><span style="color: #0000ff;">%&gt;</span>
</span>
&lt;<span style="color: #800000;">html</span> <span style="color: #ff0000;">xmlns</span><span style="color: #3366ff;">="http://www.w3.org/1999/xhtml"</span> &gt;
&lt;<span style="color: #800000;">head</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span>&gt;
&lt;/<span style="color: #800000;">head</span>&gt;
&lt;<span style="color: #800000;">body</span>&gt;
    &lt;<span style="color: #800000;">form</span> <span style="color: #ff0000;">id</span><span style="color: #3366ff;">="form1"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span>&gt;
    &lt;<span style="color: #800000;">div</span>&gt;
        &lt;<span style="color: #800000;">asp:ScriptManager</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="smMain"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span> /&gt;
        &lt;<span style="color: #800000;">test:ScriptControlExample</span> <span style="color: #ff0000;">ID</span><span style="color: #3366ff;">="sceTest"</span> <span style="color: #ff0000;">runat</span><span style="color: #3366ff;">="server"</span> /&gt;
    &lt;/<span style="color: #800000;">div</span>&gt;
    &lt;/<span style="color: #800000;">form</span>&gt;
&lt;/<span style="color: #800000;">body</span>&gt;
&lt;/<span style="color: #800000;">html</span>&gt;</pre>
<p>    Run it and.....NOTHING! Surprise! Actually there is something, just nothing visual yet. Next post will get to that. Sucker.</p>
<p>    One thing you can take away from this:  A drinking game.  Take a drink everytime you see the word 'Ok' in this post.  I dare you.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/ajax-control-library-autocomplete-control-to-scriptcontrol/" title="Ajax Control Library: Autocomplete Control to Scriptcontrol">Ajax Control Library: Autocomplete Control to Scriptcontrol</a></li><li><a href="http://byatool.com/ui/return-false-with-a-scriptcontrol-method-and-why-i-hate-firefox/" title="Return False from a ScriptControl method and why I hate Firefox&#8230;">Return False from a ScriptControl method and why I hate Firefox&#8230;</a></li><li><a href="http://byatool.com/ui/aspnet-script-controls-adding-controls-and-handling-events/" title="Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)">Handle Events and Add Controls to Script Controls (ASP.Net ScriptControl)</a></li><li><a href="http://byatool.com/pontification/ajax-webmethods-javascript-and-anonymous-types-booyah/" title="Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah ">Ajax Webmethods, Javascript, and Anonymous Types&#8230; Booyah </a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/creating-script-controls-and-love/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Microsoft Charting Controls: Adding Charts Dynamically</title>
		<link>http://byatool.com/ui/microsoft-charting-controls-adding-dynamically/</link>
		<comments>http://byatool.com/ui/microsoft-charting-controls-adding-dynamically/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 21:31:18 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Charting]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Dynamic Controls]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=122</guid>
		<description><![CDATA[So this is kind of repost as I had already posted this at StackOverflow but I thought it might have some merit here. Whatever. Charts are hot right now so I'm going to push the damned bandwagon. You don't like it? Well then go do something to yourself that you would consider rude for me [...]]]></description>
			<content:encoded><![CDATA[<p>So this is kind of repost as I had already posted this at <a href="http://www.stackoverflow.com">StackOverflow</a> but I thought it might have some merit here. Whatever. Charts are hot right now so I'm going to push the damned bandwagon. You don't like it? Well then go do something to yourself that you would consider rude for me to suggest it. Anyways, this might have been overkill but hey, that's me.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
Bench benchList<span style="color: #008000;">;</span>
FoodIntake foodIntakeList<span style="color: #008000;">;</span>
Panel panelChartHolder<span style="color: #008000;">;</span>
&nbsp;
panelChartHolder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Panel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>panelChartHolder<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
benchList <span style="color: #008000;">=</span> Bench.<span style="color: #0000FF;">GetAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
AddNewCharts<span style="color: #000000;">&#40;</span>benchList, panelChartHolder, GetBenchXValue, GetBenchYValue<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
foodIntakeList <span style="color: #008000;">=</span> FoodIntake.<span style="color: #0000FF;">GetAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
AddNewCharts<span style="color: #000000;">&#40;</span>foodIntakeList, panelChartHolder, GetFoodIntakeXValue, GetFoodIntakeYValue<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Ok so this first part is simple. Create a panel to hold the charts you are adding, get the lists you want represented by the charts and call the method to create the charts.</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> AddNewCharts(T[] listToAdd, <span style="color: #33cccc;">Panel</span> panelToAddTo,
     <span style="color: #33cccc;">Func</span>&lt;T, <span style="color: #33cccc;">DateTime</span>&gt; xMethod, <span style="color: #33cccc;">Func</span>&lt;T, <span style="color: #33cccc;">Int32</span>&gt;)
  {

    <span style="color: #33cccc;">ChartArea</span> mainArea;
    <span style="color: #33cccc;">Chart</span> mainChart;
    <span style="color: #33cccc;">Series</span> mainSeries;

    mainChart = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">Chart</span>();
    mainSeries = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">Series</span>(<span style="color: #ff0000;">"MainSeries"</span>);

    <span style="color: #0000ff;">for</span> (<span style="color: #33cccc;">Int32</span> loopCounter = 0; loopCounter &lt; listToAdd.Length; loopCounter++)
    {
      mainSeries.Points.AddXY(xMethod(listToAdd[loopCounter]),
        yMethod(listToAdd[loopCounter]));
    }

    mainChart.Series.Add(mainSeries);
    mainArea = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">ChartArea</span>(<span style="color: #ff0000;">"MainArea"</span>);
    mainChart.ChartAreas.Add(mainArea);

    panelToAddTo.Controls.Add(mainChart);
  }</pre>
<p>As you can see, I just created a new chart, added a series to it, and added a ChartArea to it. Next part is pretty much just looping through the collection and adding each item in it to the list itself. It uses the passed in delegate methods (Func) to get the X and Y values.</p>
<p>Last part holds the four methods responsible for getting the X and Y values from the two lists. Basically I did this to allow the chart creating method to be a generic as possible. Might be overkill.</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">DateTime</span> GetBenchXValue(Bench currentBench)
  {
    <span style="color: #0000ff;">return</span> currentBench.DateLifted;
  }

  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Int32</span> GetBenchYValue(Bench currentBench)
  {
    <span style="color: #0000ff;">return</span> currentBench.BenchAmount;
  }

  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">DateTime</span> GetFoodIntakeXValue(FoodIntake currentIntake)
  {
    <span style="color: #0000ff;">return</span> currentIntake.DateEaten;
  }

  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Int32</span> GetFoodIntakeYValue(FoodIntake currentIntake)
  {
    <span style="color: #0000ff;">return</span> currentIntake.Calories;
  }</pre>
<p>And so when you run this, you will get two graphs side by side. Mind you, they will be very plain as there are million different properties that can be set to improve the look. I guess the main point of this was to show that it's pretty easy to create graphs dynamically using any kind of object list. You know what? Screw you. This is my blog and I'll post whatever I want to. If you don't like that then you can just come back at a later time and read something else I post. Yeah so there.</p>
<pre>  <span style="color: #0000ff;">using</span> System;
  <span style="color: #0000ff;">using</span> System.Web.UI.DataVisualization.Charting;
  <span style="color: #0000ff;">using</span> System.Web.UI.WebControls;</pre>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/microsoft-charting-control-so-easy-an-idiot-can-use-it-and-i-have/" title="Microsoft Charting Control: So easy an idiot can use it&#8230; And I have.">Microsoft Charting Control: So easy an idiot can use it&#8230; And I have.</a></li><li><a href="http://byatool.com/general-coding/the-switch-remover/" title="The Switch Remover: Convert Switch Statements to Dictionaries">The Switch Remover: Convert Switch Statements to Dictionaries</a></li><li><a href="http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/" title="ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View">ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View</a></li><li><a href="http://byatool.com/ui/jquery-slide-menu-with-pop-up-divs-yay/" title="jQuery: Slide Menu With Pop Up Divs.  Yay!">jQuery: Slide Menu With Pop Up Divs.  Yay!</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/microsoft-charting-controls-adding-dynamically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
