Last post Jun 29, 2010 01:48 AM by group_solutions
Jun 25, 2010 04:30 PM|group_solutions|LINK
Jun 25, 2010 06:23 PM|mbanavige|LINK
It seems based on what my programmer told me that I cannot change design layout as it only inherits class and can only change events and property using the common user control
I believe your programmer is thinking of a custom server control rather than a usercontrol.
If you use a usercontrol (.ascx) then yes - you can quickly create a reusable customized control.
The design pattern you should be thinking of here is Decorator.
When you create a true Decorator, the decorator and the object that it decorates can be used interchangeably. So if you were to decorate a RAD control or a GridView (any control), the decorator (which contains the control it is decorating) would need to
re-expose each and every property and each and every event and interface of the control it is decorating.
That's a lot of work though and most of the time, the reason I'd do this is that i want to have strict control over various aspects of the control I'm decorating and do not want the other developers to change them or need to worry about them.
So i tend to create partial decorators. As a partial decorator, some but not all of the inner controls events and properties get re-exposed. I end up with a usercontrol that "sorta" looks a little like the control it is decorating.
For example, lets consider decorating a GridView using a usercontrol.
Create an ascx usercontrol with this markup:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="ControlWrapper_WebUserControl" %>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" AllowSorting="true">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
There was no data.
Clearly we now have a grid that's been customized and supports paging and sorting as well as having a default "no data" message.
Note: put your styles in a CSS file. This is just for demo purposes. ;-)
Then in the code behind for the usercontrol, we re-expose some of the properties of the control we're decorating that our consumers will still need/want access to.
Partial Class ControlWrapper_WebUserControl
'i'm going to reraise these events
'other events from the inner gridview
'are not going to be needed so im ignoring them
Public Event PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
' ... more if needed
're-expose properties of the object we are wrpaping as needed
'developers who use this usercontrol can then gain partial access to the control we're decorating.
Public Property PageSize() As Integer
Set(ByVal value As Integer)
Me.GridView1.PageSize = value
Public Property Datasource() As Object
'defer to the object we are decorating
Set(ByVal value As Object)
Me.GridView1.DataSource = value
'if we expose this, then a consumer could change
'any property of the gridview we're decorating
'possibly undoing some of our customizations.
'i dont usually do this... but you could
Public ReadOnly Property InnerGridView() As GridView
'Re-raise events of the object we are wrapping as needed
Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
'by reraising the event, we're starting to look like
'and behave like the gridview we're wrapping
RaiseEvent PageIndexChanged(Me, e)
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
RaiseEvent Sorting(Me, e)
' more as needed ...
Now, instead of dragging a gridview onto a page, you can drag an instance of this usercontrol onto the page.
This usercontrol can have a datasource set, it can raise paging and sorting events. consumers can set a custom pagesize. The rest of what the gridview does is generally tucked away and preset by this new usercontrol.
Now when this gridview like user control get used in various places throughout your site instead of a gridview, you have a single place to go to make adjustments to the design of that custom grid.
you should be able to do something very similar with any control
Jun 29, 2010 01:48 AM|group_solutions|LINK
Thanks for the reply. My coder is using usercontrol and not custom server control. I had him post a question directly at this link http://forums.asp.net/t/1573070.aspx with more details and he tried your solution and others but it did not work.
Can you please check and try to help?
He said it is not possible to inheit class in design mode.
I really need to get this done so any help is really appreciated. Thanks again.