473,503 Members | 12,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET GridView Custom BoundField

2 New Member
Hello,

I am trying to extend the System.Web.UI.WebControls.BoundField class to create a custom GridView / DetailsView column that will render other controls, for example a Calendar or a DropDownList.

I have made a start using some sample code from Stephen Walther's book "ASP.NET 2.0 Unleashed". (By the way, it's excellent).

The sample inherits from the basic BoundField class to create a multi-line text bound field:

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Web.UI
  3. Imports System.Web.UI.WebControls
  4. Imports System.Web.UI.HtmlControls
  5.  
  6. Namespace myControls
  7.     ''' <summary>
  8.     ''' Enables you to display a long text field
  9.     ''' </summary>
  10.     Public Class LongTextField
  11.         Inherits BoundField
  12.  
  13.         Private _width As Unit = New Unit("250px")
  14.         Private _height As Unit = New Unit("60px")
  15.  
  16.         ''' <summary>
  17.         ''' The Width of the field
  18.         ''' </summary>
  19.         Public Property Width() As Unit
  20.             Get
  21.                 Return _width
  22.             End Get
  23.             Set(ByVal Value As Unit)
  24.                 _width = Value
  25.             End Set
  26.         End Property
  27.  
  28.         ''' <summary>
  29.         ''' The Height of the field
  30.         ''' </summary>
  31.         Public Property Height() As Unit
  32.             Get
  33.                 Return _height
  34.             End Get
  35.             Set(ByVal Value As Unit)
  36.                 _height = Value
  37.             End Set
  38.         End Property
  39.  
  40.         ''' <summary>
  41.         ''' Builds the contents of the field
  42.         ''' </summary>
  43.         Protected Overrides Sub InitializeDataCell(ByVal cell As DataControlFieldCell, ByVal rowState As DataControlRowState)
  44.             ' If not editing, show in scrolling div
  45.             If (rowState And DataControlRowState.Edit) = 0 Then
  46.                 Dim div As HtmlGenericControl = New HtmlGenericControl("div")
  47.                 div.Attributes("class") = "longTextField"
  48.                 div.Style(HtmlTextWriterStyle.Width) = _width.ToString()
  49.                 div.Style(HtmlTextWriterStyle.Height) = _height.ToString()
  50.                 div.Style(HtmlTextWriterStyle.Overflow) = "auto"
  51.  
  52.                 AddHandler div.DataBinding, AddressOf div_DataBinding
  53.  
  54.                 cell.Controls.Add(div)
  55.             Else
  56.                 Dim txtEdit As TextBox = New TextBox()
  57.                 txtEdit.TextMode = TextBoxMode.MultiLine
  58.                 txtEdit.Width = _width
  59.                 txtEdit.Height = _height
  60.  
  61.                 AddHandler txtEdit.DataBinding, AddressOf txtEdit_DataBinding
  62.  
  63.                 cell.Controls.Add(txtEdit)
  64.             End If
  65.         End Sub
  66.  
  67.         ''' <summary>
  68.         ''' Called when databound in display mode
  69.         ''' </summary>
  70.         Private Sub div_DataBinding(ByVal s As Object, ByVal e As EventArgs)
  71.             Dim div As HtmlGenericControl = CType(s, HtmlGenericControl)
  72.  
  73.             ' Get the field value
  74.             Dim value As Object = Me.GetValue(div.NamingContainer)
  75.  
  76.             ' Assign the formatted value
  77.             div.InnerText = Me.FormatDataValue(value, Me.HtmlEncode)
  78.         End Sub
  79.  
  80.         ''' <summary>
  81.         ''' Called when databound in edit mode
  82.         ''' </summary>
  83.         Private Sub txtEdit_DataBinding(ByVal s As Object, ByVal e As EventArgs)
  84.             Dim txtEdit As TextBox = CType(s, TextBox)
  85.  
  86.             ' Get the field value
  87.             Dim value As Object = Me.GetValue(txtEdit.NamingContainer)
  88.  
  89.             ' Assign the formatted value
  90.             txtEdit.Text = Me.FormatDataValue(value, Me.HtmlEncode)
  91.         End Sub
  92.  
  93.     End Class
  94. End Namespace
  95.  
This sample works perfectly (as I would expect), but I don't understand how!

I can see that the InitializeDataCell() method is doing most of the work. The contained TextBox is being initialized with the bound value, but I can't see how the binding in the other direction works. Can anyone please explain how the GridView (or DetailsView) retrieves the value from the TextBox after a modification?

If you can point me in the right direction, I hope I will be able to replicate this principle for other contained controls.

Thanks in advance,
Tim
Aug 6 '07 #1
0 6509

Sign in to post your reply or Sign up for a free account.

Similar topics

8
5015
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
7
14784
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
5
8348
by: Mark Olbert | last post by:
I ran into a situation this morning where the RowUpdating event of a GridView kept insisting there were no entries in either the OldValues or NewValues dictionaries. The GridView is bound to a...
1
14298
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. ...
13
41953
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The...
8
18068
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
5
2966
by: Andrew Robinson | last post by:
I am attempting to better automate a Pager Template within a GridView. I am succesfully skinning a Drop Down List withing my control (the DDL is added to my control). I correctly populate the item...
4
3476
by: =?Utf-8?B?Y2hyaXM=?= | last post by:
I have a perent gridview which includes in a template field a child gridview. Child gridview includes command buttons for opening different windows based on its row selected. I used the code...
1
3057
by: SachinSachin | last post by:
Hi All, I am implementing a custom gridview control, that emits some javascript to blink a row whenever a new row is added in gridview. The gridview is inside the <asp:updatepanel> for...
4
8809
by: Peter | last post by:
I want to call a JavaScript on PageIndexChanged event, how do I do that? Thank You Peter
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.