Click here to Skip to main content
Click here to Skip to main content

Migrating BoundColumn to BoundField Fix

By , 6 Sep 2007
Rate this:
Please Sign up or sign in to vote.

Introduction

When converting DataTables to GridView controls in large (intranet), object reference errors often occur when a BoundColumn is set to "Visible=false".

Background

In applications that use the DataTable control, BoundColumns are sometimes set to invisible when a developer wants to hide data from users while using hidden data in client-side applications such as JavaScript.

The BoundField however, doesn't render any data on the client side when "visible" is set to false. When converting large applications, this might cause some problems. The normal fix would be to set the "visible" property programmatically to false after the "OnDataBind" event. However, this solution costs time.

To solve this problem in a short time span, I made my own class that uses BoundField as its base. The class simply solves the issue by setting the value of the field to visible on init, and restoring it after the field is bound.

Using the Code

To use this solution, place a new class in your app code called BoundColumnField. Add the following code to that class:

namespace GridViewFix 
{
    public class BoundColumnField : BoundField
    {    
        private Boolean NotVisible;
        protected override void InitializeDataCell(
                  DataControlFieldCell cell, DataControlRowState rowState)
        {
            NotVisible = false;
            //If the Field isn't set to visible=true
            //the control will set it to visible.

            if (!this.Visible)
            {
                this.Visible = true;
                NotVisible = true;
            }
            base.InitializeDataCell(cell, rowState);
        }

        protected override void OnDataBindField(object sender, EventArgs e)
        { 
            base.OnDataBindField(sender, e);
            //If the Field had visible=False the field will be hidden after databind. 
            if (NotVisible) this.Visible = false;
        } 
    }
}

First register your namespace on the top of each page where you want to use this control, as follows:

<%@ Register TagPrefix="GridViewFix" Namespace="GridViewFix"%>

Next, you can use the class in your DataGrids (using <GridViewFix:BoundColumnField> instead). The usage of the class is the same as that of the BoundField class. Only the render behavior is compatible with the old BoundColumn control.

Points of Interest

Microsoft removed the rendering of invisible fields for security reasons. This fix changes the behavoir of hidden fields. Please keep in mind that hidden values will be accessible to users when they open the client-side code.

History

  • 1.0: Added my first article here - 06 September 2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

JornP
Web Developer
Unknown
Currently Junior Software Developer for Atos Origin. Working at BAS Netherland for the Microsoft Unit.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 6 Sep 2007
Article Copyright 2007 by JornP
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid