ASP.NET: Extend BoundField Control - Add MultiLine support

20. September 2006

I've been working with the data controls and it's somewhat cumbersome to use a TemplateField control everytime you want to just have a MultiLine TextBox. So, I decided to extend upon the BoundField control to add MultiLine support. It is actually rather simple to do.

Using the BoundTextBoxField control in the page:

<%@ Register Namespace="CustomBoundField" TagPrefix="custom" %>

<asp:GridView ID="GridView1" runat="server">
<Columns>
<custom:BoundTextBoxField DataField="Text" HeaderText="MultiLine Text" SortExpression="Text" TextMode="MultiLine" Rows="5" Columns="25" Wrap="True" />
</Columns>
</asp:GridView>

 

Code for the BoundTextBoxField control (~/App_Code/BoundTextBoxField.cs):

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomBoundField
{

/// <summary>
/// Summary description for BoundTextBoxField
/// </summary>
public class BoundTextBoxField : System.Web.UI.WebControls.BoundField
{

public TextBoxMode TextMode
{
   get
   {
      TextBoxMode _tm = TextBoxMode.SingleLine;
      if (this.ViewState["TextMode"] != null)
         _tm = (
TextBoxMode)this.ViewState["TextMode"];
      return _tm;
   }
   set { this.ViewState["TextMode"] = value; }
}

public int Columns
{
   get
   {
      int i = 0;
      if (this.ViewState["Columns"] != null)
         i = (
int)this.ViewState["Columns"];
      return i;
   }
   set { this.ViewState["Columns"] = value; }
}

public int Rows
{
   get
   {
      int i = 0;
      if (this.ViewState["Rows"] != null)
         i = (
int)this.ViewState["Rows"];
      return i;
   }
   set { this.ViewState["Rows"] = value; }
}

public bool Wrap
{
   get
   {
      bool b = true;
      if (this.ViewState["Wrap"] != null)
         b = (
bool)this.ViewState["Wrap"];
      return b;
   }
   set { this.ViewState["Wrap"] = value; }
}

protected override void OnDataBindField(object sender, EventArgs e)
{
   base.OnDataBindField(sender, e);
   Control c = (Control)sender;
   if (c is TextBox)
   {
      TextBox txt = (TextBox)c;
      txt.TextMode =
this.TextMode;
      txt.Columns =
this.Columns;
      txt.Rows =
this.Rows;
      txt.Wrap =
this.Wrap;
   }
}

}

}

 

asp.net

Comments

Ameya
Ameya
10/3/2006 9:08:00 AM #
This is the fantastic article for many days i just wondered how can i extend the textbox in boundfield to have a multiline property ... many of my friends said this is not possible ...well now i think nothing is impossible ...that gr8 keep it up...
ll
ll
11/8/2006 10:40:00 AM #
It doesn't work. I think this code is incomplete how do you insert TextBox in the grid cell.
Mila
Mila
12/27/2006 1:20:00 PM #
This was very helpful.  Thank you.  I can input data on multiple lines, but when it displays, it's still a single line.  What am I missing?  Thanks again.
G
G
1/31/2007 4:34:00 AM #
I am trying to set the HeaderText from OnDataBindField, but it is always empty. Does anyone have any suggestions on how to do this? Thanks!
Adam
Adam
4/1/2007 8:49:00 PM #
Thank you very much, it worked great and was very easy to implement thanks to your clear write up.
Des Legumes
Des Legumes
5/17/2007 3:54:00 AM #
Thanx, this really made the difference, had a big description field in details view which looks much better as a multiline textbox ;))
fritz
fritz
8/2/2007 11:27:00 AM #
Is there a way to get the custom BoundField to show up when using the smart tag in design mode (when I click Edit Columns)?
PSay
PSay
8/29/2007 10:25:00 PM #
The is a problem, when used in a DetailsView and the mode is set to insert, as a result the OnDataBindField event isn't called as there is no data to bind. Therfore you are still left with a standard textbox
Mike
Mike
9/12/2007 8:10:00 AM #
Chris,
  I have been trying to find a way to do what you did in the custom tag for days.  Thanks!  
On a side note: Is there a way display the text the same way it is displayed in the edit mode?  I need to display the text the same way it was entered to include the CR/LFs.  Thanks.
9/13/2007 11:32:00 AM #
Nice control!
I found out that the intellisense doesn´t work when using custom boundfields. Is there anything, like attributes, needed to make it work?
9/13/2007 7:01:00 PM #
Actually, I don't know how to get the intellisense to work. I think the reason it doesn't work is because the GridView doesn't recognize my custom boundfield control at design time.