Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



ASP.NET: Extend BoundField Control - Add MultiLine support

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;
   }
}

}

}

Currently rated 4.6 by 5 people

  • Currently 4.6/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Wednesday, September 20, 2006 12:18 AM
Permalink | Comments (11) | Post RSSRSS comment feed

Related posts

Comments

Ameya

Tuesday, October 03, 2006 1:08 AM

Ameya

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

Wednesday, November 08, 2006 2:40 AM

ll

It doesn't work. I think this code is incomplete how do you insert TextBox in the grid cell.

Mila

Wednesday, December 27, 2006 5:20 AM

Mila

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

Tuesday, January 30, 2007 8:34 PM

G

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

Sunday, April 01, 2007 12:49 PM

Adam

Thank you very much, it worked great and was very easy to implement thanks to your clear write up.

Des Legumes

Wednesday, May 16, 2007 7:54 PM

Des Legumes

Thanx, this really made the difference, had a big description field in details view which looks much better as a multiline textbox ;))

fritz

Thursday, August 02, 2007 3:27 AM

fritz

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

Wednesday, August 29, 2007 2:25 PM

PSay

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

Wednesday, September 12, 2007 12:10 AM

Mike

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.

Seiti

Thursday, September 13, 2007 3:32 AM

Seiti

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?

Chris Pietschmann

Thursday, September 13, 2007 11:01 AM

Chris Pietschmann

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.

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann