Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29985 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Extending Grid View Control of ASP .NET

Extending Grid View Control of ASP .NET

Article posted by Hareshambaliya on 6/29/2009 | Views: 4798 | Category: ASP.NET | Level: Advance red flag


If you want to modify some functionality of existing Grid View control of ASP .Net then you always have the option of extending the control. In this article I have extend the Grid View with custom field.

To extend the Grid View control you can inherit the new class from existing class or any of the following class

1). DataControlField – The base class for bind field

2). ButtonFieldBase – The base class for all button field and command field.

In this article I am extending the Grid View control of ASP .Net with LongTextField and ConfirmDeleteButton (it will be in my next blog)

Extending Grid View Control of ASP .NET

If you want to modify some functionality of existing Grid View control of ASP .Net then you always have the option of extending the control. In this article I have extend the Grid View with custom field.

To extend the Grid View control you can inherit the new class from existing class or any of the following class

1). DataControlField – The base class for bind field

2). ButtonFieldBase – The base class for all button field and command field.

In this article I am extending the Grid View control of ASP .Net with LongTextField and ConfirmDeleteButton (it will be in my next blog)

Extending the Grid View control with LongTextField

None of the existing Grid View field provide facility for handling the long text like description field, so I am going to create LongTextField, which is used to display the long text regard less of the length text.

In normal display mode Grid View control display text in scrolling <DIV> tab, and in edit mode it will display text in multi-line text box. To create custom field, a new class must inherited from base class BoundField control. The following listing contain the code for LongTextField.

Imports Microsoft.VisualBasic Namespace myControls Public Class LongTextField Inherits BoundField Private _Height As Unit = New Unit("60px") Private _Width As Unit = New Unit("250px") Public Property Height() As Unit Get Return _Height End Get Set(ByVal value As Unit) _Height = value End Set End Property Public Property Width() As Unit Get Return _Width End Get Set(ByVal value As Unit) _Width = value End Set End Property Protected Overrides Sub InitializeDataCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal rowState As System.Web.UI.WebControls.DataControlRowState) ' If not editing, show in scrolling div If (rowState And DataControlRowState.Edit) = 0 Then Dim div As New HtmlGenericControl("div") div.Attributes("class") = "longTextField" div.Style(HtmlTextWriterStyle.Width) = _Width.ToString() div.Style(HtmlTextWriterStyle.Height) = _Height.ToString() div.Style(HtmlTextWriterStyle.Overflow) = "auto" AddHandler div.DataBinding, AddressOf div_DataBinding cell.Controls.Add(div) Else Dim txtEdit As New TextBox() txtEdit.TextMode = TextBoxMode.MultiLine txtEdit.Width = _Width txtEdit.Height = _Height AddHandler txtEdit.DataBinding, AddressOf txtEdit_DataBinding cell.Controls.Add(txtEdit) End If End Sub ''' <summary> ''' Called when databound in display mode ''' </summary> Private Sub div_DataBinding(ByVal s As Object, ByVal e As EventArgs) Dim div As HtmlGenericControl = DirectCast(s, HtmlGenericControl) ' Get the field value Dim value As [Object] = Me.GetValue(div.NamingContainer) ' Assign the formatted value div.InnerText = Me.FormatDataValue(value, Me.HtmlEncode) End Sub ''' <summary> ''' Called when databound in edit mode ''' </summary> Private Sub txtEdit_DataBinding(ByVal s As Object, ByVal e As EventArgs) Dim txtEdit As TextBox = DirectCast(s, TextBox) ' Get the field value Dim value As [Object] = Me.GetValue(txtEdit.NamingContainer) ' Assign the formatted value txtEdit.Text = Me.FormatDataValue(value, Me.HtmlEncode) End Sub End Class

End Namespace

In this listing InitializeDataCell is overridden, this method is responsible for creating all the control in Normal and Edit mode. First check for the mode, if the mode is not in edit then div tag is created and text is rendered inside div. If the mode is edit then Multi-line text box is created and text is render inside the Multi-line text box.

You can use LongTextField as shown in following listing. This page use the LongTextField  for display the holiday description.

@ Page Language="VB" AutoEventWireup="false" CodeFile="Temp.aspx.vb" Inherits="Temp" EnableEventValidation="false" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
  <asp:GridView ID="grvHoliday" runat="server" AutoGenerateColumns="False" DataSourceID="lds_Holiday"
                AutoGenerateEditButton="True" DataKeyNames="Holiday_ID">
                <RowStyle HorizontalAlign="Left" VerticalAlign="Top" BorderWidth="0px" />
                <EditRowStyle VerticalAlign="Top" HorizontalAlign="Left" BorderWidth="0px" />
                <Columns>
                    <CustomField:DeleteButtonField>
                    </CustomField:DeleteButtonField>
                    <asp:BoundField DataField="Holiday_ID" HeaderText="Holiday_ID" ReadOnly="True" SortExpression="Holiday_ID"
                        Visible="False" />
                    <asp:TemplateField HeaderText="Title" SortExpression="Title">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <CustomField:LongTextField Height="60px" Width="300px" DataField="Description" HeaderText="Description">
                    </CustomField:LongTextField>
                    <%--<asp:TemplateField HeaderText="Description" SortExpression="Description">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' TextMode="MultiLine"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>--%>
                    <asp:TemplateField HeaderText="Date" SortExpression="Date">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date", "{0:d}") %>'></asp:TextBox>
                            <cc1:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True"
                                TargetControlID="txtDate">
                            </cc1:CalendarExtender>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDate" runat="server" Text='<%# Bind("Date", "{0:d}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:LinqDataSource ID="lds_Holiday" runat="server" ContextTypeName="HolidayDataContext"
                TableName="College_Holidays" EnableDelete="True" EnableUpdate="True">
            </asp:LinqDataSource>
</body>
</html>

When you run the page it will display as shown in image

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:2 year(s)
Home page:http://www.linqtechnologies.com
Member since:Tuesday, November 11, 2008
Level:Starter
Status: [Member]
Biography:I was born in 25th March ,1983 - India. I pursue my bachelor of engineering in Information Technologies from C. U. Shah Engineering, Saurashtra University. I am very honest and active in my life.

I started my career in Intelligent Business Solution Development India Pvt. Ltd. , Ahmedabad, Which is subsidiary of IBSG Inc., Celebration, Florida - USA. I worked 8 Months for IBSG Inc. Unfortunately due to recession in USA the IBSGI was totally shutdown.

After shutdown of IBSGI I started to work for Rapid Infosoft (Ahmedabad), I have worked 6 to 7 months for Rapid Infosoft.

Currently I am working for VIS Pvt. Ltd. (Ahmedabad).

Skills:
ASP .Net, C# .Net, VB .Net, DotNetNuke CMS, LINQ, SQL Server 2000,2005, eCommerce CMS like nopCommerce.
>> Write Response - Respond to this post and get points
Related Posts

In this article, I tried to explore the new feature URL Routing of ASP.NET 4.0.

Post substitution cache wllows us to write dynamica content into web page if the page is served from cache.

Vulnerability in ASP.NET Could Allow Information leak, how to solve it and link to good knowledge base post ;)

In this article, I am going to describe how to send email with attachment in ASP.NET. I am going to use System.IO, System.Net and System.Net.Mail namespaces in this article.

In this article we will get an idea, about how to encode query string while information passing and how to decode it at the receiver end.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:57:50 AM