<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://v900u039rux.maximumasp.com/Web/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Raheel Hussain : ItemCommand</title><link>http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/ItemCommand/default.aspx</link><description>Tags: ItemCommand</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>Sub Classing the ASP.NET Pages</title><link>http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/2007/08/06/centralizing-the-rowdatabound-amp-itemdatabound-events.aspx</link><pubDate>Mon, 06 Aug 2007 10:16:00 GMT</pubDate><guid isPermaLink="false">3790bee0-d05b-4b84-a272-3ed522a0473a:48</guid><dc:creator>Raheel Hussain</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://v900u039rux.maximumasp.com/Web/blogs/raheel/rsscomments.aspx?PostID=48</wfw:commentRss><comments>http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/2007/08/06/centralizing-the-rowdatabound-amp-itemdatabound-events.aspx#comments</comments><description>&lt;div style="BACKGROUND-COLOR:#ffffff;"&gt;
&lt;table class="" cellspacing="0" cellpadding="5"&gt;

&lt;tr&gt;
&lt;td class="" style="VERTICAL-ALIGN:top;WIDTH:8%;TEXT-ALIGN:left;"&gt;&lt;a href="http://yukonizer.com/Web/blogs/raheel/untitled.bmp"&gt;&lt;img src="http://yukonizer.com/Web/blogs/raheel/untitled.bmp" border="0" alt="" /&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td class="" style="VERTICAL-ALIGN:top;WIDTH:92%;"&gt;The goal of every programmer is to shorten the code as much as possible. &lt;br /&gt;Although for us, Microsoft has done a lot in the form of ASP.NET (Vb.net or C#) controls and lot of namespaces and libraries available that we do not really need to write our code at certain places.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="" colspan="2"&gt;But those are not business logics. Business logic is what you need to write your way, according to the needs of your client. I write code in the “itemdatabound” event of Listview control as well as the “RowDataBound” event of Gridview Controls. But I thought for a moment that, if there are multiple pages using the same GridView or ListView and more or less same logic in the “RowDataBound” or “ItemDataBound” events then why not I make a centralized event handler? One way to do this is to simply write comma separated Control IDs with the Handles clause&lt;br /&gt;&lt;br /&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound,GridView2.RowDataBound

End Sub
&lt;/pre&gt;
&lt;div&gt;&lt;/div&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;But this approach will only be available for multiple GridView or ListView controls within a single page.&lt;br /&gt;&lt;br /&gt;A little tricky but interesting solution for that is to inherit the ASPX pages with another class (say ParentPage). &lt;br /&gt;&lt;br /&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;&amp;#39;&amp;#39;page1.aspx.vb
Partial Class page1
    Inherits ParentPage
End Class

&amp;#39;&amp;#39;page2.aspx.vb
Partial Class page2
    Inherits ParentPage
End Class

&amp;#39;&amp;#39;ParentPage.vb
Public Class ParentPage
    Inherits System.Web.UI.Page
End Class

&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;The above code shows three classes, two are partial classes, of page1.aspx and page2.aspx pages respectively, but you can notice that those two classes are extended by another class called as ParentPage. I replaced the System.Web.UI.Page with ParentClass&lt;br /&gt;&lt;br /&gt;The ParentPage class&amp;nbsp;is extended with the System.Web.UI.Page. instead.&lt;br /&gt;&lt;br /&gt;The reason to do this obvious, that is to make use of centralized procedures, which I&amp;#39;m going to do below.&lt;br /&gt;&lt;br /&gt;Note that the approach of having this ParentPage is not necessarily the only way to do this, but having the events in a Parent class is useful in many ways, such as you are taking advantage of the OO features of using the public procedures of Parent class which are viewable to all child classes, as well as there&amp;#39;s no need to instantiate a new object everytime.&lt;br /&gt;&lt;br /&gt;Another way could be to use a static (shared in vb.net) method instead. But this approach will have restrictions. Such as you may require to make use of a method exposed by an instantiated object of any other class and definetely the static method will not allow you to do instantiation within its body (code block).&lt;br /&gt;&lt;br /&gt;The child pages will call the centralized event procedure as shown in the code below:&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        MyBase.EventHandlerForRowDataBound(sender, e)
    End Sub
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;There&amp;nbsp;may be some pages (such as user controls and master pages) which are not inherited (extended) from the ParentPage class, and they might&amp;nbsp;contain the &lt;br /&gt;GridView or ListView, obviously, then there is a slight change, and instead of Mybase it should be an object on the fly.&lt;br /&gt;&lt;br /&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Call (New ParentPage()).EventHandlerForRowDataBound(sender, e)
End Sub
&lt;/pre&gt;&lt;/div&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;While this is also obvious, that if there are multiple GridView across multiple pages (master pages, controls, childpages) which are using a centrlized procedure, then there must some GridView specific operations which are particular to some specific GridView control on a specific page. Below code would illustrate a typical look of the event code blocks which I made to control this. &lt;br /&gt;&lt;br /&gt;the &amp;quot;sender&amp;quot; object will act here as the source of the event, that is the GridView and we can&amp;nbsp;use the &amp;quot; ID &amp;quot;&amp;nbsp;property (not shown in the intellisence help, and you&amp;#39;ll have to type it)&amp;nbsp; if the GridView to differentiate between different GridViews. While you can also make use of the &amp;quot; Page &amp;quot; property of the sender as shown in the following code snippet.&lt;br /&gt;&lt;br /&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;    Public Sub EventHandlerForRowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        &amp;#39;Your code should come here
        If e.Row.RowType = DataControlRowType.DataRow Then
            &amp;#39;Your code here
            If sender.id.ToString() = &amp;quot;GridView1&amp;quot; Then
                &amp;#39;Some specific operation
            ElseIf sender.id.ToString() = &amp;quot;GridView2&amp;quot; Then
                &amp;#39;Some specific operation
            End If

            If sender.page.ToString().Contains(&amp;quot;page1&amp;quot;) Then
                &amp;#39;Some specific
            End If
            &amp;#39;Common Code Operations
        End If
    End Sub
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;To be noted here, is that the Event Delegate signatures must be same to make it work. That means that the procedure must follow the same&amp;nbsp;signature as the actual event of the RowDataBound or ItemDataBound have.&lt;br /&gt;&lt;br /&gt;You can&amp;nbsp;learn more about Event Delegates&amp;nbsp;from&amp;nbsp;the following URL.&lt;br /&gt;&lt;span style="FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-ansi-language:EN-US;mso-no-proof:yes;"&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx"&gt;http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;
&lt;div&gt;Hense the procedure in the ParentPage class, should look like below code.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style="BORDER-RIGHT:1px solid;BORDER-TOP:1px solid;OVERFLOW:scroll;BORDER-LEFT:1px solid;WIDTH:546px;BORDER-BOTTOM:1px solid;BACKGROUND-COLOR:#f5f5f5;"&gt;&lt;pre&gt;    Public Sub EventHandlerForRowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        &amp;#39;Your code should come here
    End Sub
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Within the above procedure, you can make use of other objects, which are not static as well as use the Response.Write and all the other methods which are supported by the .aspx page as the ParentPage class it extending from System.Web.UI.Page class.&lt;br /&gt;&lt;br /&gt;Moreover, not only the RowDataBound or ItemDataBound event, but this would clear the idea that veriety of events can be centralized this way, according to the business logice of the application you are working on.&lt;br /&gt;&lt;br /&gt;Happy Coding !!&amp;nbsp;&lt;br /&gt;&lt;br /&gt;PS ! Do not sit for long hours continous while programming, take some walk, have tea (or whatever) to keep your body active, to avoid backache. Because this is not at all&amp;nbsp;co-incidance that I and danish having same problem, both went to doctor, both took pain killer, and now both are advised light excercises........&lt;br /&gt;&lt;br /&gt;regards - raheel&lt;img src="http://v900u039rux.maximumasp.com/Web/aggbug.aspx?PostID=48" width="1" height="1"&gt;</description><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/RowCommand/default.aspx">RowCommand</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/System.Web.UI.Page/default.aspx">System.Web.UI.Page</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/RowDataBound/default.aspx">RowDataBound</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/GridView/default.aspx">GridView</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/ItemDataBound/default.aspx">ItemDataBound</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/ListView/default.aspx">ListView</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/raheel/archive/tags/ItemCommand/default.aspx">ItemCommand</category></item></channel></rss>