<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-777933054050893742</id><updated>2024-09-21T05:11:07.281+05:30</updated><category term="C#"/><category term="jquery"/><category term="ASP.NET"/><category term="ASPX"/><category term="gridview"/><category term="javascript"/><category term="knockoutjs"/><category term="nested gridview"/><category term="selectable row"/><category term="?? Operator"/><category term="App_Browsers"/><category term="Check for 404"/><category term="Click Grid"/><category term="Click GridView"/><category term="Click Row"/><category term="Clickable GridViewRow"/><category term="Coalesce Operator"/><category term="Custom DateTime Formats"/><category term="DateTime Format Strings"/><category term="DateTime Formats"/><category term="Grid Row HyperLink"/><category term="GridViewRow HyperLink"/><category term="JavaScript Space Trim"/><category term="JavaScript Strip Spaces"/><category term="JavaScript Trim"/><category term="JavaScript Trim Space"/><category term="Remove Spaces"/><category term="Strip Space"/><category term="Ternary Operator"/><category term="URL Check"/><category term="URL Checker"/><category term="URL Exist"/><category term="W3c Validation"/><category term="ajaxStart"/><category term="ajaxStop"/><category term="asp.net mvc"/><category term="child parent gridivew"/><category term="copy datatable row"/><category term="datatables"/><category term="div over div"/><category term="gridview checkbox paging maintaining persisting state"/><category term="gridview filtering datalist filter alphabet"/><category term="gridview nesting"/><category term="gridview sorting paging"/><category term="gridview with javascript"/><category term="image data type gridview"/><category term="inser custom row gridview"/><category term="javascript div"/><category term="javascript gridview"/><category term="javascript nesting gridview"/><category term="jquery microsoft"/><category term="jquery visual studio"/><category term="listview paging"/><category term="maintain checkbox state multiple gridviews"/><category term="modal div"/><category term="paging and filtering gridview"/><category term="popup div"/><category term="registry key"/><category term="remote"/><category term="remove div input node javascript jquery"/><category term="repeater paging pageddatasource"/><category term="server-side paging"/><category term="subtotal gridview"/><category term="there is no attribute &quot;border&quot;"/><category term="unbind"/><category term="utility-function"/><category term="validate"/><category term="validations"/><category term="zindex div"/><title type='text'>Coding Nuances</title><subtitle type='html'>A tech blog to core.&#xa;Concentrating mainly on ASP.NET and Javascript</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default?start-index=26&amp;max-results=25'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>41</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-2989999066286377514</id><published>2013-11-25T19:06:00.001+05:30</published><updated>2020-05-25T18:59:12.335+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="registry key"/><title type='text'>Get Registry key value for 32 and 64 bit machines</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Here is the snippet that pulls a &lt;b&gt;LOCALMACHINE\SOFTWARE&lt;/b&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/b0f08a8a3d2d4484fa9d9e31c8038931.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/2989999066286377514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/get-registry-key-value-for-32-and-64.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2989999066286377514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2989999066286377514'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/get-registry-key-value-for-32-and-64.html' title='Get Registry key value for 32 and 64 bit machines'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-5431350107125037948</id><published>2013-11-25T18:42:00.001+05:30</published><updated>2020-05-25T19:12:50.372+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net mvc"/><category scheme="http://www.blogger.com/atom/ns#" term="knockoutjs"/><title type='text'>Binding html select using knockoutjs and asp.net mvc</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Suppose we have a ViewModel like this&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/5ba2a6c35cc02ad08b49662f45db848c.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
The markup to populate HTML Select should be&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/1e97b5d1f374c3c833835afb47415b7a.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/9659613e966d75c3168a713f3ba20587.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/5431350107125037948/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/binding-html-select-using-knockoutjs.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5431350107125037948'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5431350107125037948'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/binding-html-select-using-knockoutjs.html' title='Binding html select using knockoutjs and asp.net mvc'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-3641366038151750953</id><published>2013-11-05T20:11:00.000+05:30</published><updated>2020-05-25T19:59:07.233+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="datatables"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="server-side paging"/><title type='text'>Server side paging, filtering and sorting using datatables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Nothing better than writing some code to explain a feature. This post shows how to do server-side paging in a table using datatables.net
&lt;br /&gt;
The idea is to display a table with a large amount of data (68k rows). As it&#39;s not a good practice to load all at once, it&#39; better to implement server-side paging.&lt;br /&gt;
&lt;br /&gt;
First download datatables.net from here and reference them in your html file.&lt;br /&gt;
&lt;br /&gt;
In our sample we are using a table Tasks with 3 fields, ie, TaskID, TaskName, Complete&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/aea5591985bd4405ad346f4b5731fb4c.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
Lets create the HTML markup first&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/408ec8e1b4191cd788610f1577d9bf65.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets wire up datatable to the table&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/fcbb488aaf0d93acd33b82adf677aed5.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
Here as you can see TaskID and TaskName are sortable and searchable.&lt;br /&gt;
Now, the model that is passed from datatables to server-side&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/5ed75f8d7dde8dc8a4e09f3e5c7fe979.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, the controller method&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/codeandcloud/e96126f85719d6ee7ce11b49cdb3fe48.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
That&#39;s all to it. Happy coding!
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/3641366038151750953/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/server-side-paging-filtering-and.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3641366038151750953'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3641366038151750953'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/server-side-paging-filtering-and.html' title='Server side paging, filtering and sorting using datatables'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-1283438106926610343</id><published>2013-11-05T19:07:00.000+05:30</published><updated>2013-11-05T19:10:49.089+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="knockoutjs"/><category scheme="http://www.blogger.com/atom/ns#" term="utility-function"/><title type='text'>Utility function to make an array observable in knockoutjs</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Suppose we have an array&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: jscript;&quot;&gt;var employees = [
    { EmployeeID: 1, EmployeeName: &quot;Naveen&quot; },
    { EmployeeID: 2, EmployeeName: &quot;Shebin&quot; }
];
&lt;/pre&gt;
&lt;br /&gt;
We can make it an observable array by calling it like this
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: jscript;&quot;&gt;
var observableEmployees = ko.observableArray( employees );
&lt;/pre&gt;
But as per knockoutjs documentation,&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;
Simply putting an object into an observableArray doesn’t make all of that object’s properties themselves observable. Of course, you can make those properties observable if you wish, but that’s an independent choice. An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.&lt;br /&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&amp;nbsp;This means that any change in array will be remembered. ie, adding and removing elements in an array is observed.
But changes to properties are not remembered. That is if employee[0].EmployeeName is changed to Noah, it wont be remembered. To do that we have to make each property ko.observable. Here is a small utility function to do so.

&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: jscript;&quot;&gt;
function MakeArrayKoObservableObjectArray(arr) {
    var observableArr = [];
    for (var i = 0; i &lt; arr.length; i++) {
        var observableObj = {}, obj = arr[i];
        for (var prop in obj) {
            if (obj.hasOwnProperty(prop)) {
                observableObj[prop] = ko.observable(obj[prop]);
            }
        }
        observableArr.push(observableObj);
    }
    return observableArr;
}
&lt;/pre&gt;
Now we can call it like this.
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: jscript;&quot;&gt;employees = MakeArrayKoObservableObjectArray( employees  );
var observableEmployees = ko.observableArray( employees );
&lt;/pre&gt;
&lt;pre style=&quot;brush: jscript;&quot;&gt;
&lt;/pre&gt;
Happy Coding!
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/1283438106926610343/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/utility-function-to-make-array.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1283438106926610343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1283438106926610343'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/utility-function-to-make-array.html' title='Utility function to make an array observable in knockoutjs'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-4961771968153363032</id><published>2013-11-04T06:08:00.003+05:30</published><updated>2013-11-04T06:15:56.083+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="remote"/><category scheme="http://www.blogger.com/atom/ns#" term="validate"/><title type='text'>Remote validation using jQuery Validate</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This is how jQuery validate is done in ASP.NET MVC&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
Markup&lt;/div&gt;
&lt;pre class=&quot;brush:xhtml&quot;&gt;
&amp;lt;form id=&amp;quot;manager-form&amp;quot; action=&amp;quot;&amp;quot;&amp;gt;
    &amp;lt;p&amp;gt;
        &amp;lt;label style=&amp;quot;display:inline-block; width: 200px;&amp;quot;&amp;gt;Manager Name&amp;lt;/label&amp;gt;
        &amp;lt;input type=&amp;quot;text&amp;quot; id=&amp;quot;Manager&amp;quot; name=&amp;quot;Manager&amp;quot; /&amp;gt;
    &amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;
        &amp;lt;input type=&amp;quot;submit&amp;quot; id=&amp;quot;saveManager&amp;quot; class=&amp;quot;btn btn-primary&amp;quot; value=&amp;quot;Save Sales Person&amp;quot; /&amp;gt;
    &amp;lt;/p&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/pre&gt;
&lt;div&gt;
JavaScript&lt;/div&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;
    function SetValidationRules(){
        $(&quot;#manager-form&quot;).validate({
            onkeyup: false,
            rules: {
                Manager: {
                    required: true,
                    minlength: 3,
                    remote: {
                        url: &#39;/Manager/IsManagerNameTaken&#39;,
                        type: &#39;POST&#39;,
                        dataType: &#39;json&#39;,
                        data: {
                            managerName: function () {
                                return $(&#39;#Manager&#39;).val();
                            }
                        }
                    }
                }
            },
            messages: {
                Manager: {
                    required: &quot;Manager name is required.&quot;,
                    minlength: &quot;Manager name should be 3 atleast characters.&quot;
                }
            }
        });
    }
&lt;/pre&gt;
&lt;div&gt;
Controller&lt;/div&gt;
&lt;pre class=&quot;brush: csharp;&quot;&gt;
    public ActionResult IsManagerNameTaken(string managerName)
    {
        var result = ManagerRepository.IsManagerNameTaken(managerName) ?
            &quot;Manager name is already taken. Try another!&quot; : &quot;&quot;;
        return Json(result);
    }
&lt;/pre&gt;
&lt;div&gt;Thats all to it. Happy coding!&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/4961771968153363032/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/remote-validation-using-jquery-validate.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/4961771968153363032'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/4961771968153363032'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/remote-validation-using-jquery-validate.html' title='Remote validation using jQuery Validate'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-6432679563916448744</id><published>2013-11-02T04:32:00.000+05:30</published><updated>2013-11-02T04:32:41.508+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="ajaxStart"/><category scheme="http://www.blogger.com/atom/ns#" term="ajaxStop"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="unbind"/><title type='text'>Overriding jQuery ajaxStart and ajaxStop locally</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
While performing ajax calls, I use to provide provide visual indicators for better user experience.&lt;br /&gt;
My preferred method is showing a modal at jQuery.ajaxStart which gets hidden at jQuery.ajaxStop&lt;br /&gt;
&lt;br /&gt;
The code goes like this&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;    $(document).on(&quot;ajaxStart&quot;, function () {
        $(&quot;#jQueryAjaxmodal&quot;).modal(&quot;show&quot;);
    }).on(&quot;ajaxStop&quot;, function () {
        $(&quot;#jQueryAjaxmodal&quot;).modal(&quot;hide&quot;);
    });
&lt;/pre&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;&lt;/pre&gt;
But this causes problem when one uses something like jQuery.autocomplete as ajaxStart fires on every keyup. To override this for a particular page, do this&lt;br /&gt;
&lt;br /&gt;
1. Namespace the ajaxStart and ajaxStop like this&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;    $(document).on(&quot;ajaxStart.myblock&quot;, function () {
        $(&quot;#jQueryAjaxmodal&quot;).modal(&quot;show&quot;);
    }).on(&quot;ajaxStop.myblock&quot;, function () {
        $(&quot;#jQueryAjaxmodal&quot;).modal(&quot;hide&quot;);
    });
&lt;/pre&gt;
2. Now on the DOM Ready of the page on which you wanna override this behaviour, unbind it&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;    $(document).on(&quot;ready&quot;, function () {
        $(document).off(&quot;.myblock&quot;);
    });
&lt;/pre&gt;
Courtesy: &lt;a href=&quot;http://stackoverflow.com/a/1062007/17447&quot;&gt;jQuery should I use multiple ajaxStart/ajaxStop handling&lt;/a&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/6432679563916448744/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/11/overriding-jquery-ajaxstart-and.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6432679563916448744'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6432679563916448744'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/11/overriding-jquery-ajaxstart-and.html' title='Overriding jQuery ajaxStart and ajaxStop locally'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-7797157247106739389</id><published>2013-04-03T16:58:00.002+05:30</published><updated>2013-11-02T04:18:17.354+05:30</updated><title type='text'>Validating date in JavaScript</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
People make me want to cry when they validate date using regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Some people, when confronted with a problem, think &quot;I know, I&#39;ll use regular expressions.&quot; Now they have two problems.&lt;/blockquote&gt;
&lt;br /&gt;
IMHO, its much simpler to use &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse&quot; target=&quot;_blank&quot;&gt;Date.Parse&lt;/a&gt;. For example,&lt;/div&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;var myDateString = &#39;01-Aug-12​&#39;;
var isValidDate = !isNaN( Date.parse( myDateString ));
&lt;/pre&gt;
Happy Coding!&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/7797157247106739389/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2013/04/validating-date-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/7797157247106739389'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/7797157247106739389'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2013/04/validating-date-in-javascript.html' title='Validating date in JavaScript'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-5521273400882498893</id><published>2012-06-06T19:57:00.004+05:30</published><updated>2012-06-06T20:00:18.300+05:30</updated><title type='text'>Steps to Register an application for web access with Google</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;Steps&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
1. Go to https://code.google.com/apis/console/&lt;br /&gt;
2. Now click on &quot;Create Project...&quot;&lt;br /&gt;
3. Click on &quot;API Access&quot;on the left menu.&lt;br /&gt;
4. Now &quot;Create an OAuth 2.0 client ID...&quot; on the center&lt;br /&gt;
5. Enter a Product Name and Product Logo(optional) and click Next&lt;br /&gt;
6. Now select the application type as &quot;Web Application&quot;&lt;br /&gt;
7. Enter the site or host name where you want this application deployed.&lt;br /&gt;
( In this demo I am setting it as http://localhost:4515/GoogleOAuth/oauth2callback.aspx)&lt;br /&gt;
8. Click to &quot;Create Client ID&quot; and you will be redirected to a page with&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;a. Client ID&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;b. Client Secret&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;C. Email&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;d. Redirect URLs&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;e. JavaScript Origins&lt;br /&gt;
&lt;br /&gt;
Now you Google Application of type Web Application &amp;nbsp;has been successfully registered.&lt;br /&gt;
Please store all the information securely so that you can use it at a later stage.&lt;br /&gt;
&lt;br /&gt;
Happy Coding :)&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/5521273400882498893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2012/06/steps-to-register-application-for-web.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5521273400882498893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5521273400882498893'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2012/06/steps-to-register-application-for-web.html' title='Steps to Register an application for web access with Google'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-5555235988479552191</id><published>2011-04-15T19:15:00.010+05:30</published><updated>2011-04-16T13:32:27.484+05:30</updated><title type='text'>Tutorial - Post data on a Facebook Company Page as Admin</title><content type='html'>Its been a while I have blogged about something.&lt;br /&gt;
Have been working on the Facebook API and one of the requirement is to post data into a Facebook company page from one&#39;s website.&lt;br /&gt;
&lt;br /&gt;
Here is a step by step procedure on posting data to a company page(on which you have admin rights) using the Facebook API.&lt;br /&gt;
Feel free to skip the first two steps if you know how to set up a Facebook Application and to create pages in Facebook.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 1 - Creating a Facebook Application&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Sign in to your Facebook Account and key in the following URL to your address bar&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.facebook.com/developers/createapp.php&quot;&gt;http://www.facebook.com/developers/createapp.php&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Set Application Name as &quot;My Demo Application&quot;&lt;/blockquote&gt;&lt;br /&gt;
Now I am adding the following data also to the Website tab of the core settings&lt;br /&gt;
(Lets come to that later)&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Set Site URL as http://localhost/FacebookPost/&lt;/blockquote&gt;&lt;br /&gt;
After entering, click on the Save Changes Button.&lt;br /&gt;
Now you will be re directed to a page where your App Settings are displayed.&lt;br /&gt;
Copy the values App ID, App Key and App Secret and store them securely.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 2 - Creating a Facebook Page&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Now, key in the following URL to your address bar&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.facebook.com/pages/create.php&quot;&gt;http://www.facebook.com/pages/create.php&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Now select the Page Type. Here is what I selected.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Type         : Company, Organisation (Click on the type you want to create.&lt;br /&gt;
Category     : Computers/Technology&lt;br /&gt;
Comapany Name: MyCompany&lt;/blockquote&gt;&lt;br /&gt;
Now agree to the Facebook Pages Terms and click Get Started.&lt;br /&gt;
Your page is created by Facebook and you will be landed on a URL like that&lt;br /&gt;
http://www.facebook.com/pages/&lt;page_name&gt;/&lt;page_id&gt;?created&lt;br /&gt;
Please copy the PageID and store it securely.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 3 - Create a website locally in your system&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Open up your Visual Studio and Create an empty website. &lt;br /&gt;
Create a page &quot;Default.aspx&quot; and run the website and allow the web.config to be created.&lt;br /&gt;
&lt;br /&gt;
(I have mapped my site to my IIS with the application name FacebookPost.&lt;br /&gt;
Please note that it is being done to match the Site URL I have set at the Facebook Application Page)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 4 - Pick a Facebook C# SDK to communicate with the Facebook Graph API.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
My research came up with two options&lt;br /&gt;
1. Use the &lt;a href=&quot;http://developers.facebook.com/blog/post/395/&quot;&gt;Facebook&#39;s own lesser used SDK&lt;/a&gt; found at GitHub ( &lt;a href=&quot;https://github.com/facebook/csharp-sdk&quot;&gt;link here&lt;/a&gt; )&lt;br /&gt;
&lt;br /&gt;
2. Use the more widely used third party C# SDK hosted at Codeplex ( &lt;a href=&quot;http://facebooksdk.codeplex.com/&quot;&gt;link here&lt;/a&gt; )&lt;br /&gt;
&lt;br /&gt;
I preferred Facebook&#39;s own SDK. Actually, you can also write your own API if you want. Its set of simple WebRequest/WebResponse. So I went to GitHub where the SDK was hosted and downloaded the folder named facebook and changed it to Facebook ( :) Pascal Casing) and pasted it inside my App_Code&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 5 - Modify your AppSettings at web.config&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Remember those AppID, AppKey, AppSecret, PageID I asked you to keep securely?&lt;br /&gt;
Lets now store them at AppSettings of web.config.&lt;br /&gt;
(Ideally you would be much better of encrypting them, but details on encrypting is beyond the scope of this tutorial.)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;&amp;lt;appSettings&amp;gt;
    &amp;lt;add key=&amp;quot;AppID&amp;quot; value=&amp;quot;Your App ID&amp;quot;/&amp;gt;
    &amp;lt;add key=&amp;quot;AppKey&amp;quot; value=&amp;quot;Your App Key&amp;quot;/&amp;gt;
    &amp;lt;add key=&amp;quot;AppSecret&amp;quot; value=&amp;quot;Your App Secret&amp;quot;/&amp;gt;
    &amp;lt;add key=&amp;quot;PageID&amp;quot; value=&amp;quot;Your Page ID&amp;quot;/&amp;gt;
  &amp;lt;/appSettings&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Step 6 - Create a form for entering the data&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Open up Default.aspx in Visual Studio and change Source like this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:xhtml&quot;&gt;&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&amp;gt;
&amp;lt;head runat=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;title&amp;gt;Codovations Demo - Posting to Facebook Company Page&amp;lt;/title&amp;gt;
    &amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;
        .title {
            display:inline-block;
            vertical-align:top;
            width:100px;   
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;div&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Message&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;MessageText&amp;quot; runat=&amp;quot;server&amp;quot; TextMode=&amp;quot;MultiLine&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Name&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;NameText&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Description&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;DescriptionText&amp;quot; runat=&amp;quot;server&amp;quot; TextMode=&amp;quot;MultiLine&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Picture(URL)&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;PictureText&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Caption&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;CaptionText&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;Link&amp;lt;/label&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;LinkText&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;200px&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;label class=&amp;quot;title&amp;quot;&amp;gt;&amp;lt;/label&amp;gt;
        &amp;lt;asp:Button ID=&amp;quot;PostToCompanyButton&amp;quot; runat=&amp;quot;server&amp;quot; 
                Text=&amp;quot;Post to Company Page&amp;quot; onclick=&amp;quot;PostToCompanyButton_Click&amp;quot;/&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Step 7 - Posting data on the Company Page.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
This is the trickiest part in this project. Facebook provides no straight forward way to post data to a company page. Here is how we achieve that.&lt;br /&gt;
&lt;br /&gt;
First we will request oAuth from Facebook and on successful authorisation we get an access token.&lt;br /&gt;
Using the token we fetch &quot;me/accounts&quot; and will check in that account array, an account that matches the stored PageID. If found, we fetch the associated accesstoken for that page. When we get that we post the content to &quot;me/feeds&quot; using that accesstoken like this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;if (Request.Params[&quot;code&quot;] != null)
{
    Status.Text = &quot;&quot;;
    string accessToken = GetAccessToken(Request.Params[&quot;code&quot;]);
    FacebookAPI api = new FacebookAPI(accessToken);

    JSONObject me = api.Get(&quot;me/accounts&quot;);
    Dictionary&lt;string, JSONObject&gt; dictionary = me.Dictionary;
    JSONObject myPage = me.Dictionary[&quot;data&quot;].Array
        .SingleOrDefault(d =&gt; d.Dictionary[&quot;id&quot;].String.Equals(ConfigurationManager.AppSettings[&quot;PageID&quot;].ToString()));
    if (myPage != null)
    {
        Dictionary&lt;string, string&gt; parameters = Session[&quot;DataForFacebook&quot;] as Dictionary&lt;string, string&gt;;
        Session.Remove(&quot;DataForFacebook&quot;);
        string newToken = myPage.Dictionary[&quot;access_token&quot;].String;
        var app = new FacebookAPI(newToken);
        var response = app.Post(&quot;me/feed&quot;, parameters);
        if (response != null &amp;&amp; response.Dictionary[&quot;&quot;] != null)
        {
            Status.Text = &quot;Data posted successfully to company page.&quot;;
            ClearData();
        }
        else
        {
            Status.Text = &quot;An error occured while posting.&quot;;
        }
    }
}&lt;/pre&gt;&lt;br /&gt;
Two helper methods used are &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;private string GetAccessToken(string accessToken)
    {       
        Dictionary&lt;string, string&gt; args = GetOauthTokens(accessToken);
        return args[&quot;access_token&quot;];
    }

    private Dictionary&lt;string, string&gt; GetOauthTokens(string accessToken)
    {
        Dictionary&lt;string, string&gt; tokens = new Dictionary&lt;string, string&gt;();
        try
        {
            string url = String.Format(&quot;{0}client_id={1}&amp;redirect_uri={2}&amp;client_secret={3}&amp;code={4}&amp;scope={5}&quot;,
                            &quot;https://graph.facebook.com/oauth/access_token?&quot;,
                            ConfigurationManager.AppSettings[&quot;AppID&quot;].ToString(),
                            &quot;http://localhost/FacebookPost/Default.aspx&quot;,
                            ConfigurationManager.AppSettings[&quot;AppSecret&quot;].ToString(),
                            accessToken,
                            &quot;publish_stream,offline_access,manage_pages&quot;);

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string retVal = reader.ReadToEnd();

                foreach (string token in retVal.Split(&#39;&amp;&#39;))
                {
                    tokens.Add(token.Substring(0, token.IndexOf(&quot;=&quot;)),
                        token.Substring(token.IndexOf(&quot;=&quot;) + 1, token.Length - token.IndexOf(&quot;=&quot;) - 1));
                }
            }
        }
        catch (Exception err)
        {

        }
        return tokens;
    }&lt;/pre&gt;&lt;br /&gt;
Thats all to it. Happy coding.</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/5555235988479552191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2011/04/tutorial-post-data-on-facebook-company.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5555235988479552191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5555235988479552191'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2011/04/tutorial-post-data-on-facebook-company.html' title='Tutorial - Post data on a Facebook Company Page as Admin'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-5527242092047727167</id><published>2011-01-18T16:55:00.000+05:30</published><updated>2011-01-18T16:55:43.057+05:30</updated><title type='text'>Generating PDF from DataTable using iTextSharp</title><content type='html'>iTextSharp is a wonderful open-source tool for generating PDF in C#.&lt;br /&gt;
&lt;br /&gt;
Here is a simple generic helper class for Exporting data from a DataTable to a PDF file.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;
using System;
using System.Web;
using System.Data;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace yetanothercoder
{
    /// &lt;summary&gt;
    /// Summary description for CreatePdf
    /// &lt;/summary&gt;
    public class PDFExporter
    {

        private readonly DataTable dataTable;
        private readonly string fileName;
        private readonly bool timeStamp;

        public PDFExporter(DataTable dataTable, string fileName, bool timeStamp)
        {
            this.dataTable = dataTable;
            this.fileName = timeStamp ? String.Format(&quot;{0}-{1}&quot;, fileName, GetTimeStamp(DateTime.Now)) : fileName;
            this.timeStamp = timeStamp;
        }

        public void ExportPDF()
        {
            HttpResponse Response = HttpContext.Current.Response;
            Response.Clear();
            Response.ContentType = &quot;application/octet-stream&quot;;
            Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + fileName + &quot;.pdf&quot;);

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 10, 10, 90, 10);

            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

            //set some header stuff
            document.AddTitle(fileName);
            document.AddSubject(String.Format(&quot;Table of {0}&quot;, fileName));
            document.AddCreator(&quot;www.yetanothercoder.com&quot;);
            document.AddAuthor(&quot;naveenj&quot;);

            // step 3: we open the document
            document.Open();

            // step 4: we add content to the document
            CreatePages(document);

            // step 5: we close the document
            document.Close();
        }

        private void CreatePages(Document document)
        {
            document.NewPage();
            document.Add(FormatPageHeaderPhrase(dataTable.TableName));
            PdfPTable pdfTable = new PdfPTable(dataTable.Columns.Count);
            pdfTable.DefaultCell.Padding = 3;
            pdfTable.WidthPercentage = 100; // percentage
            pdfTable.DefaultCell.BorderWidth = 2;
            pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

            foreach (DataColumn column in dataTable.Columns)
            {
                pdfTable.AddCell(FormatHeaderPhrase(column.ColumnName));
            }
            pdfTable.HeaderRows = 1;  // this is the end of the table header
            pdfTable.DefaultCell.BorderWidth = 1;

            foreach (DataRow row in dataTable.Rows)
            {
                foreach (object cell in row.ItemArray)
                {
                    //assume toString produces valid output
                    pdfTable.AddCell(FormatPhrase(cell.ToString()));
                }
            }

            document.Add(pdfTable);
        }

        private static Phrase FormatPageHeaderPhrase(string value)
        {
            return new Phrase(value, FontFactory.GetFont(FontFactory.TIMES, 10, Font.BOLD, new BaseColor(255, 0, 0)));
        }

        private static Phrase FormatHeaderPhrase(string value)
        {
            return new Phrase(value, FontFactory.GetFont(FontFactory.TIMES, 8, Font.UNDERLINE, new BaseColor(0, 0, 255)));
        }

        private Phrase FormatPhrase(string value)
        {
            return new Phrase(value, FontFactory.GetFont(FontFactory.TIMES, 8));
        }

        private string GetTimeStamp(DateTime value)
        {
            return value.ToString(&quot;yyyyMMddHHmmssffff&quot;);
        }
    }
}

&lt;/pre&gt;&lt;br /&gt;
Now if you have an Export to Excel Button, you can generate pdf easily like this&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;
protected void ExportToPDFLink_Click(object sender, EventArgs e)
{
    PDFExporter pdf = new PDFExporter(GetCostumers(), &quot;customer&quot;, true);
    pdf.ExportPDF();
}

&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/5527242092047727167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2011/01/generating-pdf-from-datatable-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5527242092047727167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/5527242092047727167'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2011/01/generating-pdf-from-datatable-using.html' title='Generating PDF from DataTable using iTextSharp'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-2652204024412377972</id><published>2011-01-18T14:56:00.003+05:30</published><updated>2011-01-18T15:01:00.302+05:30</updated><title type='text'>SQLite in ASP.NET Web Forms</title><content type='html'>What is SQLite?&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;SQLite is a in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. &lt;/blockquote&gt;&lt;br /&gt;
SQLite is a must have tool for manipulating database for small to mid size projects, there by cutting the cost of maintaining an extra database server. Its very fast and has less percentage of crashing also.&lt;br /&gt;
Next, download an SQLite Manager from which we can create and manipulate a sample database and its objects. For that I used the &lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/&quot;&gt;SQLite Manager add on for Firefox&lt;/a&gt;. Feel free to use any other tools. &lt;br /&gt;
Now we are all set to use the SQLite database in our ASP.NET application. &lt;br /&gt;
&lt;br /&gt;
Steps to follow&lt;br /&gt;
&lt;br /&gt;
1. Create a Sample Database called Northwind.sqlite using the firefix addon.&lt;br /&gt;
2. Add a table named Customers to it with some fields and some dummy data to it.&lt;br /&gt;
3. Create an empty website in .NET 3.5 using Visual Studio&lt;br /&gt;
4. Create an App_Data folder in it and place the Northwind.sqlite to it&lt;br /&gt;
5. Add to the empty website as reference, the dll&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;using System.Data.SQLite&lt;/pre&gt;6. Create a default page in your application and place a gridview in that&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;&amp;lt;asp:GridView ID=&amp;quot;CustomersGrid&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
7. At code behind, add reference to System.Data.SQLite;&lt;br /&gt;
8. Write the code to get data from Customers Table here.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;private void BindCustomerGrid()
{
    string commandText = &quot;SELECT * FROM Customers&quot;;
    string path_to_db = System.IO.Path.Combine(Server.MapPath(&quot;.&quot;), &quot;App_Data&quot;, &quot;Northwind.sqlite3&quot;);
    string connectionString = String.Format(&quot;Data Source={0};Version=3;&quot;, path_to_db);
    DataTable dataTable = new DataTable();
    using (SQLiteConnection connection = new SQLiteConnection(connectionString))
    {
        using (SQLiteCommand cmd = new SQLiteCommand(commandText, connection))
        {
            cmd.CommandType = CommandType.Text;
            using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd))
            {
                adapter.Fill(dataTable);
            }
        }
    }
    CustomersGrid.DataSource = dataTable;
    CustomersGrid.DataBind();
}
&lt;/pre&gt;&lt;br /&gt;
Thats all to it. Happy Coding!</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/2652204024412377972/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2011/01/sqlite-in-aspnet-web-forms.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2652204024412377972'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2652204024412377972'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2011/01/sqlite-in-aspnet-web-forms.html' title='SQLite in ASP.NET Web Forms'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-7454006716829230492</id><published>2010-12-07T18:55:00.004+05:30</published><updated>2010-12-07T18:59:40.749+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="maintain checkbox state multiple gridviews"/><title type='text'>Maintaining CheckBox State of multiple GridViews</title><content type='html'>Almost two years back, I did a post on &lt;a target=&quot;_blank&quot; href=&quot;http://fun2code.blogspot.com/2009/02/maintaining-state-of-checkbox-in.html&quot;&gt;Maintaining State of Checkbox while Paging in Gridview&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
This post is an extension to it. this is the code to maintain CheckBox states of multiple gridviews in a page.&lt;br /&gt;
&lt;br /&gt;
The logic is simple. We are storing the primary keys of the checkboxes that are checked into a List&amp;lt;T&amp;gt; where T = data type of the primary key. This is done at PageIndexChanging before we change the page index and re-bind the GridView. And at RowDataBound we are checking whether the DataKeyName(primary key) of each row is in the List&lt;t&gt;. If present , we mark the CheckBox as checked.&lt;br /&gt;
&lt;br /&gt;
At ASPX&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:xhtml&quot;&gt;&amp;lt;asp:GridView ID=&amp;quot;gvProducts&amp;quot; runat=&amp;quot;server&amp;quot; 
            AllowPaging=&amp;quot;True&amp;quot; 
            AutoGenerateColumns=&amp;quot;False&amp;quot;
            DataKeyNames=&amp;quot;ProductID&amp;quot;
            OnPageIndexChanging=&amp;quot;gvProducts_PageIndexChanging&amp;quot; 
            OnRowDataBound=&amp;quot;gvProducts_RowDataBound&amp;quot;&amp;gt;
    &amp;lt;Columns&amp;gt;
        &amp;lt;asp:TemplateField HeaderText=&amp;quot;Select&amp;quot;&amp;gt;
            &amp;lt;ItemTemplate&amp;gt;
                &amp;lt;asp:CheckBox ID=&amp;quot;chkSelect&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;
            &amp;lt;/ItemTemplate&amp;gt;
        &amp;lt;/asp:TemplateField&amp;gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;ProductID&amp;quot; HeaderText=&amp;quot;ProductID&amp;quot; InsertVisible=&amp;quot;False&amp;quot;
        ReadOnly=&amp;quot;True&amp;quot; SortExpression=&amp;quot;ProductID&amp;quot; /&amp;gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;ProductName&amp;quot; HeaderText=&amp;quot;ProductName&amp;quot; SortExpression=&amp;quot;ProductName&amp;quot; /&amp;gt;
    &amp;lt;/Columns&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&amp;lt;asp:GridView ID=&amp;quot;gvCustomers&amp;quot; runat=&amp;quot;server&amp;quot; 
            AllowPaging=&amp;quot;True&amp;quot; 
            AutoGenerateColumns=&amp;quot;False&amp;quot;
            DataKeyNames=&amp;quot;CustomerID&amp;quot;
            OnPageIndexChanging=&amp;quot;gvCustomers_PageIndexChanging&amp;quot; 
            OnRowDataBound=&amp;quot;gvCustomers_RowDataBound&amp;quot;&amp;gt;
    &amp;lt;Columns&amp;gt;
        &amp;lt;asp:TemplateField HeaderText=&amp;quot;Select&amp;quot;&amp;gt;
            &amp;lt;ItemTemplate&amp;gt;
                &amp;lt;asp:CheckBox ID=&amp;quot;chkSelect&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;
            &amp;lt;/ItemTemplate&amp;gt;
        &amp;lt;/asp:TemplateField&amp;gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;CustomerID&amp;quot; HeaderText=&amp;quot;ID&amp;quot; InsertVisible=&amp;quot;False&amp;quot; /&amp;gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;CompanyName&amp;quot; HeaderText=&amp;quot;CompanyName&amp;quot;  /&amp;gt;
    &amp;lt;/Columns&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
At ASPX.CS&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;private List&amp;lt;int&amp;gt; ProductIDs
{
    get
    {
        if (this.ViewState[&amp;quot;ProductIDs&amp;quot;] == null)
        {
            this.ViewState[&amp;quot;ProductIDs&amp;quot;] = new List&amp;lt;int&amp;gt;();
        }
        return this.ViewState[&amp;quot;ProductIDs&amp;quot;] as List&amp;lt;int&amp;gt;;
    }
}

private List&amp;lt;string&amp;gt; CustomerIDs
{
    get
    {
        if (this.ViewState[&amp;quot;CustomerIDs&amp;quot;] == null)
        {
            this.ViewState[&amp;quot;CustomerIDs&amp;quot;] = new List&amp;lt;string&amp;gt;();
        }
        return this.ViewState[&amp;quot;CustomerIDs&amp;quot;] as List&amp;lt;string&amp;gt;;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid(gvProducts, &amp;quot;Products&amp;quot;);
        BindGrid(gvCustomers, &amp;quot;Customers&amp;quot;);
    }
}

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    foreach (GridViewRow gvr in gvProducts.Rows)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex][&amp;quot;ProductID&amp;quot;]);
            if (chkSelect.Checked &amp;amp;&amp;amp; !this.ProductIDs.Contains(productID))
            {
                this.ProductIDs.Add(productID);
            }
            else if (!chkSelect.Checked &amp;amp;&amp;amp; this.ProductIDs.Contains(productID))
            {
                this.ProductIDs.Remove(productID);
            }
        }
    }
    gvProducts.PageIndex = e.NewPageIndex;
    BindGrid(gvProducts, &amp;quot;Products&amp;quot;);
}

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex][&amp;quot;ProductID&amp;quot;]);
            chkSelect.Checked = this.ProductIDs.Contains(productID);
        }
    }
}

protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    foreach (GridViewRow gvr in gvCustomers.Rows)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            string customerID = gvCustomers.DataKeys[gvr.RowIndex][&amp;quot;CustomerID&amp;quot;].ToString();
            if (chkSelect.Checked &amp;amp;&amp;amp; !this.CustomerIDs.Contains(customerID))
            {
                this.CustomerIDs.Add(customerID);
            }
            else if (!chkSelect.Checked &amp;amp;&amp;amp; this.CustomerIDs.Contains(customerID))
            {
                this.CustomerIDs.Remove(customerID);
            }
        }
    }
    gvCustomers.PageIndex = e.NewPageIndex;
    BindGrid(gvCustomers, &amp;quot;Customers&amp;quot;);
}

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            string customerID = gvCustomers.DataKeys[gvr.RowIndex][&amp;quot;CustomerID&amp;quot;].ToString();
            chkSelect.Checked = this.CustomerIDs.Contains(customerID);
        }
    }
}

private DataTable PopulateData(string tableName)
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&amp;quot;NorthwindConnectionString&amp;quot;].ConnectionString))
    {
        string sql = String.Format(&amp;quot;SELECT * FROM {0}&amp;quot;, tableName);
        using (SqlDataAdapter adap = new SqlDataAdapter(sql, conn))
        {
            adap.Fill(dt);
        }
    }
    return dt;
}

private void BindGrid(GridView gvTemp, string tableName)
{
    gvTemp.DataSource = PopulateData(tableName);
    gvTemp.DataBind();
}
&lt;/pre&gt;&lt;br /&gt;
Happy Coding!&lt;br /&gt;
&lt;br /&gt;
P.S: Database in use is Northwind Sample Database.</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/7454006716829230492/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2010/12/maintaining-checkbox-state-of-multiple.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/7454006716829230492'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/7454006716829230492'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2010/12/maintaining-checkbox-state-of-multiple.html' title='Maintaining CheckBox State of multiple GridViews'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-3542418707227386989</id><published>2010-12-07T11:03:00.003+05:30</published><updated>2010-12-07T11:06:43.614+05:30</updated><title type='text'>Insert Arabic Words into Database</title><content type='html'>Often at forums.asp.net we come across the problem that occurs while inserting foreign language to the database. Supopose I wanted to insert &#39;اتصالات&#39; to a table named &#39;table_test&#39;. When I look at the entry at all I see is ?????. &lt;br /&gt;
&lt;br /&gt;
Why did this happen? Its because the field we enter the data needs to be having COLLATE Arabic.&lt;br /&gt;
&lt;br /&gt;
To understand that we must first understand what collation is.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is collation?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Collation refers to a set of rules that determine how data is sorted and compared. &lt;br /&gt;
Character data is sorted using rules that define the correct character sequence, &lt;br /&gt;
with options for specifying &lt;br /&gt;
1) case-sensitivity, &lt;br /&gt;
2) accent marks, &lt;br /&gt;
3) kana character types and &lt;br /&gt;
4)character width.&lt;br /&gt;
&lt;br /&gt;
Case sensitivity&lt;br /&gt;
&lt;br /&gt;
If A and a, B and b, etc. are treated in the same way then it is case-insensitive. A computer treats A and a differently because it uses ASCII code to differentiate the input. The ASCII value of A is 65, while a is 97. The ASCII value of B is 66 and b is 98.&lt;br /&gt;
&lt;br /&gt;
Accent sensitivity&lt;br /&gt;
&lt;br /&gt;
If a and á, o and ó are treated in the same way, then it is accent-insensitive. A computer treats a and á differently because it uses ASCII code for differentiating the input. The ASCII value of a is 97 and áis 225. The ASCII value of o is 111 and ó is 243.&lt;br /&gt;
&lt;br /&gt;
Kana Sensitivity&lt;br /&gt;
&lt;br /&gt;
When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive. &lt;br /&gt;
&lt;br /&gt;
Width sensitivity&lt;br /&gt;
&lt;br /&gt;
When a single-byte character (half-width) and the same character when represented as a double-byte character (full-width) are treated differently then it is width sensitive.&lt;/blockquote&gt;&lt;br /&gt;
Now we have identified the problem. Lets try to solve it using an example.&lt;br /&gt;
In this example I am saving a textbox value to database on the click of a button.&lt;br /&gt;
&lt;br /&gt;
Suppose I have a table named table_test with two columns&lt;br /&gt;
1. [myid] int auto&lt;br /&gt;
2. [mytext] nvarchar(50)&lt;br /&gt;
&lt;br /&gt;
I want to enter the arabic word to the field mytext.&lt;br /&gt;
&lt;br /&gt;
Step1&lt;br /&gt;
At Database provide collate to the field on which we want to insert the arabic word&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;ALTER TABLE test_table
ALTER COLUMN mytext VARCHAR(50) COLLATE Arabic_CI_AI
&lt;/pre&gt;Step 2&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;protected void myButton_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;forums_ConnectionString&quot;].ConnectionString))
    {
        conn.Open();
        string sql = String.Format(&quot;INSERT INTO test_table (mytext) VALUES (N&#39;{0}&#39;)&quot;,
            mytext.Text.Trim());
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            cmd.ExecuteNonQuery();
        }
    }
}
&lt;/pre&gt;Please note that I have put N&#39; before the value.&lt;br /&gt;
Read the article &lt;a href=&quot;http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html&quot; target=&quot;_blank&quot;&gt;Why do some SQL strings have an &#39;N&#39; prefix?&lt;/a&gt; if you want to know its significance.</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/3542418707227386989/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2010/12/insert-arabic-words-into-database.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3542418707227386989'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3542418707227386989'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2010/12/insert-arabic-words-into-database.html' title='Insert Arabic Words into Database'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-1497432109213488113</id><published>2010-11-16T10:16:00.004+05:30</published><updated>2010-11-16T11:14:10.561+05:30</updated><title type='text'>Prevent site from getting loaded on iframe</title><content type='html'>What if somebody loads your obfuscated fancy javascript or AJAX methods to drive traffic to their site utilising your bandwidth? Here is a simple solution in javascript.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:javascript&quot;&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
//&amp;lt;![CDATA[
    if (window.top !== window.self) {
        document.write = &amp;quot;&amp;quot;;
        window.top.location = window.self.location;
        setTimeout(function () {
            document.body.innerHTML = &amp;#39;&amp;#39;;
        }, 1);
        window.self.onload = function (evt) {
            document.body.innerHTML = &amp;#39;&amp;#39;;
        }; 
    }
//]]&amp;gt;
&amp;lt;/script&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
Paste that javascript snippet inside your head tag of your html.&lt;br /&gt;
Twitter(Dustin Diaz) uses that at the &lt;a href=&quot;http://twitter.com/about/resources/widgets/widget_profile&quot;&gt;Twitter Profile Widget Page&lt;/a&gt;. :(&lt;br /&gt;
I found it the hard way (lol). Way to go Dustin!</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/1497432109213488113/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2010/11/prevent-your-site-from-getting-loaded.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1497432109213488113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1497432109213488113'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2010/11/prevent-your-site-from-getting-loaded.html' title='Prevent site from getting loaded on iframe'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-464776931925859118</id><published>2010-11-15T11:36:00.000+05:30</published><updated>2010-11-15T11:36:04.261+05:30</updated><title type='text'>HTML Encode Decode in Javascript/jQuery</title><content type='html'>Last day, I had a resolute and unyielding need to HTML Decode a string.&lt;br /&gt;
I saw many solutions but was not satisfied.&lt;br /&gt;
Then I found the &lt;a href=&quot;http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding&quot;&gt;StackOverFlow reference&lt;/a&gt; which is the most optimal solution I have seen till date. Just pasting it here for easy reference. &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;
function htmlEncode(value){ 
  return $(&#39;&amp;lt;div/&amp;gt;&#39;).text(value).html(); 
} 

function htmlDecode(value){ 
  return $(&#39;&amp;lt;div/&amp;gt;&#39;).html(value).text(); 
}

&lt;/pre&gt;&lt;br /&gt;
Did I say jQuery? I sure did say optimal :P&lt;br /&gt;
Happy Coding!&lt;br /&gt;
&lt;br /&gt;
Dependency: jQuery</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/464776931925859118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2010/11/html-encode-decode-in-javascriptjquery.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/464776931925859118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/464776931925859118'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2010/11/html-encode-decode-in-javascriptjquery.html' title='HTML Encode Decode in Javascript/jQuery'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-3678071981011973487</id><published>2010-11-10T15:32:00.003+05:30</published><updated>2010-11-10T15:35:44.295+05:30</updated><title type='text'>Different ways to create a DataTable and set Schema</title><content type='html'>Was hanging out StackOverflow and I saw &lt;a href=&quot;http://stackoverflow.com/questions/441023/fastest-way-to-convert-datatable-to-generic-list#441059&quot;&gt;Marc Gravell initializing the DataTable&lt;/a&gt; in a simpler and cleaner way. Here goes &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;DataTable dataTable = new DataTable
{
    Columns = {
        {&quot;ID&quot;, typeof(int)},
        {&quot;Name&quot;, typeof(string)},
        &quot;Location&quot;
    },
    TableName=&quot;NaveenTest&quot;
};
dataTable.Rows.Add(1, &quot;Naveen&quot;, &quot;Coder&quot;);
&lt;/pre&gt;&lt;br /&gt;
&lt;blockquote&gt;Please note that if you don&#39;t specify the type it will automatically be converted to string. See how &quot;Location&quot; is initialised&lt;br /&gt;
&lt;/blockquote&gt;And my old method&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;DataTable dtOld = new DataTable(&quot;NaveenTest&quot;);
dtOld.Columns.Add(&quot;MyID&quot;, typeof(int));
dtOld.Columns.Add(&quot;Name&quot;);
dtOld.Columns.Add(&quot;Location&quot;);
DataRow drOld = new DataRow();
drOld[0] = 1;
drOld[1] = &quot;Naveen&quot;;
drOld[2] = &quot;Coder&quot;;
dtOld.Rows.Add(drOld);
&lt;/pre&gt;&lt;br /&gt;
Aaarghhh...&lt;br /&gt;
&lt;br /&gt;
A lot cleaner!&lt;br /&gt;
What do you say?</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/3678071981011973487/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2010/11/different-ways-to-create-datatable-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3678071981011973487'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3678071981011973487'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2010/11/different-ways-to-create-datatable-and.html' title='Different ways to create a DataTable and set Schema'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-4698266886015925121</id><published>2009-12-22T14:25:00.004+05:30</published><updated>2010-11-08T19:30:40.449+05:30</updated><title type='text'>Gray Scale images in all Browsers</title><content type='html'>Its been a while since I blogged on anything.&lt;br /&gt;
Today I came across a post at forums.asp.net: &lt;a href=&quot;http://forums.asp.net/t/1506347.aspx&quot;&gt;change color image to gray scale&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;GrayScale - Converts the colors of the object to 256 shades of gray.&lt;/blockquote&gt;An example will be&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;http://sites.google.com/site/fun2code/Home/naveenj-org-to-gray.jpg&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
Since GrayScale filter is a filter present in the Internet Explorer, &lt;br /&gt;
we can achieve this result in IE simply by using the css property&lt;br /&gt;
&lt;pre class=&quot;brush:css&quot;&gt;.greyscale_filter{ 
 filter: gray;
}
&lt;/pre&gt;or&lt;br /&gt;
&lt;pre class=&quot;brush:css&quot;&gt;.greyscale_filter{
 -ms-filter: &quot;progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)&quot;;
 filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
}
&lt;/pre&gt;Here the second method is more advanced and must have Internet Explorer 5.5 or later to work properly.&lt;br /&gt;
&lt;br /&gt;
There are number of filters in IE apart from GrayScale. Here are some lists&lt;br /&gt;
&lt;br /&gt;
1. &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms673539(VS.85).aspx&quot;&gt;Static filters by MSDN&lt;/a&gt;&lt;br /&gt;
2. &lt;a href=&quot;http://www.ssi-developer.net/css/visual-filters.shtml&quot;&gt;CSS Visual Filters&lt;/a&gt;&lt;br /&gt;
3. &lt;a href=&quot;http://www.javascriptkit.com/filters/index.shtml&quot;&gt;IE Multimedia filters reference&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the case with Internet Explorer.&lt;br /&gt;
Now the big question.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Why does not the GrayScale filter work with browsers like Chrome, Firefox and Safari?&lt;br /&gt;
Simple. The filter is specific to IE only.&lt;/blockquote&gt;&lt;br /&gt;
While I was searching through the internet for a good solution, I found &lt;a href=&quot;http://stackoverflow.com/questions/878786/css-do-non-ie-browsers-support-the-filter-attribute#878795&quot;&gt;this at SO&lt;/a&gt;&lt;br /&gt;
There I discovered a wonderful javascript coder named &lt;a href=&quot;http://james.padolsey.com/&quot;&gt;James Padolsey&lt;/a&gt; who is just 19!&lt;br /&gt;
&lt;br /&gt;
Copied his GrayScale.js which according to James Padolsey is &lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;b&gt;Grayscale.js is an experimental attempt to emulate Microsoft&#39;s &lt;br /&gt;
proprietary &#39;grayscale&#39; filter (available in most IE versions).&lt;/b&gt;&lt;/blockquote&gt;&lt;br /&gt;
And it worked like a charm.&lt;br /&gt;
&lt;br /&gt;
So here are the steps involved in achieving the hover effect&lt;br /&gt;
&lt;br /&gt;
1. Grab the GrayScale.js from James Padolsey&#39;s site. (&lt;a href=&quot;http://james.padolsey.com/demos/grayscale/grayscale.js&quot;&gt;The link is here&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
2. Add refernce to jQuery. Please note that jQuery is not required for grayscale.js - it&#39;s only used for this demo&lt;br /&gt;
Its better to refer jQuery like this (&lt;a href=&quot;http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/&quot;&gt;Read Dave Ward ka Encosia on this topic&lt;/a&gt;)&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
  &lt;/pre&gt;than&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;/js/jQuery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
  &lt;/pre&gt;3. Add a small javascript snippet like this&lt;br /&gt;
&lt;pre class=&quot;brush:jscript&quot;&gt;$(document).ready(function(){
     $(&#39;img.greyonhover&#39;).hover(function(){
    grayscale(this);
   }, function(){
    grayscale.reset(this);
   })
  });
  &lt;/pre&gt;4. Assign the class=&quot;greyonhover&quot; for the image you want to implement grayscale effect onhover &lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;img class=&amp;quot;greyonhover&amp;quot; src=&amp;quot;images/naveenj-thumb.jpg&amp;quot; /&amp;gt;
  &lt;/pre&gt;&lt;br /&gt;
This is all you have to do to achieve greying effect on hover.  &lt;a href=&quot;http://sites.google.com/site/fun2code/Home/grayscaling.zip&quot;&gt;Download Source Code(9k)&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/4698266886015925121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/12/gray-scale-images-in-all-browsers.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/4698266886015925121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/4698266886015925121'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/12/gray-scale-images-in-all-browsers.html' title='Gray Scale images in all Browsers'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-1042968033133456316</id><published>2009-10-21T12:49:00.005+05:30</published><updated>2013-11-05T19:12:53.177+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="gridview"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="selectable row"/><title type='text'>Selectable GridViewRow using Javascript</title><content type='html'>Hope you have read my post about &lt;a href=&quot;http://fun2code.blogspot.com/2009/10/gridviewrow-as-select-button.html&quot;&gt;Selectable GridViewRow&lt;/a&gt;&lt;br /&gt;
If you haven&#39;t done that please do that before you proceed further.&lt;br /&gt;
This is a sort of sequel to that post.&lt;br /&gt;
&lt;br /&gt;
Here I am making a GridView Selectable using &lt;span style=&quot;font-weight:bold;&quot;&gt;__doPostBack(...)&lt;/span&gt; function&lt;br /&gt;
&lt;br /&gt;
Suppose you have a GridView populated from the &lt;span style=&quot;font-weight:bold;&quot;&gt;Products&lt;/span&gt; table of the &lt;span style=&quot;font-weight:bold;&quot;&gt;NorthWind &lt;/span&gt;Database.&lt;br /&gt;
&lt;br /&gt;
So the ASPX looks like this&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;asp:GridView ID=&amp;quot;gvwProducts&amp;quot; runat=&amp;quot;server&amp;quot;
AutoGenerateColumns=&amp;quot;False&amp;quot; 
DataKeyNames=&amp;quot;ProductID&amp;quot;
DataSourceID=&amp;quot;sdsProducts&amp;quot;
OnRowDataBound=&amp;quot;gvwProducts_RowDataBound&amp;quot;&amp;gt;
&amp;lt;Columns&amp;gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Contact Name&amp;quot;&amp;gt;
&amp;lt;ItemTemplate&amp;gt;
&amp;lt;asp:Label ID=&amp;quot;lblProductName&amp;quot; runat=&amp;quot;server&amp;quot;
Text=&#39;&amp;lt;%#Eval(&amp;quot;ProductName&amp;quot;) %&amp;gt;&#39;&amp;gt;
&amp;lt;/asp:Label&amp;gt;
&amp;lt;/ItemTemplate&amp;gt;
&amp;lt;/asp:TemplateField&amp;gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Stock&amp;quot;&amp;gt;
&amp;lt;ItemTemplate&amp;gt;
&amp;lt;asp:Label ID=&amp;quot;lblunitsInStock&amp;quot; runat=&amp;quot;server&amp;quot;
Text=&#39;&amp;lt;%#Eval(&amp;quot;UnitsInStock&amp;quot;) %&amp;gt;&#39;&amp;gt;
&amp;lt;/asp:Label&amp;gt;
&amp;lt;/ItemTemplate&amp;gt;
&amp;lt;/asp:TemplateField&amp;gt;

&amp;lt;/Columns&amp;gt;
&amp;lt;SelectedRowStyle ForeColor=&amp;quot;Green&amp;quot; /&amp;gt;
&amp;lt;HeaderStyle BackColor=&amp;quot;#507CD1&amp;quot; Font-Bold=&amp;quot;True&amp;quot; ForeColor=&amp;quot;White&amp;quot; /&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&amp;lt;asp:SqlDataSource ID=&amp;quot;sdsProducts&amp;quot; runat=&amp;quot;server&amp;quot; 
ConnectionString=&amp;quot;&amp;lt;%$ ConnectionStrings:NorthwindConnectionString %&amp;gt;&amp;quot;
SelectCommand=&amp;quot;SELECT ProductID, ProductName, UnitsInStock FROM Products&amp;quot;
SelectCommandType=&amp;quot;Text&amp;quot;&amp;gt;
&amp;lt;/asp:SqlDataSource&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Now on &lt;span style=&quot;font-weight:bold;&quot;&gt;RowDataBound &lt;/span&gt;lets set the click attribute for selectable row.&lt;br /&gt;
You can also use RowCreated for this.&lt;br /&gt;
Note that we are also giving some fancy color changes in onmouseover and onmouseout events.&lt;br /&gt;
Discard them if you dont want color changes.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;protected void gvwProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Attributes[&quot;onmouseover&quot;] = &quot;this.style.color=&#39;DodgerBlue&#39;;this.style.cursor=&#39;hand&#39;;&quot;;
e.Row.Attributes[&quot;onmouseover&quot;] = &quot;javascript:return ChangeRowColor(&#39;m_over&#39;, this.style)&quot;;
//e.Row.Attributes[&quot;onmouseout&quot;] = &quot;this.style.color=&#39;Black&#39;;&quot;;
e.Row.Attributes[&quot;onmouseout&quot;] = &quot;javascript:return ChangeRowColor(&#39;m_out&#39;, this.style)&quot;;
e.Row.Attributes[&quot;onclick&quot;] = &quot;javascript:return __doPostBack(&#39;&quot; + gvwProducts.ClientID.Replace(&#39;_&#39;,&#39;$&#39;) + &quot;&#39;, &#39;Select$&quot; + e.Row.RowIndex + &quot;&#39;);&quot;;
e.Row.ToolTip = &quot;Click on the row to select it&quot;;
}
}

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Now register these postbacks for event validation at Page_Render to avoid&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using &lt;pages enableEventValidation=&quot;true&quot;/&gt; in configuration or &lt;%@ Page EnableEventValidation=&quot;true&quot; %&gt; in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation&lt;br /&gt;
&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;protected override void Render(HtmlTextWriter writer)
{
// .NET will refuse to accept &quot;unknown&quot; postbacks for security reasons. 
// Because of this we have to register all possible callbacks
// This must be done in Render, hence the override
for (int i = 0; i &lt; gvwProducts.Rows.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(new System.Web.UI.PostBackOptions(this.gvwProducts, &quot;Select$&quot; + i.ToString()));
}
// Do the standard rendering stuff
base.Render(writer);
}

&lt;/pre&gt;
Now compare the RowDataBound event to first example and you note some changes in
the way the row attributes for onmouseover and onmouseout are set.
(Check the commented lines in the RowDataBound event)

What we do here is to set a foreground color &quot;Green&quot; to the Selected Row and persists that color
onmouseout and onmouseover of the selected row.

Inorder to achieve that, place this javascript function on the page.

&lt;pre class=&quot;brush:javascript&quot;&gt;function ChangeRowColor(eventtype, rowStyle){
if(rowStyle.color.toLowerCase() != &quot;green&quot;){
if(eventtype == &quot;m_over&quot;){
rowStyle.color=&#39;DodgerBlue&#39;;
rowStyle.cursor=&#39;pointer&#39;;
}
else{
rowStyle.color=&#39;Black&#39;;
}
}
else{
rowStyle.cursor=&#39;default&#39;;
}
}

&lt;/pre&gt;Now some developers may get the 

&lt;blockquote&gt;
__doPostBack(...) object required 

&lt;/blockquote&gt;To circumvent this error place these on the ASPX Page

Two Hidden Fields

&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;input type =&amp;quot;hidden&amp;quot; name =&amp;quot;__EVENTTARGET&amp;quot; value =&amp;quot;&amp;quot; /&amp;gt;
&amp;lt;input type =&amp;quot;hidden&amp;quot; name =&amp;quot;__EVENTARGUMENT&amp;quot; value =&amp;quot;&amp;quot; /&amp;gt;

&lt;/pre&gt;Javascript Function


&lt;pre class=&quot;brush:javascript&quot;&gt;//&lt;![CDATA[
var theForm = document.forms[&#39;aspnetForm&#39;];
if (!theForm) {
theForm = (!document.aspnetForm)?document.forms[0] : document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]&gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Happy Coding!</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/1042968033133456316/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/10/selectable-gridviewrow-using-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1042968033133456316'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/1042968033133456316'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/10/selectable-gridviewrow-using-javascript.html' title='Selectable GridViewRow using Javascript'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-3659615639960610040</id><published>2009-10-19T21:12:00.009+05:30</published><updated>2013-11-05T19:12:22.291+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="gridview"/><category scheme="http://www.blogger.com/atom/ns#" term="selectable row"/><title type='text'>Selectable GridViewRow</title><content type='html'>I have always found using Select Button in GridView to select a row cumbersome.&lt;br /&gt;
It would be always better to click anywhere on the GridViewRow and Select the row.&lt;br /&gt;
&lt;br /&gt;
Here&#39;s a snippet on how to do it.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold;&quot;&gt;On RowDataBound&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;protected void gvwProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes[&quot;onmouseover&quot;] = &quot;this.style.color=&#39;DodgerBlue&#39;;this.style.cursor=&#39;hand&#39;;&quot;;
e.Row.Attributes[&quot;onmouseout&quot;] = &quot;this.style.color=&#39;Black&#39;;&quot;;
e.Row.Attributes[&quot;onclick&quot;] = ClientScript.GetPostBackClientHyperlink(this.gvwProducts, &quot;Select$&quot; + e.Row.RowIndex);
e.Row.ToolTip = &quot;Click on the row to select it&quot;;
}
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Now if we run the code and click on the row we will get this error&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
System.ArgumentException: Invalid postback or callback argument.  &lt;br /&gt;
Event validation is enabled using &lt;pages enableEventValidation=&quot;true&quot;/&gt; in configuration &lt;br /&gt;
or &lt;%@ Page EnableEventValidation=&quot;true&quot; %&gt; in a page.  &lt;br /&gt;
For security purposes, this feature verifies that arguments to postback or callback events originate &lt;br /&gt;
from the server control that originally rendered them.  &lt;br /&gt;
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method &lt;br /&gt;
in order to register the postback or callback data for validation.&lt;br /&gt;
&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
To avoid this we override the Page&#39;s Render Event.&lt;br /&gt;
Inside that event we register an event reference for validation on each GridViewRow&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold;&quot;&gt;The overridden Page Render&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;protected override void Render(HtmlTextWriter writer)
{
// .NET will refuse to accept &quot;unknown&quot; postbacks for security reasons. 
// Because of this we have to register all possible callbacks
// This must be done in Render, hence the override
for (int i = 0; i &lt; gvwProducts.Rows.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(new System.Web.UI.PostBackOptions(gvwProducts, &quot;Select$&quot; + i.ToString()));
}
// Do the standard rendering stuff
base.Render(writer);
}
&lt;/pre&gt;


Here I am posting the ASPX markup of the GridView also

&lt;span style=&quot;font-weight:bold;&quot;&gt;The ASPX&lt;/span&gt;

&lt;pre class=&quot;brush:xhtml&quot;&gt;&amp;lt;asp:GridView ID=&amp;quot;gvwProducts&amp;quot; runat=&amp;quot;server&amp;quot;
AutoGenerateColumns=&amp;quot;False&amp;quot; 
DataKeyNames=&amp;quot;ProductID&amp;quot;
DataSourceID=&amp;quot;sdsProducts&amp;quot;
OnRowDataBound=&amp;quot;gvwProducts_RowDataBound&amp;quot;&amp;gt;
&amp;lt;Columns&amp;gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Contact Name&amp;quot;&amp;gt;
&amp;lt;ItemTemplate&amp;gt;
&amp;lt;asp:Label ID=&amp;quot;lblProductName&amp;quot; runat=&amp;quot;server&amp;quot;
Text=&#39;&amp;lt;%#Eval(&amp;quot;ProductName&amp;quot;) %&amp;gt;&#39;&amp;gt;
&amp;lt;/asp:Label&amp;gt;
&amp;lt;/ItemTemplate&amp;gt;
&amp;lt;/asp:TemplateField&amp;gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Stock&amp;quot;&amp;gt;
&amp;lt;ItemTemplate&amp;gt;
&amp;lt;asp:Label ID=&amp;quot;lblunitsInStock&amp;quot; runat=&amp;quot;server&amp;quot;
Text=&#39;&amp;lt;%#Eval(&amp;quot;UnitsInStock&amp;quot;) %&amp;gt;&#39;&amp;gt;
&amp;lt;/asp:Label&amp;gt;
&amp;lt;/ItemTemplate&amp;gt;
&amp;lt;/asp:TemplateField&amp;gt;

&amp;lt;/Columns&amp;gt;
&amp;lt;SelectedRowStyle BackColor=&amp;quot;#D1DDF1&amp;quot; Font-Bold=&amp;quot;True&amp;quot; ForeColor=&amp;quot;#333333&amp;quot; /&amp;gt;
&amp;lt;HeaderStyle BackColor=&amp;quot;#507CD1&amp;quot; Font-Bold=&amp;quot;True&amp;quot; ForeColor=&amp;quot;White&amp;quot; /&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&amp;lt;asp:SqlDataSource ID=&amp;quot;sdsProducts&amp;quot; runat=&amp;quot;server&amp;quot; 
ConnectionString=&amp;quot;&amp;lt;%$ ConnectionStrings:NorthwindConnectionString %&amp;gt;&amp;quot;
SelectCommand=&amp;quot;SELECT ProductID, ProductName, UnitsInStock FROM Products&amp;quot;
SelectCommandType=&amp;quot;Text&amp;quot;&amp;gt;
&amp;lt;/asp:SqlDataSource&amp;gt;
&lt;/pre&gt;


Just paste the last xml snippet on ASPX and first two code snippets on cs page.
Change the connectionstring to the one you have and run the code to see the result.

We are not finished yet
From time to time, some developers will experience the &quot;__doPostBack object expected&quot; error in the page Postback.
This occurs at the second time we try to fire the row click event

The Reason

&lt;blockquote&gt;

We add onclick feature for GridView in the RowDataBound event using &lt;span style=&quot;font-weight:bold;&quot;&gt;ClientScript.GetPostBackClientHyperlink(...)&lt;/span&gt;
But RowDataBound event gets fired only the first time(at !IsPostBack). 
This creates the &lt;span style=&quot;font-weight:bold;&quot;&gt;__doPostBack()&lt;/span&gt; function when the page is first time loaded.

Now from the second time onwards(at IsPostBack) RowDataBound doesn&#39;t get fired.
So no &lt;span style=&quot;font-weight:bold;&quot;&gt;ClientScript.GetPostBackClientHyperlink(...)&lt;/span&gt; is called and no  __doPostBack() function is created

&lt;/blockquote&gt;

The solution. Just place this line under Page_Load event

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.GetPostBackClientHyperlink(this, &quot;&quot;);
}

&lt;/pre&gt;

Happy Coding!

For Better understanding please read &lt;a href=&quot;http://fun2code.blogspot.com/2009/10/selectable-gridviewrow-using-javascript.html&quot;&gt;Selectable GridViewRow using JavaScript&lt;/a&gt;

Points to Note:
1. I have given a &lt;span style=&quot;font-weight:bold;&quot;&gt;SelectedRowStyle &lt;/span&gt;to show that the row is actually selected by giving it a different color.
2. The table used is &lt;span style=&quot;font-weight:bold;&quot;&gt;Products &lt;/span&gt;from Microsoft&#39;s Sample DataBase &lt;span style=&quot;font-weight:bold;&quot;&gt;Northwind&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/3659615639960610040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/10/gridviewrow-as-select-button.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3659615639960610040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3659615639960610040'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/10/gridviewrow-as-select-button.html' title='Selectable GridViewRow'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-6208628106492245528</id><published>2009-08-27T18:12:00.004+05:30</published><updated>2009-10-22T16:57:13.569+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="gridview sorting paging"/><title type='text'>Sorting and Paging a GridView</title><content type='html'>Sorting can be tricky. But maintaining the sort order on paging is trickier&lt;br /&gt;Here is a sample code for that&lt;br /&gt;&lt;br /&gt;DataBase used is Northwind.&lt;br /&gt;Table used is Products.&lt;br /&gt;&lt;br /&gt;The ASPX&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;xhtml&quot;&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;asp:GridView ID=&amp;quot;GridView1&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                AllowPaging=&amp;quot;true&amp;quot;&lt;br /&gt;                AllowSorting=&amp;quot;true&amp;quot;&lt;br /&gt;                AutoGenerateColumns=&amp;quot;false&amp;quot;&lt;br /&gt;                DataKeyNames=&amp;quot;ProductID&amp;quot;&lt;br /&gt;                PageSize=&amp;quot;10&amp;quot;&lt;br /&gt;                OnPageIndexChanging=&amp;quot;GridView1_PageIndexChanging&amp;quot;&lt;br /&gt;                OnSorting=&amp;quot;GridView1_Sorting&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;Columns&amp;gt;&lt;br /&gt;        &amp;lt;asp:TemplateField HeaderText=&amp;quot;ID&amp;quot; SortExpression=&amp;quot;ProductID&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                &amp;lt;asp:Label ID=&amp;quot;lblProductID&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%#Eval(&amp;quot;ProductID&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;            &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;        &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;        &amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot; SortExpression=&amp;quot;ProductName&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                &amp;lt;asp:Label ID=&amp;quot;lblProductName&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%#Eval(&amp;quot;ProductName&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;            &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;        &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;        &amp;lt;asp:TemplateField HeaderText=&amp;quot;Stock&amp;quot; SortExpression=&amp;quot;UnitsInStock&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                &amp;lt;asp:Label ID=&amp;quot;lblUnitsInStock&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%#Eval(&amp;quot;UnitsInStock&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;            &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;        &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;    &amp;lt;/Columns&amp;gt;&lt;br /&gt;&amp;lt;/asp:GridView&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The ASPX.CS&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;c-sharp&quot;&gt;&lt;br /&gt;&lt;br /&gt;private const string ASCENDING = &quot; ASC&quot;;&lt;br /&gt;private const string DESCENDING = &quot; DESC&quot;;&lt;br /&gt;static private DataView dvProducts;&lt;br /&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    if (!IsPostBack)&lt;br /&gt;    {&lt;br /&gt;        dvProducts = new DataView(GetProductsFromDataTable());&lt;br /&gt;        BindGridView();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)&lt;br /&gt;{&lt;br /&gt;    string sortExpression = e.SortExpression;&lt;br /&gt;&lt;br /&gt;    if (GridViewSortDirection == SortDirection.Ascending)&lt;br /&gt;    {&lt;br /&gt;        GridViewSortDirection = SortDirection.Descending;&lt;br /&gt;        dvProducts.Sort = sortExpression + DESCENDING;&lt;br /&gt;        BindGridView();&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        GridViewSortDirection = SortDirection.Ascending;&lt;br /&gt;        dvProducts.Sort = sortExpression + ASCENDING;&lt;br /&gt;        BindGridView();&lt;br /&gt;    }   &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)&lt;br /&gt;{&lt;br /&gt;    GridView1.PageIndex = e.NewPageIndex;&lt;br /&gt;    BindGridView();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void BindGridView()&lt;br /&gt;{&lt;br /&gt;    GridView1.DataSource = dvProducts;&lt;br /&gt;    GridView1.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public SortDirection GridViewSortDirection&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        if (ViewState[&quot;sortDirection&quot;] == null)&lt;br /&gt;            ViewState[&quot;sortDirection&quot;] = SortDirection.Ascending;&lt;br /&gt;&lt;br /&gt;        return (SortDirection)ViewState[&quot;sortDirection&quot;];&lt;br /&gt;    }&lt;br /&gt;    set { ViewState[&quot;sortDirection&quot;] = value; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private DataTable GetProductsFromDataTable()&lt;br /&gt;{&lt;br /&gt;    DataTable dtProductsTemp = new DataTable();&lt;br /&gt;    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ConnectionString))&lt;br /&gt;    {&lt;br /&gt;        using (SqlDataAdapter adapProducts = new SqlDataAdapter(&quot;Select * from Products&quot;, conn))&lt;br /&gt;        {&lt;br /&gt;            adapProducts.Fill(dtProductsTemp);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return dtProductsTemp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Extra namespace used&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;c-sharp&quot;&gt;&lt;br /&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/6208628106492245528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/08/sorting-and-paging-gridview.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6208628106492245528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6208628106492245528'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/08/sorting-and-paging-gridview.html' title='Sorting and Paging a GridView'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-141350710968131361</id><published>2009-07-18T16:51:00.012+05:30</published><updated>2009-10-05T17:01:41.160+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="repeater paging pageddatasource"/><title type='text'>Paging in Repeater</title><content type='html'>Heres a sample on how to implement Paging on a Repeater.&lt;br /&gt;&lt;br /&gt;The Output&lt;br /&gt;&lt;br /&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 478px; height: 328px;&quot; src=&quot;http://sites.google.com/site/fun2code/Home/repeaterpaging.jpg&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;&lt;br /&gt;The ASPX&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;xhtml&quot;&gt;&lt;br /&gt; &amp;lt;asp:Repeater ID=&amp;quot;rptrProducts&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;HeaderTemplate&amp;gt;&lt;br /&gt;            &amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;tr style=&amp;quot;background-color:Gray;&amp;quot;&amp;gt;&lt;br /&gt;                    &amp;lt;td&amp;gt;&lt;br /&gt;                        Product Name&lt;br /&gt;                    &amp;lt;/td&amp;gt;&lt;br /&gt;                    &amp;lt;td&amp;gt;&lt;br /&gt;                        Quantity Per Unit&lt;br /&gt;                    &amp;lt;/td&amp;gt;&lt;br /&gt;                    &amp;lt;td&amp;gt;&lt;br /&gt;                        Units On Order&lt;br /&gt;                    &amp;lt;/td&amp;gt;&lt;br /&gt;                    &amp;lt;td&amp;gt;&lt;br /&gt;                        Discontinued&lt;br /&gt;                    &amp;lt;/td&amp;gt;&lt;br /&gt;                &amp;lt;/tr&amp;gt;&lt;br /&gt;        &amp;lt;/HeaderTemplate&amp;gt;&lt;br /&gt;        &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;tr&amp;gt;&lt;br /&gt;                &amp;lt;td&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblProductName&amp;quot; runat=&amp;quot;server&amp;quot; Text=&#39;&amp;lt;%#Eval(&amp;quot;ProductName&amp;quot;) %&amp;gt;&#39;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/td&amp;gt;&lt;br /&gt;                &amp;lt;td&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblQuantityPerUnit&amp;quot; runat=&amp;quot;server&amp;quot; Text=&#39;&amp;lt;%#Eval(&amp;quot;QuantityPerUnit&amp;quot;) %&amp;gt;&#39;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/td&amp;gt;&lt;br /&gt;                &amp;lt;td&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblUnitsOnOrder&amp;quot; runat=&amp;quot;server&amp;quot; Text=&#39;&amp;lt;%#Eval(&amp;quot;UnitsOnOrder&amp;quot;) %&amp;gt;&#39;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/td&amp;gt;&lt;br /&gt;                &amp;lt;td&amp;gt;&lt;br /&gt;                    &amp;lt;asp:CheckBox ID=&amp;quot;chkDiscontinued&amp;quot; runat=&amp;quot;server&amp;quot; Checked=&#39;&amp;lt;%#Eval(&amp;quot;Discontinued&amp;quot;) %&amp;gt;&#39;&lt;br /&gt;                        Enabled=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;                &amp;lt;/td&amp;gt;&lt;br /&gt;            &amp;lt;/tr&amp;gt;&lt;br /&gt;        &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;    &amp;lt;/asp:Repeater&amp;gt;&lt;br /&gt;    &amp;lt;tr align=&amp;quot;right&amp;quot; style=&amp;quot;background-color:Gray;&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;td colspan=&amp;quot;4&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;asp:LinkButton ID=&amp;quot;lnkFirst&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                            ForeColor=&amp;quot;Black&amp;quot;&lt;br /&gt;                            Text=&amp;quot;First&amp;quot; onclick=&amp;quot;lnkFirst_Click&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:LinkButton&amp;gt;&amp;amp;nbsp;&lt;br /&gt;            &amp;lt;asp:LinkButton ID=&amp;quot;lnkPrevious&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                            ForeColor=&amp;quot;Black&amp;quot;&lt;br /&gt;                            Text=&amp;quot;Previous&amp;quot; &lt;br /&gt;                            OnClick=&amp;quot;lnkPrevious_Click&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:LinkButton&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;            Now Showing Page&amp;amp;nbsp;&lt;br /&gt;            &amp;lt;asp:DropDownList ID=&amp;quot;ddlpageNumbers&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                                AutoPostBack=&amp;quot;true&amp;quot; &lt;br /&gt;                                OnSelectedIndexChanged=&amp;quot;ddlpageNumbers_SelectedIndexChanged&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:DropDownList&amp;gt;&amp;amp;nbsp;of&amp;amp;nbsp;&lt;br /&gt;            &amp;lt;asp:Label ID=&amp;quot;lblTotalPages&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:Label&amp;gt;&amp;amp;nbsp;Pages.&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;            &amp;lt;asp:LinkButton ID=&amp;quot;lnkNext&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                            ForeColor=&amp;quot;Black&amp;quot;&lt;br /&gt;                            Text=&amp;quot;Next&amp;quot; onclick=&amp;quot;lnkNext_Click&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:LinkButton&amp;gt;&amp;amp;nbsp;&lt;br /&gt;            &amp;lt;asp:LinkButton ID=&amp;quot;lnkLast&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                            ForeColor=&amp;quot;Black&amp;quot;&lt;br /&gt;                            Text=&amp;quot;Last&amp;quot; onclick=&amp;quot;lnkLast_Click&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;/asp:LinkButton&amp;gt;&lt;br /&gt;        &amp;lt;/td&amp;gt;&lt;br /&gt;    &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The ASPX.CS&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;csharp&quot;&gt;&lt;br /&gt;public partial class Repeaters_Paging : System.Web.UI.Page&lt;br /&gt;{&lt;br /&gt;    #region &quot;Local Variables and Declarations&quot;&lt;br /&gt;&lt;br /&gt;    private const bool blnAllowPaging = true;&lt;br /&gt;    private const int iPageSize = 10;&lt;br /&gt;&lt;br /&gt;    private static PagedDataSource pgdProducts = new PagedDataSource();&lt;br /&gt;&lt;br /&gt;    #endregion&lt;br /&gt;        &lt;br /&gt;    #region &quot;Page Load Event&quot;&lt;br /&gt;&lt;br /&gt;    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        if (!IsPostBack)&lt;br /&gt;        {&lt;br /&gt;            BindRepeater();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    #endregion&lt;br /&gt;&lt;br /&gt;    #region &quot;Footer LinkButtons&quot;&lt;br /&gt;&lt;br /&gt;    protected void lnkFirst_Click(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        ThisPageNumber = 0;&lt;br /&gt;        BindRepeater();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected void lnkPrevious_Click(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        --ThisPageNumber;&lt;br /&gt;        BindRepeater();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected void lnkNext_Click(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        ++ThisPageNumber;&lt;br /&gt;        BindRepeater();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected void lnkLast_Click(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        ThisPageNumber = pgdProducts.PageCount - 1;&lt;br /&gt;        BindRepeater();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    #endregion&lt;br /&gt;&lt;br /&gt;    #region &quot;Footer DropDownList&quot;&lt;br /&gt;&lt;br /&gt;    protected void ddlpageNumbers_SelectedIndexChanged(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        ThisPageNumber = ddlpageNumbers.SelectedIndex;&lt;br /&gt;        BindRepeater();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    #endregion&lt;br /&gt;&lt;br /&gt;    #region &quot;Custom Functions&quot;&lt;br /&gt;&lt;br /&gt;    private void BindRepeater()&lt;br /&gt;    {&lt;br /&gt;        pgdProducts.AllowPaging = blnAllowPaging;&lt;br /&gt;        pgdProducts.PageSize = iPageSize;&lt;br /&gt;        pgdProducts.DataSource = GetProductsDataView();&lt;br /&gt;&lt;br /&gt;        pgdProducts.CurrentPageIndex = ThisPageNumber;&lt;br /&gt;        lblTotalPages.Text = pgdProducts.PageCount.ToString();&lt;br /&gt;        FillPagesDropDownList(pgdProducts.PageCount);&lt;br /&gt;&lt;br /&gt;        lnkFirst.Enabled = !pgdProducts.IsFirstPage;&lt;br /&gt;        lnkPrevious.Enabled = !pgdProducts.IsFirstPage;&lt;br /&gt;        lnkNext.Enabled = !pgdProducts.IsLastPage;&lt;br /&gt;        lnkLast.Enabled = !pgdProducts.IsLastPage;&lt;br /&gt;        ddlpageNumbers.SelectedIndex = pgdProducts.CurrentPageIndex; &lt;br /&gt;&lt;br /&gt;        rptrProducts.DataSource = pgdProducts;&lt;br /&gt;        rptrProducts.DataBind();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private DataView GetProductsDataView()&lt;br /&gt;    {&lt;br /&gt;        DataTable dtProductsTemp = new DataTable();&lt;br /&gt;        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ConnectionString))&lt;br /&gt;        {&lt;br /&gt;            using (SqlDataAdapter adapProducts = new SqlDataAdapter(&quot;Select * from Products&quot;, conn))&lt;br /&gt;            {&lt;br /&gt;                adapProducts.Fill(dtProductsTemp);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return dtProductsTemp.DefaultView;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void FillPagesDropDownList(int iTotalPages)&lt;br /&gt;    {&lt;br /&gt;        ddlpageNumbers.Items.Clear();&lt;br /&gt;        for (int i = 1; i &lt;= iTotalPages; i++)&lt;br /&gt;        {&lt;br /&gt;            ddlpageNumbers.Items.Add(new ListItem(i.ToString(), i.ToString()));&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    #endregion&lt;br /&gt;&lt;br /&gt;    #region &quot;Properties&quot;&lt;br /&gt;&lt;br /&gt;    private int ThisPageNumber&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            object obj = ViewState[&quot;ThisPageNumber&quot;];&lt;br /&gt;            return (obj == null) ? 0 : (int)obj;&lt;br /&gt;        }&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            this.ViewState[&quot;ThisPageNumber&quot;] = value;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    #endregion&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Extra Namespace used&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;csharp&quot;&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;P.S: DataBase used is Northwind</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/141350710968131361/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/07/heres-sample-on-how-to-implement-paging.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/141350710968131361'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/141350710968131361'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/07/heres-sample-on-how-to-implement-paging.html' title='Paging in Repeater'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-2339422527752687460</id><published>2009-05-15T23:34:00.018+05:30</published><updated>2009-10-05T16:58:42.296+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="subtotal gridview"/><title type='text'>SubTotals in GridView</title><content type='html'>To display Sub Totals in a GridView is a situation that arise frequently.&lt;br /&gt;&lt;br /&gt;Here&#39;s a sample on how to do it using GridView.&lt;br /&gt;&lt;br /&gt;First we need a table TBL_Patients. Here&#39;s the structure&lt;br /&gt;&lt;br /&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 338px; height 142px;&quot; src=&quot;http://sites.google.com/site/fun2code/Home/tablestructure.JPG&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Now lets populate some sample values to it.&lt;br /&gt;&lt;br /&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 502px; height: 212px;&quot; src=&quot;http://sites.google.com/site/fun2code/Home/tablevalues.JPG&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;&lt;br /&gt;The ASPX&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;xhtml&quot;&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;asp:GridView ID=&amp;quot;gvwPatientBill&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                    AutoGenerateColumns=&amp;quot;false&amp;quot;&lt;br /&gt;                    DataKeyNames=&amp;quot;BillID&amp;quot;&lt;br /&gt;                    GridLines=&amp;quot;Both&amp;quot;&lt;br /&gt;                    OnRowDataBound=&amp;quot;gvwPatientBill_RowDataBound&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;Columns&amp;gt;&lt;br /&gt;            &amp;lt;asp:TemplateField HeaderText=&amp;quot;Bill ID&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblBillID&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%# Eval(&amp;quot;BillID&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                    &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;            &amp;lt;asp:TemplateField HeaderText=&amp;quot;Patient ID&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblPatientID&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%# Eval(&amp;quot;PatientID&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                    &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;            &amp;lt;asp:TemplateField HeaderText=&amp;quot;Patient Name&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblPatientName&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%# Eval(&amp;quot;PatientName&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                    &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;            &amp;lt;asp:TemplateField HeaderText=&amp;quot;Patient Ward&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblPatientWard&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%# Eval(&amp;quot;PatientWard&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                    &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;            &amp;lt;asp:TemplateField HeaderText=&amp;quot;Bill Amount&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;                    &amp;lt;asp:Label ID=&amp;quot;lblBillAmount&amp;quot; runat=&amp;quot;server&amp;quot;&lt;br /&gt;                            Text=&#39;&amp;lt;%# Eval(&amp;quot;BillAmount&amp;quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;                    &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;                &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;            &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;        &amp;lt;/Columns&amp;gt;&lt;br /&gt;        &amp;lt;HeaderStyle BackColor=&amp;quot;Black&amp;quot; ForeColor=&amp;quot;White&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/asp:GridView&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The ASPX.CS&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;csharp&quot;&gt;&lt;br /&gt;int iPatientIDCount = 0;&lt;br /&gt;int iRowsCount = 0;&lt;br /&gt;int iAddBills = 0;&lt;br /&gt;string sPatientID = &quot;&quot;;&lt;br /&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    if (!IsPostBack)&lt;br /&gt;    {&lt;br /&gt;        BindGridView();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void BindGridView()&lt;br /&gt;{&lt;br /&gt;    DataTable dtPatientBills = new DataTable();&lt;br /&gt;    string strSelectCommand = &quot;SELECT * FROM TBL_PatientBills ORDER BY PatientID&quot;;&lt;br /&gt;    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;MyConnectionString&quot;].ConnectionString))&lt;br /&gt;    {&lt;br /&gt;        using (SqlDataAdapter adapPatientBills = new SqlDataAdapter(strSelectCommand, sqlConn))&lt;br /&gt;        {&lt;br /&gt;            adapPatientBills.Fill(dtPatientBills);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    iRowsCount = dtPatientBills.Rows.Count - 1;&lt;br /&gt;    gvwPatientBill.DataSource = dtPatientBills;&lt;br /&gt;    gvwPatientBill.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void gvwPatientBill_RowDataBound(object sender, GridViewRowEventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    if (e.Row.RowType == DataControlRowType.DataRow)&lt;br /&gt;    {&lt;br /&gt;        string sName = &quot;&quot;;&lt;br /&gt;        if (e.Row.RowIndex == 0)&lt;br /&gt;        {&lt;br /&gt;            sPatientID = ((Label)e.Row.FindControl(&quot;lblPatientID&quot;)).Text;&lt;br /&gt;            iAddBills = Convert.ToInt32(((Label)e.Row.FindControl(&quot;lblBillAmount&quot;)).Text);&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            sName = ((Label)gvwPatientBill.Rows[e.Row.RowIndex - 1].FindControl(&quot;lblPatientName&quot;)).Text;&lt;br /&gt;            iPatientIDCount = iPatientIDCount + 1;&lt;br /&gt;            if (sPatientID == ((Label)e.Row.FindControl(&quot;lblPatientID&quot;)).Text)&lt;br /&gt;            {&lt;br /&gt;                iAddBills += Convert.ToInt32(((Label)e.Row.FindControl(&quot;lblBillAmount&quot;)).Text);&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                sPatientID = ((Label)e.Row.FindControl(&quot;lblPatientID&quot;)).Text;&lt;br /&gt;                Table tblTemp = (Table)this.gvwPatientBill.Controls[0];&lt;br /&gt;                int intIndex = tblTemp.Rows.GetRowIndex(e.Row);&lt;br /&gt;                GridViewRow gvrSubTotal = CreateGridViewRow(&lt;br /&gt;                        intIndex,&lt;br /&gt;                        &quot;#8FD8D8&quot;,&lt;br /&gt;                        gvwPatientBill.Columns.Count,&lt;br /&gt;                        &quot;Total for &quot; + sName + &quot; in &quot; + iPatientIDCount + &quot; Entries: &quot; + iAddBills,&lt;br /&gt;                        20);&lt;br /&gt;                tblTemp.Controls.AddAt(intIndex, gvrSubTotal);&lt;br /&gt;                iPatientIDCount = 0;&lt;br /&gt;                iAddBills = Convert.ToInt32(((Label)e.Row.FindControl(&quot;lblBillAmount&quot;)).Text);&lt;br /&gt;            }&lt;br /&gt;            if (iRowsCount == e.Row.RowIndex)&lt;br /&gt;            {&lt;br /&gt;                sName = ((Label)e.Row.FindControl(&quot;lblPatientName&quot;)).Text;&lt;br /&gt;                Table tblTemp = (Table)this.gvwPatientBill.Controls[0];&lt;br /&gt;                int intIndex = tblTemp.Rows.GetRowIndex(e.Row) + 1;&lt;br /&gt;&lt;br /&gt;                GridViewRow gvrLast = CreateGridViewRow(&lt;br /&gt;                        intIndex,&lt;br /&gt;                        &quot;#8FD8D8&quot;,&lt;br /&gt;                        gvwPatientBill.Columns.Count,&lt;br /&gt;                        &quot;Total for &quot; + sName + &quot; in &quot; + iPatientIDCount + &quot; Entries: &quot; + iAddBills,&lt;br /&gt;                        20);&lt;br /&gt;                tblTemp.Controls.AddAt(intIndex, gvrLast);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private GridViewRow CreateGridViewRow(int iCurrentIndex, string sTableBackColor,int iTableColumnSpan, string sTableText, int iTableHeight)&lt;br /&gt;{&lt;br /&gt;    GridViewRow gvrTemp = new GridViewRow(iCurrentIndex, iCurrentIndex, DataControlRowType.Separator, DataControlRowState.Normal);&lt;br /&gt;    TableCell cellTemp = new TableCell();&lt;br /&gt;    cellTemp.BackColor = System.Drawing.ColorTranslator.FromHtml(&quot;#8FD8D8&quot;);&lt;br /&gt;    cellTemp.Font.Bold = true;&lt;br /&gt;    cellTemp.ColumnSpan = iTableColumnSpan;&lt;br /&gt;    cellTemp.HorizontalAlign = HorizontalAlign.Left;&lt;br /&gt;    cellTemp.Text = sTableText;&lt;br /&gt;    cellTemp.Height = Unit.Pixel(iTableHeight);&lt;br /&gt;    gvrTemp.Cells.Add(cellTemp);&lt;br /&gt;    return gvrTemp;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the final result looks like this.&lt;br /&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 387px; height: 295px;&quot; src=&quot;http://sites.google.com/site/fun2code/Home/billdetails.jpg&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;&lt;br /&gt;NB: Its a no-no if you wanna implement Paging and Sorting here. For that, we have to use the wonderful third party &lt;br /&gt;GridView controls like Teleriks RadGrid and all</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/2339422527752687460/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/05/subtotal-in-gridview.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2339422527752687460'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2339422527752687460'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/05/subtotal-in-gridview.html' title='SubTotals in GridView'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-2927929036525476636</id><published>2009-03-25T13:25:00.002+05:30</published><updated>2009-10-05T17:02:00.431+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="paging and filtering gridview"/><title type='text'>Paging a Filtered GridView</title><content type='html'>&lt;blockquote&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;In many forums there are people struggling with filtering and paging in gridview.&lt;br /&gt;When separated, both filtering and paging works smoothly.&lt;br /&gt;But when trying to change the page index of a filtered result set, all hell will break loose.&lt;br /&gt;Mostly a new page with the unfiltered result set comes into play.&lt;/span&gt; &lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Here&#39;s a sample on how to do it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The ASPX&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;xhtml&quot;&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;table&amp;gt;&lt;br /&gt;    &amp;lt;tr&amp;gt;&lt;br /&gt;        &amp;lt;td&amp;gt;&lt;br /&gt;            Search By Product Name:&lt;br /&gt;        &amp;lt;/td&amp;gt;&lt;br /&gt;        &amp;lt;td&amp;gt;&lt;br /&gt;            &amp;lt;asp:TextBox ID=&amp;quot;txtProductName&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&lt;br /&gt;        &amp;lt;/td&amp;gt;&lt;br /&gt;        &amp;lt;td&amp;gt;&lt;br /&gt;            &amp;lt;asp:Button ID=&amp;quot;btnBindGridView&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                        Text=&amp;quot;Populate Grid&amp;quot; onclick=&amp;quot;btnBindGridView_Click&amp;quot; /&amp;gt;&lt;br /&gt;        &amp;lt;/td&amp;gt;&lt;br /&gt;    &amp;lt;/tr&amp;gt;&lt;br /&gt;    &amp;lt;tr&amp;gt;&lt;br /&gt;        &amp;lt;td colspan=&amp;quot;3&amp;quot;&amp;gt;&lt;br /&gt;            &amp;lt;asp:GridView ID=&amp;quot;gvwProducts&amp;quot; runat=&amp;quot;server&amp;quot; &lt;br /&gt;                            AllowPaging=&amp;quot;true&amp;quot;&lt;br /&gt;                            AutoGenerateColumns=&amp;quot;false&amp;quot;&lt;br /&gt;                            PageSize=&amp;quot;3&amp;quot; &lt;br /&gt;                            OnPageIndexChanging=&amp;quot;gvwProducts_PageIndexChanging&amp;quot;&amp;gt;&lt;br /&gt;                &amp;lt;Columns&amp;gt;&lt;br /&gt;                    &amp;lt;asp:BoundField DataField=&amp;quot;ProductName&amp;quot; HeaderText=&amp;quot;Product Name&amp;quot;/&amp;gt;&lt;br /&gt;                    &amp;lt;asp:BoundField DataField=&amp;quot;QuantityPerUnit&amp;quot; HeaderText=&amp;quot;Quantity&amp;quot;/&amp;gt;&lt;br /&gt;                    &amp;lt;asp:BoundField DataField=&amp;quot;UnitPrice&amp;quot; HeaderText=&amp;quot;Price&amp;quot;/&amp;gt;&lt;br /&gt;                &amp;lt;/Columns&amp;gt;&lt;br /&gt;            &amp;lt;/asp:GridView&amp;gt;&lt;br /&gt;        &amp;lt;/td&amp;gt;&lt;br /&gt;    &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The ASPX.CS&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;csharp&quot;&gt;&lt;br /&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    if (!IsPostBack)&lt;br /&gt;    {&lt;br /&gt;        BindGridView();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void btnBindGridView_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    BindGridView();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void gvwProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)&lt;br /&gt;{&lt;br /&gt;    gvwProducts.PageIndex = e.NewPageIndex;&lt;br /&gt;    BindGridView();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void BindGridView()&lt;br /&gt;{&lt;br /&gt;    DataTable dtProducts = new DataTable();&lt;br /&gt;    string strSelectCommand = &quot;SELECT * FROM Products&quot; &lt;br /&gt;            + ((txtProductName.Text.Trim() == &quot;&quot;) ? &quot;&quot; : (&quot; WHERE ProductName LIKE &#39;&quot; + txtProductName.Text.Trim() + &quot;%&#39;&quot;));&lt;br /&gt;    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ConnectionString))&lt;br /&gt;    {&lt;br /&gt;        using (SqlDataAdapter adapProducts = new SqlDataAdapter(strSelectCommand, sqlConn))&lt;br /&gt;        {&lt;br /&gt;            adapProducts.Fill(dtProducts);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    gvwProducts.DataSource = dtProducts;&lt;br /&gt;    gvwProducts.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;DataBase used --&gt; Northwind&lt;br /&gt;Extra Namespace used --&gt;System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;Hope this helps!</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/2927929036525476636/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/03/paging-filtered-gridview.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2927929036525476636'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/2927929036525476636'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/03/paging-filtered-gridview.html' title='Paging a Filtered GridView'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-3302829138723954690</id><published>2009-03-16T11:06:00.007+05:30</published><updated>2009-10-05T17:02:17.029+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="gridview filtering datalist filter alphabet"/><title type='text'>Filtered GridView using DataList</title><content type='html'>Many times the requirement comes like this.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;need to show  the first letter of products as a link outside grid and when the user clicks on it the grid will be showing the result with that particular product only.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Can be easily done using GridView and DataList.&lt;br /&gt;Database used is Northwind.&lt;br /&gt;&lt;br /&gt;The ASPX&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;xhtml&quot;&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;asp:DataList ID=&quot;dlstProducts&quot; runat=&quot;server&quot;&lt;br /&gt;               RepeatDirection=&quot;Horizontal&quot;&amp;gt;&lt;br /&gt;   &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;       &amp;lt;asp:LinkButton ID=&quot;lnkFirstLetters&quot; runat=&quot;server&quot;&lt;br /&gt;                       Text=&#39;&amp;lt;%# Bind(&quot;ProductNameFirstLetter&quot;) %&amp;gt;&#39;&lt;br /&gt;           onclick=&quot;lnkFirstLetters_Click&quot;&amp;gt;&lt;br /&gt;       &amp;lt;/asp:LinkButton&amp;gt;&lt;br /&gt;   &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;&amp;lt;/asp:DataList&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;asp:GridView ID=&quot;gvwProducts&quot; runat=&quot;server&quot;&lt;br /&gt;               AutoGenerateColumns=&quot;false&quot;&lt;br /&gt;               DataKeyNames=&quot;ProductID&quot;&amp;gt;&lt;br /&gt;   &amp;lt;Columns&amp;gt;&lt;br /&gt;       &amp;lt;asp:TemplateField HeaderText=&quot;Product Name&quot;&amp;gt;&lt;br /&gt;           &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;               &amp;lt;asp:Label ID=&quot;lblProductName&quot; runat=&quot;server&quot; Text=&#39;&amp;lt;%# Bind(&quot;ProductName&quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;               &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;           &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;       &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;       &amp;lt;asp:TemplateField HeaderText=&quot;Quantity Per Unit&quot;&amp;gt;&lt;br /&gt;           &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;               &amp;lt;asp:Label ID=&quot;lblQuantityPerUnit&quot; runat=&quot;server&quot; Text=&#39;&amp;lt;%# Bind(&quot;QuantityPerUnit&quot;) %&amp;gt;&#39;&amp;gt;&lt;br /&gt;               &amp;lt;/asp:Label&amp;gt;&lt;br /&gt;           &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;       &amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;   &amp;lt;/Columns&amp;gt;&lt;br /&gt;&amp;lt;/asp:GridView&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The ASPX.CS&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;csharp&quot;&gt;&lt;br /&gt;&lt;br /&gt;public partial class GridView_GridViewDataListFirstAlphabet : System.Web.UI.Page&lt;br /&gt;{&lt;br /&gt;   DataTable dtProducts = new DataTable();&lt;br /&gt;&lt;br /&gt;   protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;   {&lt;br /&gt;       if (!IsPostBack)&lt;br /&gt;       {&lt;br /&gt;           InitialBindOfControls();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   protected void lnkFirstLetters_Click(object sender, EventArgs e)&lt;br /&gt;   {&lt;br /&gt;       BindGridView(((LinkButton)sender).Text.Trim());&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void InitialBindOfControls()&lt;br /&gt;   {&lt;br /&gt;       BindGridView(&quot;Initial&quot;);&lt;br /&gt;       BindDataList();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void BindGridView(string strChar)&lt;br /&gt;   {&lt;br /&gt;       string strQuery = &quot;SELECT ProductID, ProductName, QuantityPerUnit FROM Products &quot;&lt;br /&gt;                   + ((strChar == &quot;Initial&quot;) ? &quot;&quot; : &quot;WHERE ProductName LIKE&#39;&quot; + strChar + &quot;%&#39;&quot;)&lt;br /&gt;                   + &quot;ORDER BY ProductName&quot;;&lt;br /&gt;       using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ToString()))&lt;br /&gt;       {&lt;br /&gt;           using (SqlDataAdapter adapProducts = new SqlDataAdapter(strQuery, sqlConn))&lt;br /&gt;           {&lt;br /&gt;               adapProducts.Fill(dtProducts);&lt;br /&gt;               gvwProducts.DataSource = dtProducts;&lt;br /&gt;               gvwProducts.DataBind();&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private void BindDataList()&lt;br /&gt;   {&lt;br /&gt;       if (gvwProducts.Rows.Count != 0)&lt;br /&gt;       {&lt;br /&gt;           DataTable dtTemp = new DataTable();&lt;br /&gt;           dtTemp.Columns.Add(&quot;ProductNameFirstLetter&quot;);&lt;br /&gt;           dtTemp.Rows.Add(((Label)gvwProducts.Rows[0].FindControl(&quot;lblProductName&quot;)).Text.Substring(0, 1));&lt;br /&gt;           foreach (GridViewRow gvrProducts in gvwProducts.Rows)&lt;br /&gt;           {&lt;br /&gt;               if (dtTemp.Rows[dtTemp.Rows.Count - 1][0].ToString() != ((Label)gvrProducts.FindControl(&quot;lblProductName&quot;)).Text.Substring(0, 1))&lt;br /&gt;               {&lt;br /&gt;                   dtTemp.Rows.Add(((Label)gvrProducts.FindControl(&quot;lblProductName&quot;)).Text.Substring(0, 1));&lt;br /&gt;               }&lt;br /&gt;           }&lt;br /&gt;           dlstProducts.DataSource = dtTemp;&lt;br /&gt;           dlstProducts.DataBind();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Extra Namespace used&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: csharp&quot;&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br/&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/3302829138723954690/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/03/filtered-gridview-using-datalist.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3302829138723954690'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/3302829138723954690'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/03/filtered-gridview-using-datalist.html' title='Filtered GridView using DataList'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-777933054050893742.post-6148577956120495064</id><published>2009-02-02T09:56:00.012+05:30</published><updated>2010-12-07T19:03:12.535+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="gridview checkbox paging maintaining persisting state"/><title type='text'>Maintaining State of CheckBox while Paging in GridView</title><content type='html'>The Logic&lt;br /&gt;
&lt;blockquote&gt;We are storing the primary keys of the checkboxes that are checked into a List&lt;T&gt; where T = data type of the primary key. This is done at PageIndexChanging before we change the page index and re-bind the GridView. And at RowDataBound we are checking whether the DataKeyName(primary key) of each row is in the List. If present , we mark the CheckBox as checked.&lt;/blockquote&gt;Heres the sample code.&lt;br /&gt;
&lt;br /&gt;
The ASPX&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:xhtml&quot;&gt;&amp;lt;asp:LinkButton ID=&amp;quot;btnSelectAllPage&amp;quot; runat=&amp;quot;server&amp;quot; 
            CommandName=&amp;quot;SelectAllPage&amp;quot; 
            Text=&amp;quot;Select All Pages&amp;quot;
            OnCommand=&amp;quot;SelectDeselectAllPages&amp;quot; /&amp;gt;|
&amp;lt;asp:LinkButton ID=&amp;quot;btnDeselectAllPage&amp;quot; runat=&amp;quot;server&amp;quot; 
            CommandName=&amp;quot;DeselectAllPage&amp;quot; 
            Text=&amp;quot;Deselect All Pages&amp;quot;
            OnCommand=&amp;quot;SelectDeselectAllPages&amp;quot; /&amp;gt;|
&amp;lt;asp:LinkButton ID=&amp;quot;btnSelectAll&amp;quot; runat=&amp;quot;server&amp;quot; 
            CommandName=&amp;quot;SelectAll&amp;quot; 
            Text=&amp;quot;Select Page&amp;quot;
            OnCommand=&amp;quot;SelectDeselect&amp;quot; /&amp;gt;|
&amp;lt;asp:LinkButton ID=&amp;quot;btnDeselectAll&amp;quot; runat=&amp;quot;server&amp;quot; 
            CommandName=&amp;quot;DeselectAll&amp;quot; 
            Text=&amp;quot;Deselect Page&amp;quot;
            OnCommand=&amp;quot;SelectDeselect&amp;quot; /&amp;gt;

&amp;lt;asp:GridView ID=&amp;quot;gvProducts&amp;quot; runat=&amp;quot;server&amp;quot; 
            AllowPaging=&amp;quot;True&amp;quot; 
            AutoGenerateColumns=&amp;quot;False&amp;quot;
            DataKeyNames=&amp;quot;ProductID&amp;quot;
            OnPageIndexChanging=&amp;quot;gvProducts_PageIndexChanging&amp;quot; 
            OnRowDataBound=&amp;quot;gvProducts_RowDataBound&amp;quot;&amp;gt;
    &amp;lt;Columns&amp;gt;
        &amp;lt;asp:TemplateField HeaderText=&amp;quot;Select&amp;quot;&amp;gt;
            &amp;lt;ItemTemplate&amp;gt;
                &amp;lt;asp:CheckBox ID=&amp;quot;chkSelect&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;
            &amp;lt;/ItemTemplate&amp;gt;
        &amp;lt;/asp:TemplateField&amp;gt;
    &amp;lt;asp:BoundField DataField=&amp;quot;ProductID&amp;quot; HeaderText=&amp;quot;ProductID&amp;quot; InsertVisible=&amp;quot;False&amp;quot;
    ReadOnly=&amp;quot;True&amp;quot; SortExpression=&amp;quot;ProductID&amp;quot; /&amp;gt;
    &amp;lt;asp:BoundField DataField=&amp;quot;ProductName&amp;quot; HeaderText=&amp;quot;ProductName&amp;quot; SortExpression=&amp;quot;ProductName&amp;quot; /&amp;gt;
    &amp;lt;/Columns&amp;gt;
&amp;lt;/asp:GridView&amp;gt;
&amp;lt;asp:SqlDataSource ID=&amp;quot;sdsProducts&amp;quot; runat=&amp;quot;server&amp;quot;
                ConnectionString=&amp;#39;&amp;lt;%$ ConnectionStrings:NorthwindConnectionString %&amp;gt;&amp;#39;
                SelectCommand=&amp;quot;SELECT * FROM Products&amp;quot;&amp;gt;
&amp;lt;/asp:SqlDataSource&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
The ASPX.CS&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;private List&amp;lt;int&amp;gt; ProductIDs
{
    get
    {
        if (this.ViewState[&amp;quot;ProductIDs&amp;quot;] == null)
        {
            this.ViewState[&amp;quot;ProductIDs&amp;quot;] = new List&amp;lt;int&amp;gt;();
        }
        return this.ViewState[&amp;quot;ProductIDs&amp;quot;] as List&amp;lt;int&amp;gt;;
    }
}

protected void SelectDeselect(object sender, CommandEventArgs e)
{
    foreach (GridViewRow gvr in gvProducts.Rows)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            chkSelect.Checked = e.CommandName.Equals(&amp;quot;SelectAll&amp;quot;);
        }
    }
}

protected void SelectDeselectAllPages(object sender, CommandEventArgs e)
{
    DataSourceSelectArguments dssa = new DataSourceSelectArguments();
    dssa.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
    dssa.RetrieveTotalRowCount = true;
    DataView dv = (DataView)sdsProducts.Select(dssa);
    foreach (DataRow dr in dv.Table.Rows)
    {

        int productID = Convert.ToInt32(dr.ItemArray[0]);//ItemArray[0] cos ProductID is in the first column
        if (e.CommandName.Equals(&amp;quot;SelectAllPage&amp;quot;) &amp;amp;&amp;amp; !this.ProductIDs.Contains(productID))
        {
            this.ProductIDs.Add(productID);
        }
        if (e.CommandName.Equals(&amp;quot;DeselectAllPage&amp;quot;) &amp;amp;&amp;amp; this.ProductIDs.Contains(productID))
        {
            this.ProductIDs.Remove(productID);
        }
    }
    foreach (GridViewRow gvr in gvProducts.Rows)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            chkSelect.Checked = e.CommandName.Equals(&amp;quot;SelectAllPage&amp;quot;);
        }
    }
}

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    foreach (GridViewRow gvr in gvProducts.Rows)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex][&amp;quot;ProductID&amp;quot;]);
            if (chkSelect.Checked &amp;amp;&amp;amp; !this.ProductIDs.Contains(productID))
            {
                this.ProductIDs.Add(productID);
            }
            else if (!chkSelect.Checked &amp;amp;&amp;amp; this.ProductIDs.Contains(productID))
            {
                this.ProductIDs.Remove(productID);
            }
        }
    }
}

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkSelect = gvr.FindControl(&amp;quot;chkSelect&amp;quot;) as CheckBox;
        if (chkSelect != null)
        {
            int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex][&amp;quot;ProductID&amp;quot;]);
            chkSelect.Checked = this.ProductIDs.Contains(productID);
        }
    }
}
&lt;/pre&gt;&lt;br /&gt;
As an extra enhancement, we are also giving provision for selecting, deselecting both page and the whole rows in every page also here.&lt;br /&gt;
&lt;br /&gt;
P.S: We are using Northwind Sample DataBase by Microsoft, so if you dont have it download it.&lt;br /&gt;
&lt;br /&gt;
Courtesy: A &lt;a href=&quot;http://forums.asp.net/p/1370809/2866640.aspx#2866838&quot;&gt;wonderful post&lt;/a&gt; by Ed Bruck at forums.asp.net</content><link rel='replies' type='application/atom+xml' href='http://fun2code.blogspot.com/feeds/6148577956120495064/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://fun2code.blogspot.com/2009/02/maintaining-state-of-checkbox-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6148577956120495064'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/777933054050893742/posts/default/6148577956120495064'/><link rel='alternate' type='text/html' href='http://fun2code.blogspot.com/2009/02/maintaining-state-of-checkbox-in.html' title='Maintaining State of CheckBox while Paging in GridView'/><author><name>Naveen</name><uri>http://www.blogger.com/profile/18157353195373364087</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUQAqhfbQCoUfZqMDWLjYIU4hBmWWC0qWreEJvQi5hpVvwrYsaM0dnSTGbeVL6mreqpccxblvV25qYHOZO5OVD5qqcH2-5oKIp9fF9Mv0dtVc9-nI3adsP48JqNZagaQ/s220/naveenj.jpg'/></author><thr:total>0</thr:total></entry></feed>