<?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>Musings&#60;Biefeld&#62; &#187; Asp.net</title>
	<atom:link href="http://sbiefeld.com/category/coding/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://sbiefeld.com</link>
	<description>- curiosities of development, life, the universe and everything -</description>
	<lastBuildDate>Tue, 02 Feb 2010 04:32:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extending the asp.net textbox to have an OnBlur server side event.</title>
		<link>http://sbiefeld.com/2008/09/17/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/</link>
		<comments>http://sbiefeld.com/2008/09/17/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 20:34:17 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=22</guid>
		<description><![CDATA[I recently needed a textbox that would fire a server event when the textbox’s onblur JavaScript event was fired.&#160; It took a while of googling and asking coworkers questions to figure out how exactly to do it.&#160; Couldn’t seem to find an example of exactly what I needed.
&#160;
I started off by creating a class that [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed a textbox that would fire a server event when the textbox’s <i>onblur</i> JavaScript event was fired.&#160; It took a while of googling and asking coworkers questions to figure out how exactly to do it.&#160; Couldn’t seem to find an example of exactly what I needed.
<p>&#160;</p>
<p>I started off by creating a class that inherited from textbox and implemented <i>ICallBackEventHandler.&#160; </i>I soon realized that this was not going to work for what I needed.&#160; The <i>ICallBackEventHandler</i> is best suited for a simple Ajax control that just needs to update its own content.&#160; Problem is that I need a post back to happen in order to update other controls on the page.</p>
<p>&#160;</p>
<p>So instead of implementing the <i>ICallBackEventHandler</i> interface I implemented the <i>IPostBackEventHanlder</i> interface.&#160; This turned out to be a much simpler, cleaner way to do it. </p>
<p>When implementing the <i>IPostBackEventHandler</i> you must Implement the <i>RaisePostBackEvent</i> method:</p>
<pre><span class="keyword">public</span> <span class="keyword">class</span> <span class="class">BlurTextbox</span> : <span class="class">TextBox, IPostBackEventHandler</span>
{
    <span class="keyword">#region</span> Implementation of IPostBackEventHandler

    <span class="comments">/// &lt;summary&gt;</span>
    <span class="comments">/// When implemented by a class, enables a server control to process an event raised when a form is posted to the server.</span>
    <span class="comments">/// &lt;/summary&gt;</span>
    <span class="comments">/// &lt;param name=&quot;eventArgument&quot;&gt;A &lt;see cref=&quot;T:System.String&quot; /&gt; that represents an optional event argument to be passed to the event handler. &lt;/param&gt;</span>
    <span class="keyword">public</span> <span class="keyword">void</span> RaisePostBackEvent(<span class="keyword">string</span> eventArgument)
    {

    }

    <span class="keyword">#endregion</span>
}</pre>
<p>&#160;</p>
<p>This method is what gets called by .net’s JavaScript when your control does a post back.</p>
<p>&#160;</p>
<p>Now what we want to do is create the JavaScript function that will cause the post back will allow the <i>RaisePostBackEvent</i> method to handle the logic for that event.</p>
<p>&#160;</p>
<p>I just created a method on the textbox that returns the script.&#160; You can use the Page.ClientScript.GetPostBackEventReference<i>(Control control, string argument)</i> method but this is not necessary because all the it generates is <i>__doPostBack(&#8217;controlName&#8217;,'argument&#8217;),</i> either way works.</p>
<p>&#160;</p>
<p>If you want to be able to have multiple instances of the control on the same page you need to write it yourself, this is what I did: </p>
<pre><span class="keyword">private</span> <span class="keyword">string</span> GetScript()
{
    <span class="keyword">return</span> <span class="string">&quot;function OnBlurred(control, arg)\n{\n __doPostBack(control, arg);\n}&quot;</span>;
}</pre>
<p>There are two parameters needed, the control and the argument, the control string that the asp.net JavaScript call is expecting will need the textbox’s <i>UniqueID</i> for it to work properly.&#160; </p>
<p>Next I created a server side event on the control called Blur:</p>
<pre><span class="keyword">public</span> <span class="keyword">delegate</span> <span class="keyword">void</span> <span class="class">OnBlurDelegate</span>(<span class="keyword">object</span> sender, <span class="class">EventArgs</span> e);

<span class="keyword">public</span> <span class="keyword">event</span> <span class="class">OnBlurDelegate</span> Blur;

<span class="keyword">private</span> <span class="keyword">void</span> RaiseOnBlurEvent()
{
    <span class="keyword">if</span>(Blur != <span class="keyword">null</span>)
    {
        Blur(<span class="keyword">this</span>, <span class="class">EventArgs</span>.Empty);
    }
}</pre>
<p>Now we can call the <i>RaiseOnBlurEvent()</i> method in the implemented <i>RaisePostBackEvent()</i> method like:</p>
<pre><span class="keyword">public</span> <span class="keyword">void</span> RaisePostBackEvent(<span class="keyword">string</span> eventArgument)
{
    RaiseOnBlurEvent();
}</pre>
<p>Lastly I overrode the <i>OnInit</i> event, called the base.&#160; I check if that script is already registered, if it’s not I register it, otherwise move one.&#160; Then registered the <i>OnBlurred</i> script we created, and added the JavaScript <i>onblur</i> event attribute to the textbox.&#160; I pass in the <i>UniqueID</i> and empty quotes because I don’t need any arguments.</p>
<pre><span class="keyword">protected</span> <span class="keyword">override</span> <span class="keyword">void</span> OnInit(EventArgs e)
{
    <span class="keyword">base</span>.OnInit(e);
    <span class="keyword">if</span> (!Page.ClientScript.IsClientScriptBlockRegistered(<span class="string">&quot;OnBlurTextboxEvent&quot;</span>))
        Page.ClientScript.RegisterStartupScript(GetType(), <span class="string">&quot;OnBlurTextboxEvent&quot;</span>, GetScript(), <span class="keyword">true</span>);
    Attributes.Add(<span class="string">&quot;onblur&quot;</span>, <span class="string">&quot;OnBlurred('&quot;</span> + UniqueID + <span class="string">&quot;','')&quot;</span>);
}</pre>
<p>Now all that is left to do is use it on some web page and setup the server side handling.</p>
<p>Aspx:</p>
<pre>&lt;<span class="keyword">cc3:BlurTextbox</span> <span class="class">ID</span>=&quot;Test&quot; <span class="class">runat</span>=&quot;server&quot; <span class="class">OnBlur</span>=&quot;Test_OnBlur&quot;<span class="keyword">&gt;&lt;/</span><span class="keyword">cc3:BlurTextbox</span><span class="keyword">&gt;</span></pre>
<p>Server Handling:</p>
<pre><span class="keyword">protected</span> <span class="keyword">void</span> Test_OnBlur(<span class="keyword">object</span> sender, <span class="class">EventArgs</span> e)
{
}</pre>
<p>The whole thing is <a href="http://sbiefeld.com/Stuff/BlurTextbox.cs">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2008/09/17/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
